Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Create Statistics" do in SQL Server 2005?

The Database Tuning Advisor is recommending that I create a bunch of statistics in my Database. I'm something of a SQL n00b, so this was the first time I'd ever come across such a creature. The entry in MSDN was a little obtuse - could someone explain what exactly this does, and why it's a good idea?

like image 527
Electrons_Ahoy Avatar asked Sep 24 '08 16:09

Electrons_Ahoy


People also ask

What does create statistics do SQL Server?

Auto Create Statistics SQL Server automatically creates statistics on the individual columns for the query predicate to improve the cardinality estimate and prepare a cost-effective execution plan.

What does SQL Server update statistics do?

Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently because there is a performance tradeoff between improving query plans and the time it takes to recompile queries.

Why are statistics important on a table SQL?

SQL Server statistics are essential for the query optimizer to prepare an optimized and cost-effective execution plan. These statistics provide distribution of column values to query optimizer and it helps SQL server to estimate the number of rows.

Can we create statistics in SQL Server?

You can create query optimization statistics on one or more columns of a table or indexed view in SQL Server by using SQL Server Management Studio or Transact-SQL.


1 Answers

Cost Based Query Optimisation is a technique that uses histograms and row counts to heuristically estimate the cost of executing a query plan. When you submit a query to SQL Server, it evaluates it and generates a series of Query Plans for which it uses heuristics to estimate the costs. It then selects the cheapest query plan.

Statistics are used by the query optimiser to calculate the cost of the query plans. If the statistics are missing or out of date it does not have correct data to estimate the plan. In this case it can generate query plans that are moderately or highly sub-optimal.

SQL Server will (under most circumstances) generate statistics on most tables and indexes automatically but you can supplement these or force refreshes. The query tuning wizard has presumably found some missing statistics or identified joins within the query that statistics should be added for.

like image 194
ConcernedOfTunbridgeWells Avatar answered Sep 21 '22 11:09

ConcernedOfTunbridgeWells