分析Mysql表读写、索引等操作的sql语句效率优化问题
发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,上次我们说到mysql的一些sql查询方面的优化,包括查看explain执行计划,分析索引等等。今天我们分享一些 分析mysql表读写、索引等等操作的sql语句。闲话不多说,直接上代码:反映表的读写压
千家信息网最后更新 2025年01月21日分析Mysql表读写、索引等操作的sql语句效率优化问题
上次我们说到mysql的一些sql查询方面的优化,包括查看explain执行计划,分析索引等等。今天我们分享一些 分析mysql表读写、索引等等操作的sql语句。
闲话不多说,直接上代码:
反映表的读写压力
SELECT file_name AS file, count_read, sum_number_of_bytes_read AS total_read, count_write, sum_number_of_bytes_write AS total_written, (sum_number_of_bytes_read + sum_number_of_bytes_write) AS total FROM performance_schema.file_summary_by_instanceORDER BY sum_number_of_bytes_read+ sum_number_of_bytes_write DESC;
反映文件的延迟
SELECT (file_name) AS file, count_star AS total, CONCAT(ROUND(sum_timer_wait / 3600000000000000, 2), 'h') AS total_latency, count_read, CONCAT(ROUND(sum_timer_read / 1000000000000, 2), 's') AS read_latency, count_write, CONCAT(ROUND(sum_timer_write / 3600000000000000, 2), 'h')AS write_latency FROM performance_schema.file_summary_by_instanceORDER BY sum_timer_wait DESC;
table 的读写延迟
SELECT object_schema AS table_schema, object_name AS table_name, count_star AS total, CONCAT(ROUND(sum_timer_wait / 3600000000000000, 2), 'h') as total_latency, CONCAT(ROUND((sum_timer_wait / count_star) / 1000000, 2), 'us') AS avg_latency, CONCAT(ROUND(max_timer_wait / 1000000000, 2), 'ms') AS max_latency FROM performance_schema.objects_summary_global_by_type ORDER BY sum_timer_wait DESC;
查看表操作频度
SELECT object_schema AS table_schema, object_name AS table_name, count_star AS rows_io_total, count_read AS rows_read, count_write AS rows_write, count_fetch AS rows_fetchs, count_insert AS rows_inserts, count_update AS rows_updates, count_delete AS rows_deletes, CONCAT(ROUND(sum_timer_fetch / 3600000000000000, 2), 'h') AS fetch_latency, CONCAT(ROUND(sum_timer_insert / 3600000000000000, 2), 'h') AS insert_latency, CONCAT(ROUND(sum_timer_update / 3600000000000000, 2), 'h') AS update_latency, CONCAT(ROUND(sum_timer_delete / 3600000000000000, 2), 'h') AS delete_latency FROM performance_schema.table_io_waits_summary_by_table ORDER BY sum_timer_wait DESC ;
索引状况
SELECT OBJECT_SCHEMA AS table_schema, OBJECT_NAME AS table_name, INDEX_NAME as index_name, COUNT_FETCH AS rows_fetched, CONCAT(ROUND(SUM_TIMER_FETCH / 3600000000000000, 2), 'h') AS select_latency, COUNT_INSERT AS rows_inserted, CONCAT(ROUND(SUM_TIMER_INSERT / 3600000000000000, 2), 'h') AS insert_latency, COUNT_UPDATE AS rows_updated, CONCAT(ROUND(SUM_TIMER_UPDATE / 3600000000000000, 2), 'h') AS update_latency, COUNT_DELETE AS rows_deleted, CONCAT(ROUND(SUM_TIMER_DELETE / 3600000000000000, 2), 'h')AS delete_latencyFROM performance_schema.table_io_waits_summary_by_index_usageWHERE index_name IS NOT NULLORDER BY sum_timer_wait DESC;
全表扫描情况
SELECT object_schema, object_name, count_read AS rows_full_scanned FROM performance_schema.table_io_waits_summary_by_index_usageWHERE index_name IS NULL AND count_read > 0ORDER BY count_read DESC;
没有使用的index
SELECT object_schema, object_name, index_name FROM performance_schema.table_io_waits_summary_by_index_usage WHERE index_name IS NOT NULL AND count_star = 0 AND object_schema not in ('mysql','v_monitor') AND index_name <> 'PRIMARY' ORDER BY object_schema, object_name;
糟糕的sql问题摘要
SELECT (DIGEST_TEXT) AS query, SCHEMA_NAME AS db, IF(SUM_NO_GOOD_INDEX_USED > 0 OR SUM_NO_INDEX_USED > 0, '*', '') AS full_scan, COUNT_STAR AS exec_count, SUM_ERRORS AS err_count, SUM_WARNINGS AS warn_count, (SUM_TIMER_WAIT) AS total_latency, (MAX_TIMER_WAIT) AS max_latency, (AVG_TIMER_WAIT) AS avg_latency, (SUM_LOCK_TIME) AS lock_latency, format(SUM_ROWS_SENT,0) AS rows_sent, ROUND(IFNULL(SUM_ROWS_SENT / NULLIF(COUNT_STAR, 0), 0)) AS rows_sent_avg, SUM_ROWS_EXAMINED AS rows_examined, ROUND(IFNULL(SUM_ROWS_EXAMINED / NULLIF(COUNT_STAR, 0), 0)) AS rows_examined_avg, SUM_CREATED_TMP_TABLES AS tmp_tables, SUM_CREATED_TMP_DISK_TABLES AS tmp_disk_tables, SUM_SORT_ROWS AS rows_sorted, SUM_SORT_MERGE_PASSES AS sort_merge_passes, DIGEST AS digest, FIRST_SEEN AS first_seen, LAST_SEEN as last_seen FROM performance_schema.events_statements_summary_by_digest dwhere dORDER BY SUM_TIMER_WAIT DESClimit 20;
掌握这些sql,你能轻松知道你的库那些表存在问题,然后考虑怎么去优化。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
索引
内容
问题
分析
学习
延迟
语句
糟糕
代码
价值
压力
就是
情况
摘要
文件
方面
更多
状况
篇文章
链接
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全王欣
数据库查找数据源的子句代码
奉贤区多层软件开发参考价格
海光3185服务器
gmail服务器验证账户信息
电脑服务器时间
数据库插件产品
网络技术招聘岗位
航运数据库企业
试图在数据库安全性中的作用
我国网络安全现状商务安全
全国网络安全周宣传会
网络安全趋势卡
麒麟系统怎么连接本地数据库
sqli labs 数据库
二道区智能网络技术咨询哪家好
交通银行软件开发刘雷
平面设计和计算机网络技术哪个好
南大通用数据库有多少节点
怎么删除电脑代理服务器
软件开发能卖钱吗
数据库插件产品
网络安全责令改正通知书
自测软件开发
互联网巨头是科技公司
联想服务器管理口rd650
表就是数据库数据库就是表
网络安全 主题
数据库主从分离
东明软件开发培训在线学习