September 21, 2006 05:21 PM

Why Learn About Internals?

Insight into SQL Server's inner workings can hone your troubleshooting skills
Rating: (0)
SQL Server Magazine
InstantDoc ID #93143
My special area of interest and focus within the huge product that is SQL Server is the product's internals. I want to know how SQL Server does what it does, and I want to be able to use that knowledge to get SQL Server to do things better and faster. Furthermore,I want to know all this without having to read the source code. Knowing about SQL Server internals has given me this knowledge, which for the past seven years I've shared with readers to help you improve your own SQL Server installations.

...This article is for paid Professional Members only.

Already a Professional Member? Please log in now:

NOT A PROFESSIONAL MEMBER? YOU CHOOSE:

Professional Membership

Monthly

Annual

VIP Membership

Monthly

Annual

Add a Comment

I like the approach and style to problems/issues/subject. I have been a DBA for 10 years now and I love to know the internals,understand and apply them to resolve the issues.

DHARMA9/17/2007 8:37:37 PM


Hi Alex

I'm sorry for the delay in responding.

It turns out that slight differences in your configuration might make SQL Server's optimizer choose different plans for these queries. I tested it on several different instances to confirm the behavior I was seeing, but apparently your machine was different. It's also possible that you have performed other testing in the Northwind database to change the values in the Order Details table.

Explaining the reason for the possible difference in behavior of these two queries was actually not the purpose of this article. In the article I was just pointing out that understanding this behavior would be one of the benefits of understanding how SQL Server really works.

I have written numerous articles on indexes, and the difference between clustered and nonclustered indexes, and what kinds of queries each is good for. I invite you to search the archives of SQL Server Magazine for my past articles on the use and behavior of indexes to get many more details.

However, the short answer is that nonclustered indexes are only useful if the optimizer determines that only a few rows will satisfy the query. One of these queries returns more rows than the other, and the optimizer determine that one of the queries returned to many rows to use the nonclustered index effectively and decided it would be more efficient to just scan the table.

Thanks for writing!
Kalen Delaney

KALEN1/12/2007 11:46:08 AM


Hi Kalen,

Thanks for your advice about us learning about internals.

I ran these two queries:

SELECT * FROM [Order Details]
WHERE ProductID in ( 9, -9)

SELECT * FROM dbo.[Order Details]
WHERE ProductID in ( 5, -5)

and both query plans show a nonclustered index on ProductID being used to find the relevant rows.

I don't see why one would use non-clustered indexes and the other one a cluster index scan.

Thanks for your time and your teachings.

Alex

John Alexander11/2/2006 2:29:28 PM


You must log on before posting a comment.

Are you a new visitor? Register Here
GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS