简体   繁体   中英

SQL Server - options for indexing

I have about 200+ tables in my database. Every table has two columns IsDeleted and IsActive with type BIT for logical deletes. In all queries I check whether a row is not deleted or not IsDeleted = 0 and IsActive = 1 .

My question is: how do I create my indexes for best performance?

This is one of the main scenarios for which Filtered Indexes were added. In this scenario Filtered Indexes allow you to create indexes that don't include the deleted or inactive rows, making the indexes smaller and the queries faster.

The alternatives are to add the status columns to all the indexes as key columns or to use partitioning or a separate archive table. And neither is as simple as Filtered Indexes.

Indexes are useful when they are highly selective. There is additional overhead when indexes are added to the database. So if you have a million row table and the indexed column can only have one of the two values (IsActive or IsDeleted) then you might as well forget indexing that column because there is very little difference in the indexed search and a table scan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM