indexing - SQL Server - Query performs Index Scan instead of Seek -
i'm in process of indexing content of cms lucene, have extended sql server database schema add "isindexed" bit column, lucene indexer can find piece of content hasn't been indexed.
i added index content
table lookups isindexed
column should go faster. database looks like:
create table content ( documentid bigint, categoryid bigint, title nvarchar(255), authoruserid bigint, body nvarchar(max), isindexed bit ) create table users ( userid bigint, username nvarchar(20) )
the following indexes exist:
content ( pk_content (clustered) : documentid asc ix_categoryid (non-unique, non-clustered) : categoryid asc ix_authoruserid (non-unique, non-clustered) : authoruserid asc ix_indexed_asc (non-unique, non-clustered) : isindexed asc, documentid asc ix_indexed_desc (non-unique, non-clustered) : isindexed desc, documentid asc ) users ( pk_users (clustered) : userid )
this query used find nonindexed content:
select top 1 content.documentid, content.categoryid, content.title, content.authoruserid, content.body users.username content inner join users on content.authoruserid = users.userid isindexed = 0
however when run actual execution plan reports clustered index scan pk_content combined clustered index seek pk_users. query takes 300ms execute.
when modify query remove users.username field , users inner-join, query takes 60ms run , there no clustered index scan pk_content, clustered index seek pk_content.
i tried before , after adding descending index content.isindexed
column, added content.documentid
ix_indexed indexes, made no difference.
what doing wrong? i've made necessary indexes (and some). content table has hundreds of thousands of rows, users table, can't see why optimiser choose scan.
an index on such low selectivity column (only 2 values 0 , 1) going ignored, see the tipping point. 1 option move leftmost key in clustered index, , make primary key constraint on documentid non-clustered index:
create table content ( documentid bigint, categoryid bigint, title nvarchar(255), authoruserid bigint, body nvarchar(max), isindexed bit, constraint pk_documentid primary key nonclustered (documentid) ) create unique clustered index cdxcontent on content (isindexed, documentid);
another option create filtered covering index:
create unique index nonindexedcontent on content (documentid) include (categoryid, title, authoruserid, body) isindexed = 0;
this second option duplicate lot of content possibly. personally, go first option.
Comments
Post a Comment