Quick question for the DBA’s out there:
Say I have 2 columns on my table, IsDeleted (bit) and DeletedDate (datetime). The table contains approx 10,000,000 rows.
IsDeleted is a computed column that checks to see if DeletedDate is NULL; and it returns 1 if it is not, and 0 if it is.
Querying this table will mainly be done on the IsDeleted column.
Could anybody give me some suggestions on where I should apply my index?
Applying it to the IsDeleted field brings in a crop of problems due to SET QUOTED IDENTIFIER being off, which isn’t necessarily a deal breaker, but would cause some additional work.
Would I see any benefit applying it to DeletedDate, even though I’m not querying that field directly? Should I just bite the bullet and add it to IsDeleted? Is the performance difference between the two negligible?
Thanks again; and if you’d like any clarifications; leave me a comment and I’ll update my post.
It doesn’t make sense to put an index on a bit column because it is not selective enough. When executing a query, SQL Server determines the most appropriate indexes to use. If your index is not selective enough, it will be ignored or it may decide to do an index scan instead of an index seek. Either way, it won’t really help all that much.
Putting the index on the DeletedDate could possibly help with some queries, but filtering on NULL vs. “any value” will probably not be that much help either because of the selectivity.
I encourage you to read this:
Seek Vs. Scan