This is probably a really stupid question, but is there going to be much benefit in indexing a boolean field in a database table?
Given a common situation, like "soft-delete" records which are flagged as inactive, and hence most queries include WHERE deleted = 0
, would it help to have that field indexed on its own, or should it be combined with the other commonly-searched fields in a different index?
No.
You index fields that are searched upon and have high selectivity/cardinality. A boolean field's cardinality is obliterated in nearly any table. If anything it will make your writes slower (by an oh so tiny amount).
Maybe you would make it the first field in the clustered index if every query took into account soft deletes?
What is about a deleted_at DATETIME column? There are two benefits.
You query could look like this:
SELECT * FROM xyz WHERE deleted_at IS NULL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With