March 12, 2011 at 12:43 pm
Hello,
I use Sql Server 2005 Enterprise.
When I ran the following query, Sqlserver fails to use the Primary key index on [id] column.
SELECT * FROM REQUEST_LOG WHERE id = 343323 AND Content = '';
If I drop the Content = '' criteria, result comes up immediately.
Do you have a comment on this issue? And how to improve Sqlserver 's default query plans mechanism without giving index hint.
Thanks in advance,
-Mehmet
March 12, 2011 at 1:22 pm
Likely the statistics for the Content column indicates the is might be enough rows with a '', that it's worth just scanning the table. Even if you think it's quicker to use the index, it's possible that SQL Server might not.
If ID is the clustered index, I might leave it alone, but if you run this query often, I might add a nonclustered index on ID and include Content either as an indexes column, or an include.
March 12, 2011 at 1:39 pm
Is ID the only primary key column? Is it clustered?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 12, 2011 at 2:13 pm
Thanks for quick reply.
ID is the only primary key field, it has non-clustered index.
Content has no index or is not part of any index.
Since ID is primary key, and since table has more than 200million records, it is hard to understand how SqlServer comes up with wrong query plan.
Given this bad decision of query executing, I have doubts in other queries.
By the way, I have executed this sample query to test since recently waits are increased in SqlServer. So it is important that I have confidence SqlServer does its best.
Regards,
-Mehmet
March 12, 2011 at 2:53 pm
Where's the clustered index? Any reason the PK was made nonclustered?
Can you post the execution plan please?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 12, 2011 at 3:03 pm
The bottom level of nodes in the index is called the leaf nodes
In a clustered index, the leaf nodes contain the data pages of the underlying table.
Read this article and learn how to use indexes.
http://technet.microsoft.com/en-us/library/ms177443.aspx
Your quote:
Given this bad decision of query executing, I have doubts in other queries.
It is not a bad decision of the database engine, but rather a bad decision(s) on whomever designed the table(s).
March 12, 2011 at 4:00 pm
Hello,
Strangely, same query brings result immediately now.
In the meantime, I have gotten estimated query plan and realized query plan (though I had to abort query after 30 sec since this is a high transaction system) a few times.
Estimated query plan showed index seek and row lookup correctly, but It was doing something I guess. Doing these must have helped SqlServer correct itself, I guess.
I have checked msdn links. But there is no obvious advantage for this query btw clustered and nonclustered index. It will go to one row via index from root to leaf where it will find the row data in clustered index whereas it has to make one more visit in nonclustered index.
Thanks all for your time.
March 12, 2011 at 8:04 pm
But there is no obvious advantage for this query btw clustered and nonclustered index.
Maybe not. But you're really going to hate yourself if this table get's any depth to it because you can't rebuild nonclustered indexes unless there's a clustered index on the table.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 13, 2011 at 3:25 am
Jeff Moden (3/12/2011)
...because you can't rebuild nonclustered indexes unless there's a clustered index on the table.
????
You can't rebuild the table (to eliminate forwarding pointers or compact pages) if there's no cluster, but the nonclustered indexes can still be rebuilt
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 13, 2011 at 3:26 pm
Boy, do I need some sleep or what. You're, of course, spot on, Gail. I'm not sure where my comment came from. :blush:
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply