search
HomeDatabaseMysql Tutorial性能优化统计信息SQLServer自动更新和自动创建统计信息

性能优化统计信息SQLServer自动更新和自动创建统计信息

Jun 07, 2016 pm 03:19 PM
sqlserveroptimizationinformationcreateperformancestatisticsautomatic

原文译自:http://www.mssqltips.com/sqlservertip/2766/sql-server-auto-update-and-auto-create-statistics-options/?utm_source=dailynewsletterutm_medium=emailutm_content=headlineutm_campaign=2012913 统计信息是如何提高SQLServer查询性能的?统计

原文译自:http://www.mssqltips.com/sqlservertip/2766/sql-server-auto-update-and-auto-create-statistics-options/?utm_source=dailynewsletter&utm_medium=email&utm_content=headline&utm_campaign=2012913

 

        统计信息是如何提高SQLServer查询性能的?统计直方图用作在查询执行计划中查询优化器的选择依据。如果一个查询谓词包含统计信息的列,那么查询优化器不需要预测该查询中影响行数,因此,查询优化器有足够的信息去创建执行计划。SQLServer创建执行计划有一下几种不同的方式:

  • 统计信息会在每个新创建的索引中自动创建统计信息。
  • 如果数据库中AUTO_CREATE_STATISTICS被设置为ON,SQLServer将会自动对查询中用到的,且没有索引的列自动创建统计信息。

 

AUTO_CREATE_STATISTICS选项:

当把该选项设为ON时,查询优化器会对在谓词中使用的到列,如果这些列的统计信息不可用,则会单独对每列创建统计信息。这些统计信息对创建一个查询计划非常必要。它们创建于那些现有统计对象中不存在直方图的列上,名字包括列名和对象ID的十六进制格式:_WA_Sys__。这些统计信息用于查询优化器决定使用何种优化后的执行计划。

可以通过以下语句启用自动统计信息创建功能:

ALTER  DATABASE[你的库名]

SET AUTO_CREATE_STATISTICS ON

 

Auto Update Statistics选项:

         统计信息会在查询编译或者执行缓存执行计划前被检查。当在以下情况下,统计信息会被认为过期:

1、  在一个空表中有数据的改动。

2、  当统计信息创建时,表的行数只有500或以下,且后来统计对象中的引导列的更改次数大于500.

3、  当表的统计信息收集时,超过了500行,且统计对象的引导列后来更改次数超过500+表总行数的20%时。

4、  在Tempdb中的表,少于6行且最少有6行被更改。

更多的信息可以查看MSDN

 

可以使用一下语句来开启自动更新统计信息:

ALTER  DATABASE[你的库名]

SET AUTO_UPDATE_STATISTICS ON

 

过时的统计信息会引起大量的性能问题,所以建议开启自动更新。它的默认设置是ON。没有更新统计信息常见的影响是选择了次优的执行计划,然后性能下降。有时候,过期的统计信息可能比没有统计信息更加糟糕。

使用以下语句来开启异步更新统计信息:

ALTER  DATABASE[你的库名]

SET AUTO_UPDATE_STATISTICS_ASYNC ON

如果开启了这个选项,查询优化器将先执行一次查询,然后更新过期的统计信息。当你把这个选项设为OFF时,查询优化器将在编译查询之前更新过期统计信息。这个选项在OLTP环境下很有用,但在数据仓库中有负面影响。

 

如何关闭SQLServer自动更新统计信息的选项?

         在非常特殊的情况下,你不得不禁用这个有用的特性,可以使用以下方式关闭:

1、  使用sp_autostats来在表、索引或者统计对象上显式并更改自动更新统计信息选项。

2、  在表级别中,可以使用NORECOMPUTEoption of the UPDATE STATISTICS命令。

3、  你也可以在CREATESTATISTICS命令中使用NORECOMPUTE选项,但之后需要删除并重建统计信息。

4、  在CREATE INDEX命令中使用STATISTICS_NORECOMPUTE。

5、  在数据库级别,可以使用以下命令来禁用:

ALTER DATABASE[你的库名]

SET AUTO_UPDATE_STATISTICS OFF

当使用数据库级别的禁用时,表、索引或者统计对象的设置将全部失效。

 

何时创建统计信息?

         其中一个答案是当使用数据库引擎优化顾问(DTA)时建议创建。另外一个情况是当你查看执行计划是,出现丢失统计信息的警告(missing statistics warnings),如下图的黄色三角叹号:

 性能优化统计信息SQLServer自动更新和自动创建统计信息

可以使用SQLServer Profiler 去监控丢失列统计信息的事件,你也可以考虑当你的查询从子集或者查询谓词中包含关联列的那些列上创建统计信息。

创建统计信息的语句如下:

--Create statistics on all rows

CREATE STATISTICSstatistics_name   ONYourDBName.YourSchema.YourTable(YourColumn1,YourColumn2)  

WITH FULLSCAN

 --Create statistics using a random 10 percent sampling rate

CREATE STATISTICSstatistics_name   ONYourDBName.YourSchema.YourTable(YourColumn1,YourColumn2)   

WITH SAMPLE 10PERCENT

 

何时更新统计信息?

         如果你的查询执行得很慢,那么是时候更新统计信息了。并且建议当你插入大量数据到升序或者降序的列时,更新统计信息,因为在这种情况下,统计信息直方图将不包含新插入的值,同时,强烈建议在除索引维护(当你重建、整理碎片或者重组索引时,数据分布不会改变)外的维护工作之后更新统计信息。

         如果数据库的数据更改频繁,建议最低限度每天更新一次统计信息。一般来说,在数据仓库中,可以降低更新统计信息的频率,当更新时,通常建议执行sp_updatestats存储过程来实现。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools