In the user data layer, why does the author prefer moving deleted users to an xuser table instead of adding a Boolean deleted field to the user table?
The author prefers a separate xuser table because it avoids the need to repeatedly check a deleted flag in every user query, and because indexing a low-cardinality Boolean field does not meaningfully speed up queries. Moving deleted users out keeps the active user table simpler to query and potentially faster.
Instead of adding a Boolean deleted field to the user table and leaving inactive records in place, the author removes the deleted user's row from the user table and inserts the same data into an xuser table. This design means each user query does not need to include an extra condition to filter out deleted users, eliminating repetitive checks. The author also notes that an index on a Boolean field is ineffective because the field has low cardinality, so retaining the Boolean would not provide a query speed benefit. The delete function in the data layer demonstrates this approach: it fetches the user, deletes the row from user, and then creates the same user record in the xuser table.
Key points
- A Boolean deleted field would require filtering out deleted users in every user query.
- Separating deleted users into an xuser table avoids that repetitive checking.
- An index on a Boolean field is not helpful because of its low cardinality.
- The delete operation removes the user from user and adds the record to xuser.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.