Top 4 Reasons to Study Internals
If you still aren't convinced that you need to know anything about SQL Server
internals, here are my top four reasons why you should learn about them:
4. You can determine when a behavior is normal and when it's a bug.
Here's an example in SQL Server 2000's Northwind database. Look at the plan
for this query:
SELECT * FROM [Order Details]
WHERE ProductID in ( 9, -9)
The query plan shows a nonclustered index on ProductID being used to find the relevant rows.
However, this query, which returns the same rows, doesn't use the index on ProductID. Instead it uses a clustered index scan, and if you have a basic understanding of index internals, you'll know that a clustered index scan is a table scan:
SELECT * FROM dbo.[Order Details]
WHERE abs(ProductID) = 9
This query also uses a clustered index scan:
SELECT * FROM dbo.[Order Details]
WHERE ProductID in ( 5, -5)
There are two different reasons for the nonuse of the nonclustered index on
ProductID, and they're both valid. That is, both are normal and expected behavior.
Once you understand how indexes are organized and how SQL Server decides to
use them, you'll be able to expect it, too, and make the best choices in writing
queries and designing indexes.
3. You can determine whether a problem is really a bug. I've talked about
covered queries in several of my columns. A common place to see them is in simple
aggregate queries, such as this one in the Northwind database:
SELECT avg(UnitsInStock)
FROM dbo.Products
Suppose you've built a nonclustered index on UnitsInStock (because the Northwind database doesn't come with this index). SQL Server wouldn't have to touch the data pages at all.All the data needed to compute the average is in the index. So what would you do if the plan for the previous query did a table scan and didn't use the nonclustered index? I have a table in one of my databases, and a query exactly like the previous one, which returns the average on a column with a nonclustered index and doesn't use the index unless I add this WHERE clause:
WHERE Quantity >=0
This condition is actually meaningless because every row in my table has a
value for Quantity greater than or equal to 0, but I still need to add the WHERE
clause in order for SQL Server to use my nonclustered index on Quantity. This
is a bug.The more you know about what SQL Server should do, the faster you can
tell whether it's really doing the wrong thing and avoid wasting time trying
to fix it yourself.
2. You can explain your decisions with confidence. If you've been working with SQL Server for a while, you've probably learned from experience what works and what doesn't in many cases. But what do you do if your boss asks you to explain your reasoning or brings in an outside consultant who recommends a different solution. Can you explain why your ideas are valid?
A student from one of my recent classes sent me an email message after the
class was over. He said that before he took the class he'd actually suggested
to the database designers that they change their clustered index to columns
other than the primary key. They ignored him. After taking the class, he was
so sure he was correct, he told the engineers again that he thought they should
change some of the clustered indexes.The designers didn't know he had just taken
the class, but this time they took his advice. He felt he was more confident
because he knew that he could explain his decision if asked about it.When you
know what you're talking about,people listen to you.
1. You can get your own answers to questions. How can you find out everything
you've ever wanted to know about SQL Server? Not by reading this one article,
or even by reading one book. After you start developing your own expertise,
you'll find that your knowledge will grow exponentially. The more you know,
the more quickly you can find the best sources of information. You can describe
your problems more precisely when you post them in a public Help forum. And
you can quickly develop tests or write queries by using SQL Server metadata
to provide even more information about what your SQL Server system is really
doing.
End of Article
Prev. page
1
[2]
next page -->