How does SQLServer know the statistics used by the execution plan?

Source: Internet
Author: User
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,

 
  1. USE Northwind
  2. GO
  3. DBCC FREEPROCCACHE()
  4. GO
  5. DBCC TRACEON(3604, 9292, 9204)
  6. GO
  7. SELECT Employees.FirstName, COUNT(Orders.OrderID)
  8. FROM Orders
  9. INNER JOIN Employees
  10. ON Orders.EmployeeID = Employees.EmployeeID
  11. WHERE Employees.FirstName = 'Steven'
  12. GROUP BY Employees.FirstName
  13. 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,

 
  1. USE Northwind
  2. GO
  3. DBCC FREEPROCCACHE()
  4. GO
  5. DBCC TRACEON(8666)
  6. GO
  7. SELECT Employees.FirstName, COUNT(Orders.OrderID)
  8. FROM Orders
  9. INNER JOIN Employees
  10. ON Orders.EmployeeID = Employees.EmployeeID
  11. WHERE Employees.FirstName = 'Steven'
  12. GROUP BY Employees.FirstName
  13. GO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.