Questions tagged [execution-plan]

The strategy selected by the query optimizer to process a query.

SQL is a declarative language and does not specify the actual algorithm used to resolve a query. The query plan generator can generate a number of semantically equivalent approaches to resolving a query and select the one that it estimates to be most efficient based on a heuristic.

Various transformations can be applied to a query plan for example, the order in which tables are joined or predicates applied can be changed, various join algorithms such as hash, merge or nested loops joins can be used, or internal results can be calculated and materialised for later use.

A sub-optimal plan can result in poor query performance. Most database management systems supply tools for analysing query plans and examining the query plan is a key technique for tuning a query.

1041 questions
83
votes
4 answers

Why is my query suddenly slower than it was yesterday?

[Salutations] (check one) [ ] Well trained professional, [ ] Casual reader, [ ] Hapless wanderer, I have a (check all that apply) [ ] query [ ] stored procedure [ ] database thing maybe that was running fine (if applicable) [ ] yesterday [ ] in…
67
votes
4 answers

Index Seek vs Index Scan

Looking at an execution plan of a slow running query and I noticed that some of the nodes are index seek and some of them are index scan. What is the difference between and index seek and an index scan? Which performs better? How does SQL choose…
Greg
  • 3,132
  • 3
  • 28
  • 51
47
votes
3 answers

Excessive sort memory grant

Why is this simple query granted so much memory? -- Demo table CREATE TABLE dbo.Test ( TID integer IDENTITY NOT NULL, FilterMe integer NOT NULL, SortMe integer NOT NULL, Unused nvarchar(max) NULL, CONSTRAINT PK_dbo_Test_TID …
Paul White
  • 67,511
  • 25
  • 368
  • 572
42
votes
3 answers

Parameter Sniffing vs VARIABLES vs Recompile vs OPTIMIZE FOR UNKNOWN

So we had a long running proc causing problems this morning (30 sec + run time). We decided to check to see if parameter sniffing was to blame. So, we rewrote the proc and set the incoming parameters to variables so as to defeat parameter…
RThomas
  • 3,286
  • 3
  • 21
  • 45
42
votes
3 answers

Execution Plan Basics -- Hash Match Confusion

I am starting to learn execution plans and am confused about how exactly a hash match works and why it would be used in a simple join: select Posts.Title, Users.DisplayName From Posts JOIN Users on Posts.OwnerUserId = Users.Id OPTION (MAXDOP 1) As…
Kyle Brandt
  • 2,335
  • 8
  • 29
  • 37
39
votes
2 answers

Optimising plans with XML readers

Executing the query from here to pull the deadlock events out of the default extended events session SELECT CAST ( REPLACE ( REPLACE ( XEventData.XEvent.value ('(data/value)[1]', 'varchar(max)'), '',…
Martin Smith
  • 77,689
  • 15
  • 224
  • 316
36
votes
2 answers

How (and why) does TOP impact an execution plan?

For a moderately complex query I am trying to optimize, I noticed that removing the TOP n clause changes the execution plan. I would have guessed that when a query includes TOP n the database engine would run the query ignoring the the TOP clause,…
33
votes
2 answers

Using EXCEPT in a recursive common table expression

Why does the following query return infinite rows? I would have expected the EXCEPT clause to terminate the recursion.. with cte as ( select * from ( values(1),(2),(3),(4),(5) ) v (a) ) ,r as ( select a from cte …
Tom Hunter
  • 2,069
  • 3
  • 16
  • 11
33
votes
2 answers

If a CTE is defined in a query and is never used, does it make a sound?

Do unused CTEs in queries affect performance and / or alter the generated query plan?
J.D.
  • 22,196
  • 3
  • 34
  • 78
33
votes
3 answers

Why does SQL Server use a better execution plan when I inline the variable?

I have a SQL query that I am trying to optimize: DECLARE @Id UNIQUEIDENTIFIER = 'cec094e5-b312-4b13-997a-c91a8c662962' SELECT Id, MIN(SomeTimestamp), MAX(SomeInt) FROM dbo.MyTable WHERE Id = @Id AND SomeBit = 1 GROUP BY Id MyTable has two…
Rainbolt
  • 770
  • 1
  • 9
  • 17
29
votes
1 answer

SET STATISTICS IO- worktable/workfile

I am executiong query, that produces plan: Statistics IO: Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Workfile'. Scan count 128,…
Jānis
  • 1,208
  • 2
  • 12
  • 21
28
votes
1 answer

SQL Server 2014: any explanation for inconsistent self join cardinality estimate?

Consider the following query plan in SQL Server 2014: In the query plan, a self-join ar.fId = ar.fId yields an estimate of 1 row. However, this is a logically inconsistent estimate: ar has 20,608 rows and just one distinct value of fId (accurately…
26
votes
2 answers

"Recheck Cond:" line in query plans with a bitmap index scan

This is a spin-off from comments to the previous question: Postgres 9.4.4 query takes forever Using PostgreSQL 9.4, there always seems to be a Recheck Cond: line after bitmap index scans in query plans output by EXPLAIN. Like in the EXPLAIN output…
Erwin Brandstetter
  • 146,376
  • 19
  • 347
  • 493
26
votes
1 answer

Are there any risks to granting users SQL Server SHOWPLAN permission?

I'm doing some performance tuning on a large SQL server 2008 database, and the IT group is unwilling to give SHOWPLAN permission. In the past, "Show Execution Plan" has been the most effective way to understand the performance of individual queries…
RaoulRubin
  • 949
  • 2
  • 9
  • 12
25
votes
3 answers

Index on Persisted Computed column needs key lookup to get columns in the computed expression

I have a persisted computed column on a table which is simply made up concatenated columns, e.g. CREATE TABLE dbo.T ( ID INT IDENTITY(1, 1) NOT NULL CONSTRAINT PK_T_ID PRIMARY KEY, A VARCHAR(20) NOT NULL, B VARCHAR(20) NOT NULL, …
1
2 3
69 70