性能优化统计信息SQLServer自动更新和自动创建统计信息
原文译自: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 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存储过程来实现。

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

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),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor
