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

 更新时间:2018年12月08日 10:36:58   作者:执笔记忆的空白  
今天小编就为大家分享一篇关于分析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,你能轻松知道你的库那些表存在问题,然后考虑怎么去优化。   

总结

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

相关文章

  • mysql like查询字符串示例语句

    mysql like查询字符串示例语句

    在mysql中如果我们要模糊查询数据我们可以使用like带%%号来实现查询,下面我来简单的介绍一下关于mysql like使用方法
    2013-10-10
  • MySQL中 and or 查询的优先级分析

    MySQL中 and or 查询的优先级分析

    这个可能是容易被忽略的问题,首选我们要清楚,MySQL中,AND的执行优先级高于OR。也就是说,在没有小括号()的限制下,总是优先执行AND语句,再执行OR语句
    2021-03-03
  • MySQL主从复制原理详情

    MySQL主从复制原理详情

    这篇文章主要介绍了MySQL主从复制原理详情,MySQL 主从复制是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点,文章围绕主题展开详细的内容介绍。感兴趣的小伙伴可以参考一下
    2022-06-06
  • 解决MySQL因不能创建 PID 导致无法启动的方法

    解决MySQL因不能创建 PID 导致无法启动的方法

    这篇文章主要给大家介绍了关于解决MySQL因不能创建 PID 导致无法启动的方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • MySQL 文本文件的导入导出数据的方法

    MySQL 文本文件的导入导出数据的方法

    但有时为了更快速地插入大批量数据或交换数据,需要从文本中导入数据或导出数据到文本。下面的具体的方法大家可以参考下。多测试。
    2009-11-11
  • MYSQL Left Join优化(10秒优化到20毫秒内)

    MYSQL Left Join优化(10秒优化到20毫秒内)

    在实际开发中,相信大多数人都会用到join进行连表查询,但是有些人发现,用join好像效率很低,而且驱动表不同,执行时间也不同。那么join到底是如何执行的呢,本文就详细的介绍一下
    2021-12-12
  • MySQL ALTER命令使用详解

    MySQL ALTER命令使用详解

    这篇文章主要为大家详细介绍了MySQL ALTER命令的使用方法,简单实用,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Mysql中关于Incorrect string value的解决方案

    Mysql中关于Incorrect string value的解决方案

    在对mysql数据库中插入数据的时候,直接插入中文是没有问题的!但是用预编译语句时,用流对数据进行处理总报incorrect string value这个异常。本篇文章教给你解决方法
    2021-09-09
  • MySQL 修改数据库名称的一个新奇方法

    MySQL 修改数据库名称的一个新奇方法

    这篇文章主要介绍了MySQL 修改数据库名称的一个新奇方法,MySQL 修改数据库名的一个变通方法,需要的朋友可以参考下
    2014-07-07
  • mysql解压缩方式安装和彻底删除的方法图文详解

    mysql解压缩方式安装和彻底删除的方法图文详解

    这篇文章主要介绍了mysql解压缩方式安装和彻底删除的方法,只有mysql彻底删除干净了,才可以装另外新的版本,需要的朋友可以参考下
    2018-01-01

最新评论