统计数据的重要性相信了解oracle数据库的都非常清楚,他是生成执行计划的唯一标准,所以统计数据是否准确直接影响到执行计划的正确性, 关于mysql虽然没有oracle那么多维度的统计数据(比如直方图)但是还是有的,mysql 5.6后分为2种统计数据。
1、永久化的统计数据
默认的5.6.6后使用时这种方式,影响他的参数有
innodb_stats_persistent_sample_pages 每次采样的块数,默认为20
innodb_stats_auto_recalc 是否开启自动收集,默认为修改量超过10%的数据
innodb_stats_persistent 默认为on,是否使用永久化的统计数据
永久化的统计数据存储在innodb_index_stats和innodb_table_stats中,官方文档说统计数据并非实时的,也就是收集统计数据会滞后几分钟,如果想要及时的更新统计
数据需要手动执行analyze table(in some cases, statistics
recalculation may be delayed by a few seconds. if up-to-date statistics are
required immediately after changing significant portions of a table, run analyze
tableto initiate a synchronous (foreground) recalculation of statistics)
如果统计数据不准可能需要修改innodb_stats_persistent_sample_pages的值更大,如果analyze table过慢可以考虑减少innodb_stats_persistent_sample_pages的值更小。这两个表为innodb的表
2、非永久化的统计数
如果设置innodb_stats_persistent为off就使用这种方式的统计数据,这种方式的统计数据在数据库重启后就丢失,他的统计数据保存在statistics,tables两个表中,他们是memory引擎的表,这种方式受影响的参数
innodb_stats_persistent 设置为off
innodb_stats_transient_sample_pages 默认为8,为采样块数
innodb_stats_on_metadata 英文描述to have statistics updated when metadata statements such as show table status
or show index are run, or when accessing the information_schema.tables or
information_schema.statisticst ables, execute the statement set global
innodb_stats_on_metadata=on
当innodb_stats_persistent设置为on的时候,默认innodb_stats_on_metadata是off的,因为它会影响show table status等的速度
最后我们再来简单描述一下orcale的统计数据收集的策略,oracle 10g,11g都是通过自动化的作业来收集,默认的11g周1到周5晚上10点开始收集统计数据,周末则是早上6点开始,当然这种收集统计数据并不是全部收集,oracle会把修改数量超过10%的tables记录到mon_mods_all$内部表中,然后对这些表进行收集,这样来保证统计数据能够尽可能的描述数据的分布,同时oracle会通过谓词的使用来判定是否收集直方图, 谓词的使用记录在col_usage$内部视图中,直方图用于判定数据的倾斜,mysql在这方面视乎还没有。
