Created
May 13, 2019 16:17
-
-
Save erikdarlingdata/a94095ec7e0623a81d0176d9cb731fcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE INDEX ix_whatever ON dbo.Users (Reputation, Age, CreationDate); | |
GO | |
CREATE OR ALTER PROCEDURE dbo.WORLDSTAR (@Reputation INT) | |
AS | |
SET NOCOUNT, XACT_ABORT ON | |
BEGIN | |
SET STATISTICS TIME, IO ON; | |
/*Regular Key Lookup*/ | |
SELECT TOP (1000) | |
u.* | |
FROM dbo.Users AS u | |
WHERE u.Reputation = @Reputation | |
ORDER BY u.CreationDate DESC; | |
SET STATISTICS TIME, IO OFF; | |
SET STATISTICS TIME, IO ON; | |
/*Self Join Instead*/ | |
SELECT TOP (1000) | |
u2.* | |
FROM dbo.Users AS u | |
JOIN dbo.Users AS u2 | |
ON u.Id = u2.Id | |
WHERE u.Reputation = @Reputation | |
ORDER BY u.CreationDate DESC; | |
SET STATISTICS TIME, IO OFF; | |
END | |
/*oh no parameter sniffing! the little plan is first!*/ | |
EXEC dbo.WORLDSTAR @Reputation = 2; | |
/*UH OH HERE COMES THE BIG PLAN*/ | |
EXEC dbo.WORLDSTAR @Reputation = 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment