What is the effect of statistics on queries in SQL Server

Source: Internet
Author: User
Tags sql server query table name

Brief introduction

SQL Server Query Analyzer is cost-based. Typically, the Query Analyzer determines how to choose an efficient query route based on predicates, such as which index to choose. And every time the Query Analyzer looking for the path, and not every time to statistics the number of rows included in the index, the range of values, but based on certain conditions to create and update the information after the save to the database, which is called statistical information.

How to view statistics

Viewing SQL Server statistics is simple, using the following directives:

DBCC show_statistics (' table name ', ' Index name ')

The resulting results are shown in Figure 1.

Figure 1. Statistical information

How statistical information affects queries

Let's use a simple example to see how the statistics affect the Query Analyzer. I set up a test table with two int values of columns, where ID is self increment, nonclustered index is set up on ref, insert 100 data, from 1 to 100, and then insert 9,900 data equal to 100. The statistics in Figure 1 are statistical information for the sample data.

At this point, where I use the ref value as the query condition, but given the different values, we can see that the Query Analyzer has made a different choice based on the statistics, as shown in Figure 2.

Figure 2. According to different predicates, the query optimizer makes different choices

Related Article

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.