• Create STATISTICS,UPDATE STATISTICS


    该命令在一张表或者索引了的视图上更新查询优化统计数字信息. 默认情况下, 查询优化器已经更新了必要的用来提高查询计划的统计信息; 在某些情况下, 你可以通过使用UPDATE STATISTICS 命令或者存储过程sp_updatestats 来比默认更频繁地更新统计信息来提高查询效率.

    更新统计信息能确保查询能以最新的统计信息来编译. 然而, 更新统计信息会引起查询的重新编译. 我们建议不要过于频繁地更新统计信息, 因为这里有一个在提高查询计划和用来重新编译查询的权衡. 具体的权衡要看你的应用程序而定.

    关于统计信息的更多信息, 还有何时使用UPDATE STATISTICS, 请参考Using Statistics to Improve Query Performance.

    注意: 

    何时使用UPDATE STATISTICS

    参考Using Statistics to Improve Query Performance.

    使用 sp_updatestats更新所有的统计信息

    更多信息关于如何更新所有数据库中的用户定义的表和系统内部表, 请参考存储过程sp_updatestats (Transact-SQL)

    举例, 下面的命令调用sp_updatestats 存储过程来更新数据库里所有的统计信息.

    EXEC sp_updatestats

    确定统计信息最后更新的时间
    要确定统计信息最后更新的时间, 请使用STATS_DATE 函数.

    权限

    如要运行这个命令, 你需要在表或视图上有ALTER权限.

    例子

    A. Update all statistics on a table

    The following example updates the statistics for all indexes on the SalesOrderDetail table.

    USE AdventureWorks;
    GO
    UPDATE STATISTICS Sales.SalesOrderDetail;
    GO
    
    
    B. Update the statistics for an index

    The following example updates the statistics for the AK_SalesOrderDetail_rowguid index of the SalesOrderDetail table.

    USE AdventureWorks;
    GO
    UPDATE STATISTICS Sales.SalesOrderDetail AK_SalesOrderDetail_rowguid;
    GO
    
    
    C. Update statistics by using 50 percent sampling

    The following example creates and then updates the statistics for the Name and ProductNumber columns in the Product table.

    USE AdventureWorks;
    GO
    CREATE STATISTICS Products
        ON Production.Product ([Name], ProductNumber)
        WITH SAMPLE 50 PERCENT
    -- Time passes. The UPDATE STATISTICS statement is then executed.
    UPDATE STATISTICS Production.Product(Products) 
        WITH SAMPLE 50 PERCENT;
    
    D. Update statistics by using FULLSCAN and NORECOMPUTE

    The following example updates the Products statistics in the Product table, forces a full scan of all rows in the Product table, and turns off automatic statistics for the Products statistics.

    USE AdventureWorks;
    GO
    UPDATE STATISTICS Production.Product(Products)
        WITH FULLSCAN, NORECOMPUTE;
    GO
    

    译自:

    UPDATE STATISTICS (Transact-SQL)

    http://msdn.microsoft.com/en-us/library/ms187348.aspx

    1.STATISTICS是一个表中某几个列的统计信息,如一个表是全校学生某次考试的分数,score列的类型是int,取值范围是0到100的整数,那么statistc就是每个分数分别有多少人。在一个特定的查询中,使用索引可能加快速度,也可能减慢速度,所以SQL server要事先对使用索引的效果做一个预测,预测的依据就是STATISTICS。
    2.默认情况下,表或索引更改了,统计会相应地自动更新,以保持统计是最新的。但是可以在数据库选项中关闭这个自动更新的功能,提高数据表的更新速度。但需要定期手动更新统计。因为过期的统计信息会对是否使用索引的判断带来误判。没发现SQL2000和SQL2005的统计有什么区别。
    3.index要依赖正确的STATISTICS才能发挥作用。而你的SQL2005优化工具对缺乏STATISTIC的列自动产生了创建语句。

  • 相关阅读:
    理解jquery的$.extend()、$.fn和$.fn.extend()
    前端跨域请求原理及实践
    [leetcode]Minimum Path Sum
    [leetcode]Jump Game II
    [leetcode]Merge Intervals
    [leetcode]Length of Last Word
    [leetcode]Unique Paths
    [leetcode]Text Justification
    [leetcode]Binary Tree Level Order Traversal
    [leetcode]Jump Game
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/4362354.html
Copyright © 2020-2023  润新知