Indexing

Indexed view execution plan

How to Make Leading Wildcard Searches Fast

Computed columns
11 Comments
99.9% of you are never gonna need this. But let’s say you need to run queries with leading (not trailing) wildcards, like this search for all the different national versions of the Encabulator, each of which has different prefixes depending on which government it’s being sold to: Transact-SQL SELECT * FROM dbo.Parts WHERE PartNumber LIKE…
Read More
You probably don't wanna hire these two.

Your SQL Server Needs Spring Cleaning, Too.

Indexing
5 Comments
First things first: pick up that filthy keyboard, take it over to the trash can, turn it upside down, and give it a good, vigorous shake. Next, go to your biggest SQL Server and run: Transact-SQL EXEC sp_BlitzIndex @GetAllDatabases = 1, @Mode = 2, @SortOrder = 'rows'; 12 EXEC sp_BlitzIndex @GetAllDatabases = 1,  @Mode = 2,…
Read More
What If You Need to Index a Lot of Duplicate Data?

What If You Need to Index a Lot of Duplicate Data?

Indexing
18 Comments
Let’s get a little nerdy and look at database internals. Create two tables: one with a million unique values, and one with a million identical values: Transact-SQL DROP TABLE IF EXISTS dbo.AllTheSame; DROP TABLE IF EXISTS dbo.AllDifferent; CREATE TABLE dbo.AllTheSame (Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, TimeItHappened DATETIME2 INDEX IX_TimeItHappened); CREATE TABLE dbo.AllDifferent (Id INT…
Read More

How Many Indexes Is Too Many?

Indexing
4 Comments
Let’s start with the Stack Overflow database (any size will work), drop all the indexes on the Users table, and run a delete: Transact-SQL SET STATISTICS IO ON; GO BEGIN TRAN DELETE dbo.Users WHERE DisplayName = N'Brent Ozar'; 1234 SET STATISTICS IO ON;GOBEGIN TRANDELETE dbo.Users WHERE DisplayName = N'Brent Ozar'; I’m using SET STATISTICS IO…
Read More

What’s Faster: IN or OR? Columnstore Edition

Pinal Dave recently ignited a storm of controversy when he quizzed readers about which one of these would be faster on AdventureWorks2019: SELECT * FROM Sales.SalesOrderHeader WHERE CustomerID IN (1, 2, 3); SELECT * FROM Sales.SalesOrderHeader WHERE CustomerID = 1 OR CustomerID = 2 OR CustomerID = 3; 123456789 SELECT *FROM Sales.SalesOrderHeaderWHERE CustomerID IN (1,…
Read More

When Should You Use DESC in Indexes?

Indexing
8 Comments
The short answer is that if your query orders columns by a mix of ascending and descending order, back to back, then the index usually needs to match that same alternating order. Now, for the long answer. When you create indexes, you can either create them in ascending order – which is the default: CREATE INDEX…
Read More

New Year’s Resolution: Lose Weight in Your Database

Indexing
14 Comments
Made a New Year’s resolution to lose weight this year? Forget taking it off your waistline – take it off your database instead with: sp_BlitzIndex @GetAllDatabases = 1, @Mode = 2, @SortOrder = ‘size’ Or: sp_BlitzIndex @GetAllDatabases = 1, @Mode = 2, @SortOrder = ‘rows’ This produces an inventory of all the indexes sorted by…
Read More
Fundamentals of Columnstore

Lock Escalation Sucks on Columnstore Indexes.

Columnstore Indexes
1 Comment
If you’ve got a regular rowstore table and you need to modify thousands of rows, you can use the fast ordered delete technique to delete rows in batches without hitting the lock escalation threshold. That’s great for rowstore indexes, but…columnstore indexes are different. To demo this technique, I’m going to use the setup from my…
Read More