当前位置: 首页 > 编程笔记 >

分析Mysql表读写、索引等操作的sql语句效率优化问题

仲涵亮
2023-03-14
本文向大家介绍分析Mysql表读写、索引等操作的sql语句效率优化问题,包括了分析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_instance
ORDER 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_instance
ORDER 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_latency
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL
ORDER 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_usage
WHERE index_name IS NULL
  AND count_read > 0
ORDER 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 d
where d
ORDER BY SUM_TIMER_WAIT DESC
limit 20;

掌握这些sql,你能轻松知道你的库那些表存在问题,然后考虑怎么去优化。   

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对小牛知识库的支持。如果你想了解更多相关内容请查看下面相关链接

 类似资料:
  • 本文向大家介绍SQL Server中的SQL语句优化与效率问题,包括了SQL Server中的SQL语句优化与效率问题的使用技巧和注意事项,需要的朋友参考一下 很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解。比如: select * from table1 where name='zhangsan' and tID > 1000

  • mysql数据库里面一个商品表;商品表有200多个字段,有2000万条数据,可是商品分页的时候,sql语句老是出现在慢查询日志里,请教高手如何优化? select * from d_shop order by id DESC limit 200, 220

  • 我有个商品表里面有30多万的数据,商品标题是中文,系统模糊查询的时候老是会显示慢,同时我在这个字段创建了一个普通索引;但是查询还是会慢?请教高手要如何进行优化? select * from goods_name where title_name like "%电器%"

  • 本文向大家介绍浅谈MySQL索引优化分析,包括了浅谈MySQL索引优化分析的使用技巧和注意事项,需要的朋友参考一下 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字段的意义。助你了解索引,分析索引,使用索引,从而写出更高性能的sql语句。还在等啥子?撸起袖子就是干! 案例

  • 主要内容:1 SHOW ENGINES,2 SHOW PROCESSLIST,3 SHOW STATUS LIKE ‘InnoDB_row_lock%’,4 SHOW ENGINE INNODB STATUS,5 SHOW INDEXS,6 ALTER TABLE xx ENGINE = INNODB,7 ANALYZE TABLE常见MySQL数据库优化的sql语句。 1 SHOW ENGINES 查看执行引擎以及默认引擎。 2 SHOW PROCESSLIST SHOW PROCESSLIS

  • 本文向大家介绍mysql性能优化之索引优化,包括了mysql性能优化之索引优化的使用技巧和注意事项,需要的朋友参考一下   作为免费又高效的数据库,mysql基本是首选。良好的安全连接,自带查询解析、sql语句优化,使用读写锁(细化到行)、事物隔离和多版本并发控制提高并发,完备的事务日志记录,强大的存储引擎提供高效查询(表记录可达百万级),如果是InnoDB,还可在崩溃后进行完整的恢复,优点非常多