2.28 sql统计

2.28.1 介绍

  • 指标:统计dble中的事务、后端节点执行sql的(CRUD)次数、耗时、以及返回的行数(或影响行数)
  • 维度:业务端下发的sql、dble内部下发至后端节点的sql
  • 性能:
    • 单独开启-DsamplingRate=100(默认),性能损耗:0%~15%(根据1000w的数据量进行单点select压测得出)。
    • 开启所有(-DenableStatisticAnalysis=1-DenableStatisticAnalysis=1-DsamplingRate=100),性能损耗:0%~30%(根据1000w的数据量进行单点select压测得出)。
    • 在不同场景下,比如表的分片数越多、返回结果集行数越多等场景,都会加大性能损耗。
    • 注意,在开启-DenableStatisticAnalysis=1使用读写分离用户比分表分库用户压测结果看,损耗更大;因为读写分离直接将sql透穿下发后侧,而show @@sql.sum.table中是对table聚合统计,需要提取sql中table,而提取过程中是个比较耗时的操作,因此性能损耗会增加。
  • 可视化建议:将数据吐给类似prometheus的第三方监控工具,这样比直接使用dble统计表格更加直观

2.28.2 bootstrap.cnf中sql统计的相关配置

#  开启statistic的开关,默认关闭;0-关闭,1-开启
#-DenableStatistic=0

#  开启show @@sql.sum.user、show @@sql.sum.table、show @@sql.condition聚合统计,默认开启;0-关闭,1-开启
#-DenableStatisticAnalysis=0

#  统计表的大小,默认1024
#-DassociateTablesByEntryByUserTableSize=1024
#-DfrontendByBackendByEntryByUserTableSize=1024
#-DtableByUserByEntryTableSize=1024

#  内部实现机制用到的队列大小,值必须为2的次方,默认4096
#-DstatisticQueueSize=4096

# 采样率,默认为100即全采样,采样率是[0,100]之间的整数,单位是 %。
#-DsamplingRate=100

# sql_log 表格大小
#-DsqlLogTableSize=1024

2.28.3 管理端命令

2.28.3.1 show @@statistic

查询statistic的开关、表格大小

show @@statistic;
+-----------------------------------------+-------+
| NAME                                    | VALUE |
+-----------------------------------------+-------+
| statistic                               | OFF   |
| statisticAnalysis                       | OFF   |
| associateTablesByEntryByUserTableSize   | 1024  |
| frontendByBackendByEntryByUserTableSize | 1024  |
| tableByUserByEntryTableSize             | 1024  |
| samplingRate                            | 0     |
| sqlLogTableSize                         | 1024  |
| queueMonitor                      | monitoring  |
+-----------------------------------------+-------+
6 rows in set (0.01 sec)
2.28.3.2 disable @@statistic

关闭sql全量统计

disable @@statistic;
Query OK, 1 row affected (0.01 sec)
2.28.3.3 enable @@statistic

开启sql全量统计

enable @@statistic;
Query OK, 1 row affected (4.26 sec)
2.28.3.4 reload @@statistic_table_size = ? [where table='?' | where table in (dble_information.tableA,...)]

重置统计表的大小

reload @@statistic_table_size = 90;
Query OK, 1 row affected (0.02 sec)

reload @@statistic_table_size = 90 where table = 'sql_statistic_by_table_by_user_by_entry';
Query OK, 1 row affected (0.02 sec)

reload @@statistic_table_size = 90 where table in(sql_statistic_by_table_by_user_by_entry,sql_statistic_by_associate_tables_by_entry_by_user);
Query OK, 1 row affected (0.02 sec)

reload @@statistic_table_size = 90 where table = 'sql_log';
Query OK, 1 row affected (0.02 sec)
2.28.3.5 reload @@samplingRate=?

设置采样统计率(等于0表示关闭采样统计)

reload @@samplingRate=90;
Query OK, 1 row affected (0.01 sec)
2.28.3.2 disable @@statisticAnalysis

