sqlserver 统计信息自动更新

时间:2021-05-25 11:14:55

对于已经应用的表(非临时表)统计信息更新条件为500+20%,具体更新条件

If the statistics is defined on a regular table, it is out of date if:

  1. The table size has gone from 0 to >0 rows.

  2. The number of rows in the table when the statistics were gathered was 500 or less, and the colmodctr of the leading column of the statistics object has changed by more than 500 since then.

  3. The table had more than 500 rows when the statistics were gathered, and the colmodctr of the leading column of the statistics object has changed by more than 500 + 20% of the number of rows in the table when the statistics were gathered.

  • If the statistics object is defined on a temporary table, it is out of date as discussed above, except that there is an additional threshold for recomputation at 6 rows, with a test otherwise identical to test 2 in the previous list.

在一些较大的表中触发此更新条件的周期可能较长,因为可能影响查询的性能.在此介绍sqlserver 的一个trace flag 2371

当开启这个flag时,统计信息的更新将会根据表数据量的总体大小动态调整触发更新条件

(如1000000数据 30000就会触发更新(3%),1000000000数据1000000就会触发(0.1%)),从而有可能提高系统整体性能.

如图所示

sqlserver 统计信息自动更新

注意:由于更新统计信息本身也是有一定得资源消耗,开启后会造成统计信息更新相对频繁,所以开启前应全面评估整体成本.

      (如未因统计信息更新对全局造成明显影响则无需开启)

个人想法:关系型数据库发展至今,所有产品差别越来越小,细节更能体现一款产品的优秀程度.

             2371的特性如果能作为一个库的option设置,细化粒度(数据库根据用途可能导致访问特点不尽相同),将会使统计信息的管理更合理灵活.