My favorite time of year to fish is fall. The fish are bulking up for the winter, so they are fatter than usual. They also will often binge feed, providing some great fishing. However, fall can be rather uncomfortable here in Wisconsin. We get freezing temperatures, blustery days, and snow or rainfail for extended periods in the fall. Just this last October I was fishing cold conditions, with a pretty strong wind and light snow that turned heavy every so often. I remember looking around, seeing no one else on the lake, and thinking to myself “This is living!”. I’ve come to enjoy the sting of the weather because I associate it with the joy of fishing and being in the outdoors.
One thing that can really sting database end users is a slow query. Web pages that take seconds to load instead of miliseconds and BI queries that take minutes instead of seconds can make people awfully grumpy toward the DBA. One way to make sure you can aviod this sting is to ensure your queries are sargable. Sarbable is a made up term that comes from the expression Search ARGument ABLE. Basically, you want to make sure any fields that are referenced in the WHERE, ORDER BY, GROUP BY, and HAVING clauses are able to use an index. The main way to do this is to avoid functions in your predicate. Let’s take a look at a quick example. I’ll create a table with a clustered primary key and a some nonclustered indexes and fill it with some data:
--Create table with primary key clustered CREATE TABLE dbo.BaitPurchases (ID INT IDENTITY(1,1), Baitname VARCHAR(255) NOT NULL, Purchasedate DATETIME2 NULL, Price MONEY NULL PRIMARY KEY CLUSTERED ([ID] ASC) ) ON [PRIMARY] GO --Create nonclustered index on PurchaseDate CREATE NONCLUSTERED INDEX IX_BaitPurchases_PurchaseDate ON [dbo].BaitPurchases ( Purchasedate ASC )INCLUDE (Baitname) GO --Create nonclustered index on Baitname CREATE NONCLUSTERED INDEX IX_BaitPurchases_Baitname ON [dbo].BaitPurchases ( Baitname ASC )INCLUDE (Price) GO --Create nonclustered index on Price CREATE NONCLUSTERED INDEX IX_BaitPurchases_Price ON [dbo].BaitPurchases ( Price ASC )INCLUDE (Baitname) GO --Throw in some data INSERT INTO dbo.BaitPurchases ( Baitname, Purchasedate, Price ) VALUES ( 'Bulldawg','2010-04-29',15.99), ( 'Double Cowgirl','2010-04-30',16.99), ( 'Shallow Raider','2010-05-29',15.49), ( 'Depth Raider','2010-05-30',18.99), ( 'Reef Runner','2011-05-29',14.99), ( 'Mepps Muskie Killer','2011-04-29',12.49), ( 'Suick Thriller','2011-04-30',16.09), ( 'Softtail Phantom','2011-05-30',26.99), ( 'Shallow Invader','2012-04-29',18.29), ( 'Medussa','2012-04-30',17.99), ( 'Grandma','2012-05-29',15.39), ( 'Jake','2012-05-30',15.39), ( 'Believer','2013-04-29',16.59), ( 'Harasser','2013-04-30',13.49), ( 'Doubledawg','2013-05-29',17.99), ( 'Buchertail 700','2013-05-30',13.89), ( 'Hawg Wobbler','2014-04-29',21.99), ( 'Pacemaker','2014-04-30',20.99), ( 'Top Raider','2014-05-29',18.50), ( 'Swim Whizz','2014-05-30',16.98), ( 'Creek Chub Pike Minnow','2015-01-12',15.00)
Now we’ll look for all items that were purchased in 2012, attempting to utilize the nonclustered index that was created on the Purchasedate field:
SELECT Baitname, Purchasedate FROM dbo.BaitPurchases WHERE Purchasedate BETWEEN '2013-01-01' AND '2013-12-31 23:59:59.999'
You can see from the execution plan that the database engine is doing an index seek on our nonclustered index, just as we were expecting. However, your junior DBA comes across this code and thinks it’ll be much more readable if he re-writes it with a function to look at the purchase year:
SELECT Baitname , Purchasedate FROM dbo.BaitPurchases WHERE YEAR(Purchasedate) = 2013
This does look more readable to me, but if we take a look at the execution plan, we find a very bad negative consequence of this change:
We are now doing a nonclustered index scan, meaning the entirety of the index was read. This doesn’t make a big difference in our current tiny table, but with a table with millions of rows you could be seeing the query execution time going from less than a second to minutes.
Let’s take a look at one more common filtering technique that causes index scans instead of seeks. Let’s say you can’t quite remember the name of that lure, but you’re sure it started with an ‘S’. The obvious way to search for this would be:
SELECT Baitname FROM dbo.BaitPurchases WHERE Baitname LIKE 'S%'
However, now we remembered the name didn’t start with an ‘S’, but it definitely had ‘Raider’ in it somewhere. Now we change up our query like this:
SELECT Baitname FROM dbo.BaitPurchases WHERE Baitname LIKE '%Raider%'
Before, the database engine could look at the beginning of each baitname to find the results we were looking for. With the index, the engine jumped right to the section of the index that had the ‘S’s and returned the results from there. Now it has to look in every single bait name and see if it contains the string ‘Raider’. Searches like the second one are usually going to be slow, but SQL Server does provide one tool for getting that data a bit faster, and that is Full-Text indexes. I’ll discuss those in a later blog post.
So take the sting out of database queries by making sure your indexes are being used.