Recently, a customer asked this interesting question about the statistics used in the execution plan generation process? To solve this problem, the customer also made some research and gave me the following two blog posts as the starting point. 1. HowtoFindtheStatisticsUsedtoCompileanExecutionPlan (sqlblog. comblogsp
Recently, a customer asked this interesting question about the statistics used in the execution plan generation process? To solve this problem, the customer also made some research and gave me the following two blog posts as the starting point. 1. How to Find the Statistics Used to Compile an Execution Plan (http://sqlblog.com/blogs/p
A customer recently asked this interesting question-what statistics are used in the execution plan generation process? To solve this problem, the customer also made some research and gave me the following two blog posts as the starting point.
1.How to Find the Statistics Used to Compile an Execution Plan(
2.Statistics used in a cached query plan(
After reading these two blog posts, I will extract the secrets and share them with you.
The first blog introduces two Trace flags, 9204 and 9292. The two Trace flags are explained as follows:
9204-print the loaded statistics
9292-print the header of the statistical information obtained from the metadata
Of course, to see this information, we also need to enable Trace Flag 3604 to display it. The following script is used as an example,
- USE Northwind
-
- GO
-
- DBCC FREEPROCCACHE()
-
- GO
-
- DBCC TRACEON(3604, 9292, 9204)
-
- GO
-
-
-
- SELECT Employees.FirstName, COUNT(Orders.OrderID)
-
- FROM Orders
-
- INNER JOIN Employees
-
- ON Orders.EmployeeID = Employees.EmployeeID
-
- WHERE Employees.FirstName = 'Steven'
-
- GROUP BY Employees.FirstName
-
- GO
The returned result is:
Stats header loaded: DbName: Northwind, ObjName: Orders, IndexId: 4, ColumnName: EmployeeID, EmptyTable: FALSE
Stats loaded: DbName: Northwind, ObjName: Orders, IndexId: 4, ColumnName: EmployeeID, EmptyTable: FALSE
Stats header loaded: DbName: Northwind, ObjName: Orders, IndexId: 5, ColumnName: EmployeeID, EmptyTable: FALSE
Stats header loaded: DbName: Northwind, ObjName: Employees, IndexId: 1, ColumnName: EmployeeID, EmptyTable: FALSE
Stats loaded: DbName: Northwind, ObjName: Employees, IndexId: 1, ColumnName: EmployeeID, EmptyTable: FALSE
Stats header loaded: DbName: Northwind, ObjName: Employees, IndexId: 4, ColumnName: FirstName, EmptyTable: FALSE
Stats loaded: DbName: Northwind, ObjName: Employees, IndexId: 4, ColumnName: FirstName, EmptyTable: FALSE
FirstName
---------------------
Steven 42
From this SELECT statement, it is not difficult to see that the query result should be the First Name of employee Steven and the number of orders owned by this employee. The table Employees and Orders are associated with the employee ID. Therefore, the statistics on the indexes created on Employees. EmployeeID and Orders. EmployeeID are used to generate the execution plan. Of course, they should also have statistical information on the field Employees. FirstName. because no index exists on this field, the system will automatically generate a statistical information here.
Note that Trace Flag 9204 and 9292 only print the statistical records when the SELECT statement is compiled for the first time. If the execution plan already exists in the cache, the records of the statistics will not be printed. It will be printed again only after dbcc freeproccache (plan_handle) is used to clear the cache plan of this statement.
The second blog post introduces another Trace Flag, 8666. This Trace Flag is explained as follows:
8666-print out the details of Showplan
This Trace Flag displays some internal information about the statistics. For example,
- USE Northwind
-
- GO
-
- DBCC FREEPROCCACHE()
-
- GO
-
- DBCC TRACEON(8666)
-
- GO
-
-
-
- SELECT Employees.FirstName, COUNT(Orders.OrderID)
-
- FROM Orders
-
- INNER JOIN Employees
-
- ON Orders.EmployeeID = Employees.EmployeeID
-
- WHERE Employees.FirstName = 'Steven'
-
- GROUP BY Employees.FirstName
-
- GO