Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Number of Executions in Execution plan

Below is the simple execution plan for below query.

Query:

SELECT TOP (25) orderid, custid, empid, shipperid, orderdate, filler
FROM dbo.Orders
ORDER BY orderid;

Execution plan: enter image description here

My question is how to make sense of number of executions

Below is the number of executions for all operators

Nested loops:
Estimated number of executions: 1
Actual Number of executions: 1

Index Scan:
Estimated number of executions: 1
Actual Number of executions: 1

Key lookup:
Estimated number of executions: 25
Actual Number of executions: 25

My questions are

  1. Why is nested loop showing only 1 execution count?

  2. Index scan also shows only one execution count, but it gets 25 rows in one execution. Are these rows stored in Rowset cache or some cache? Will nested loop take one row from cache and invoke key lookup 25 times for each row?

Below is the explanation from Itzik Ben-Gan

For example, how does the Index Scan iterator know to stop after 25 rows if the Top iterator that tells it to stop appears later in the plan? The answer is that the internal API calls start with the root node (SELECT iterator in our case). This root node invokes the Top iterator. The Top iterator invokes a method 25 times that asks for a row from the Nested Loops iterator. In turn, the Nested Loops iterator invokes a method 25 times that asks for a row from the Index Scan iterator. Hence, the Index Scan iterator doesn't proceed beyond the 25 first rows that it scans. In short, although it's usually more intuitive to follow data flow order to interpret the plan

But why the execution count only shows 1. Please let me know if I am missing anything. Attached is the execution plan

Update (January 2017) :

I have asked some what similar question on stack overflow. Please see this answer for more details on same..

https://dba.stackexchange.com/questions/134172/set-statistics-i-o-for-nested-loops

like image 367
TheGameiswar Avatar asked Oct 19 '22 06:10

TheGameiswar


1 Answers

  1. The nested loop is only executed once. It does 25 key lookups, but that's not the count how many times the nested loop operation itself is being executed.

  2. The 25 rows come from the index lookup and it passes them along to the key lookup that fetches the missing data for the row, and then passes the row to the top operator. When the top operator gets 25 rows it tells the others to stop. I didn't really understand the cache question. Of course the data is fetched into buffer pool, but it gets passed from one operator to the next once it can be.

like image 68
James Z Avatar answered Nov 29 '22 17:11

James Z