It's very easy to get in the habit of imagining the way that a query should work based on the Logical Order of query processing – the idea that the FROM clause gets evaluated first, followed by the WHERE clause, GROUP BY, and so on – finally ending with whatever is in the SELECT clause. We even get in the habit of creating indexes that focus on the WHERE clause, and this is mostly right.
But it's only mostly right, and it will often depend on statistics.
There are other situations where statistics have to play a major part in choosing the right plan, of course. In fact, almost every query you ever run will use statistics to work out the best plan. What I'm going to show you in this post is an example of how the statistics end up being incredibly vital in choosing the right plan. It also helps demonstrate an important feature of the way that Scans work, and how to read execution plans.
I'm going to use AdventureWorks2012 for this example. I'm going to ask for the cheapest product according to the first letter of the product name. This kind of query:
1 2 3 |
SELECT MIN(ListPrice) FROM Production.Product WHERE Name LIKE 'H%'; |
Don't run it yet. I want to ask you how you'd solve it on paper.
Would you prefer I give you a list of the products sorted by name, or would you prefer I give you that list sorted by price?
If you want the 'sorted by name' option, then you'll have to look through all the products that start with H, and work out which is the cheapest (notice that my predicate is not an equality predicate – if I knew what the name had to be exactly, then I could have an index which ordered by name and then price, and very quickly find the cheapest with that name). This approach could be good if you don't have many products starting with that particular letter. But if you have lots, then finding them all and then looking for the cheapest of them could feel like too much work. Funnily enough, this is the way that most people would imagine this query being run – applying the WHERE clause first, and applying the aggregate function after that.
On the other hand, if you have lots of products with that particular letter, you might be better off with your list sorted by price, looking through for the first product that starts with the right letter.
Let me explain this algorithm a little more.
If you're at a restaurant and are strapped for cash, you might want to see what the cheapest thing is. You'd pick the "sorted by price" menu, and go to the first item. But then if you saw it had peanut in, and you have an allergy, then you'd skip it and go to the next one. You wouldn't expect to have to look far to find one that doesn't have peanut, and because you've got the "sorted by price" menu, you have the cheapest one that satisfies your condition after looking through just a few records.
It's clearly not the same algorithm as finding all the things that satisfy the condition first, but it's just as valid. If you're only going to have to look through a handful of products before you find one that starts with the right letter, then great! But what if there are none? You'd end up having to look through the whole list before you realised.
The Query Optimizer faces the same dilemma, but luckily it might have statistics, so it should be able to know which will suit better.
Let's create the two indexes – one sorted by Name, one sorted by Price. Both will include the other column, so that the query will only need one of them.
1 2 |
CREATE INDEX ixNamePrice ON Production.Product (Name) INCLUDE (ListPrice); CREATE INDEX ixPriceName ON Production.Product (ListPrice) INCLUDE (Name); |
Now let's consider two queries. Both queries give the same result – $0.00. But that's not important, I'm only interested in how they run.
1 2 3 4 5 6 7 |
SELECT MIN(ListPrice) FROM Production.Product WHERE Name LIKE 'I%'; SELECT MIN(ListPrice) FROM Production.Product WHERE Name LIKE 'H%'; |
The two queries are almost identical, but they run quite differently.
Ok, they're fairly similar – they both use a Stream Aggregate operator, for example. And they have similar cost. But significantly, one is performing a Seek, while the other is doing a Scan. Different indexes, but nevertheless a Scan and a Seek.
People will tell you that Scans are bad and Seeks are good, but it's not necessarily the case. Here, we see that the Scan plan is no more expensive than the Seek plan – it's just different. We should consider why.
Those two indexes are the two different stories that I described earlier. There are very few products that start with the letter 'I', and quite a number than start with 'H', and so the Query Optimizer has chosen differently.
There are exactly 10 products that start with I. From a total of 504. That's less than 2% of the products.
There are 91 products that start with H. That's 18%. You might not have expected it to be that high, but that's okay – if SQL has been maintaining statistics for you on this, it hopefully won't be as surprised as you.
18% – nearly 1 in 5. So by the time you've looked at, oh, a dozen records, you will have almost certainly found one that starts with an H. (Actually, the chance of NOT finding one in the first 12 would be power(.82, 12), which is 0.09. That's just 9%.) If I do a bit of digging into the internals, I can discover that the pages in my index typically have over a hundred records on them each. The chance of not finding a product that starts with an H on that first page – you'd need lottery-scale luck (1 in 444 million).
On the other hand, the cost of finding the cheapest value from 91 records is a lot more expensive than finding the cheapest from just 10. And getting all 10 records should be a small number of reads too.
But a Scan! Really? It has to look through the whole table, right?
No. That's not how it works.
You see, execution plans go from left to right. If you start reading these plans from the right, you'll start thinking that the whole index has been scanned, when it's simply not the case. That Top operator asks for a single row from the index, and that's all it provides. Once that row has been found, the Scan operation stops.
For this information, I don't even need to pull up the Properties window for the Scan (but I would recommend you get in the habit of doing that). No – this is all available in the Tool Tip. Look at the number of "Actual number of rows" – it's just one.
A predicate is applied – it looks through the index for rows that start with H – but it's doing this in Order (see Ordered = True), and it's stopping after the first row is found. Remember I mentioned that there are actually 91 rows that satisfy the predicate? The Scan doesn't care – it only needs one and it stops right then.
You might figure this is because we are using MIN. What if we needed the MAX though? Well, that's just the same, except that the Direction of the Scan is BACKWARD (you'll need F4 for that one).
MIN goes forward, because it's most interested in the 'smallest' ones, MAX will go backward because it wants the 'largest'. (And as you'd probably expect, if you'd created your index to be descending, then it would be reversed.)
But again – being able to tell which is the better algorithm depends entirely on your statistics being known.
I see so many systems have bad statistics for one reason or another, and typically because the data most frequently queried is the newest data, and that makes up such a small percentage of the table. The statistics will think that there is almost no data for 'today', as they probably haven't been updated since at least some number of hours ago.
When you look at how a query is running, always have a think about you'd solve it on paper, and remember that you might actually have a better (or worse) picture of the statistics than what the Query Optimizer has.
And remember that a Scan is not necessarily bad. I might do another post on that soon as well.