前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mysql 优化——分析表读写和sql效率问题

Mysql 优化——分析表读写和sql效率问题

作者头像
执笔记忆的空白
发布2020-12-25 14:46:57
9410
发布2020-12-25 14:46:57
举报
文章被收录于专栏:Java日常

上次我们说到mysql的一些sql查询方面的优化,包括查看explain执行计划,分析索引等等。 今天我们分享一些 分析mysql表读写、索引等等操作的sql语句。

闲话不多说,直接上代码:

代码语言:javascript
复制
-- 反映表的读写压力

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,你能轻松知道你的库那些表存在问题,然后考虑怎么去优化。

另外,有些博友问我为何每次博客不写全面,比如为何优化什么的,我想说的是,大部分人只关心如何用,至于为什么,其实可以自己去找答案,而且我也没太多时间去写。至于优不优质博客我不在乎,这些算是我的自己的日常积累吧

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/06/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档