关闭show @@sql.sum.user、show @@sql.sum.table、show @@sql.condition聚合统计

disable @@statisticAnalysis;
Query OK, 1 row affected (0.01 sec)
2.28.3.3 enable @@statisticAnalysis

开启show @@sql.sum.user、show @@sql.sum.table、show @@sql.condition聚合统计

enable @@statisticAnalysis;
Query OK, 1 row affected (4.26 sec)

2.28.4 管理端统计表

采样统计:
sql_log sql_log_by_digest_by_entry_by_user (sql_log表的视图)
sql_log_by_tx_by_entry_by_user (sql_log表的视图)
sql_log_by_tx_digest_by_entry_by_user (sql_log表的视图)

全量统计:
sql_statistic_by_frontend_by_backend_by_entry_by_user
sql_statistic_by_table_by_user_by_entry
sql_statistic_by_associate_tables_by_entry_by_user

以上表(非视图)都支持truncate命令

2.28.5 统计的规则

以业务端执行的事务(非事务查询算单语句事务)为单位同步将收集的数据流入统计表中.

sharding:

  • 由dble层面解析表或数据库不存在等报错sql,一律不参与统计
  • explain、explain2语句不参与统计
  • exit不参与统计

rwsplit:

  • sql报1064错误码,不参与统计
  • 执行multi-query(指一次执行多个sql,mysql client可使用delimiter关键字实现),multi-query将会直接透传至后端节点,这里会被视作为事务级sql(如commit),参与统计

其他: 在 enableStatistic=0、statisticAnalysis=0、samplingRate=0的情况下,动态开启任意其中一个,会清理所有缓存的sql统计数据(sql_log、sql_statistic_by_frontend_by_xxxx表中的数据)

2.28.6 统计队列使用率的观测手段

2.28.6.1 start @@statistic_queue_monitor [observeTime = ? [and intervalTime = ?]]

开始观测,同时可设置观测总时长observeTime和采样间隔intervalTime(单位:s,m/min,h)

start @@statistic_queue_monitor; -- 使用默认值observeTime为1min,intervalTime为5s
start @@statistic_queue_monitor observeTime = 2min; -- observeTime为2min,intervalTime使用默认值5s
start @@statistic_queue_monitor observeTime = 2min and intervalTime = 10s; -- observeTime为2min,intervalTime为10s
2.28.6.2 stop @@statistic_queue_monitor"

停止观测

stop @@statistic_queue_monitor";
2.28.6.3 show @@statistic_queue.usage

查看队列的使用率情况列表(观测期间,每次查询结果递增)

show @@statistic_queue.usage;
+---------------------+-------+
| TIME                | USAGE |
+---------------------+-------+
| 2021-05-31 16:33:30 | 0.00% |
| 2021-05-31 16:33:35 | 0.00% |
| 2021-05-31 16:33:40 | 0.00% |
+---------------------+-------+
3 rows in set (0.01 sec)

TIME:采样时间点
USAGE:使用率
2.28.6.4 drop @@statistic_queue.usage

清空使用率情况列表

drop @@statistic_queue.usage;
2.28.6.5 其他补充

1、统计队列在被观测情况下(show @@statistic中的queueMonitor对应值为monitoring),执行关闭所有统计功能后(statistic为OFF且samplingRate为0)时,则观测会被中断.
2、在未开启任意统计时,执行start @@statistic_queue_monitor报错. 3、每次执行start @@statistic_queue_monitor,都会先清空使用率情况列表.
4、使用率情况列表中的数据以软引用(SoftReference)方式作为缓存方式;意味着:当jvm内存不足时,列表中的数据会被回收(现象:列表的数据量变少).
5、查看统计队列大小(statisticQueueSize),管理端中执行select * from dble_variables where variable_name='statisticQueueSize'.
6、统计队列大小(statisticQueueSize)值不支持动态改动;在bootstrap.cnf中调整其值后,需要重启dble才能生效.

results matching ""

    No results matching ""