关闭

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

1514人阅读 评论(3) 收藏 举报
分类:

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


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






2
0
查看评论

学习使用MySQL 5.7的sys库

Sys库里的数据来源 Sys库所有的数据源来自:performance_schema。目标是把performance_schema的把复杂度降低,让DBA能更好的阅读这个库里的内容。让DBA更快的了解DB的运行情况。   Sys库下有两种表 字母开头: 适合人阅读,显示是格式化的数...
  • yanzongshuai
  • yanzongshuai
  • 2017-06-18 22:00
  • 1387

官方mysql中查看索引是否被使用到

http://blog.sina.com.cn/s/blog_5037eacb0102vm5c.html 官方mysql中查看索引是否被使用到: 在percona版本或marida中可以通过 information_schea.index_statistics查看得到, ...
  • bluetjs
  • bluetjs
  • 2016-09-03 10:56
  • 2493

MySQL的sum函数优化

有一张表,结构如下 :    +------------------+--------------+------+-----+---------+----------------+ | Field            ...
  • xtdhqdhq
  • xtdhqdhq
  • 2014-01-21 11:15
  • 5315

详解MySQL大表优化方案

当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化:单表优化、字段、索引、查询SQL、引擎等。
  • yin767833376
  • yin767833376
  • 2016-08-04 18:00
  • 2960

mysql大表更新sql的优化策略

    问题sql背景:项目有6个表的要根据pid字段要写入对应的brand_id字段。但是这个其中有两个表是千万级别的。我的worker运行之后,线上的mysql主从同步立刻延迟了!运行了一个多小时之后,居然延迟到了40分钟,而且只更新了十几万行数据。问题sql如下:...
  • bruce128
  • bruce128
  • 2013-12-19 21:38
  • 10652

inner join MySQL Profiles 三表内联查询效率优化

今天系统爆出一个慢SQL语句,经过分析确定查询时间过长的原因主要是数据量大三张表内联耗时较长;经过重新设计将不必要的内联逻辑排除,成功的将查询时间降低一个数量级。
  • mrlichengyi
  • mrlichengyi
  • 2017-06-25 17:29
  • 640

【Mysql性能优化四】数据表的设计和读写分离技术

mysql优化可以从子查询,数据类型和索引等多个方面进行优化。这些都是从sql语句的方向进行考虑的。当我们的sql语句没有优化的空间的时候, 我们就必须从其他方面来考虑进行数据库性能优化了。 数据表设计范式化和反范式化范式化是指我们在设计表时要遵循数据库设计的三个范式,通常要符合第三范式:表中的列之...
  • wangyy130
  • wangyy130
  • 2016-05-30 19:51
  • 913

如何解决mysql读写效率

12321
  • oh_Mourinho
  • oh_Mourinho
  • 2016-08-05 10:52
  • 1704

【MySQL】基于MySQL的SQL优化(四)——对group by以及limit的优化

group by:通过下面一条 语句来了解有关group by的优化。SELECT a.first_name,a.last_name,COUNT(*) FROM film_actor fa INNER JOIN actor a USING(actor_id) GROUP BY fa.actor_id...
  • tanglei6636
  • tanglei6636
  • 2016-10-19 17:23
  • 554

MYSQL一次千万级连表查询优化(一)

概述:交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差,因此SQL优化任务交到了我手上。 这个SQL查询关联两个数据表,一个是攻击IP用户表主要是记录IP的信息,如第一次攻击时间,地址,IP...
  • Tim_phper
  • Tim_phper
  • 2017-10-25 18:02
  • 1151
    个人资料
    • 访问:590310次
    • 积分:5588
    • 等级:
    • 排名:第5636名
    • 原创:102篇
    • 转载:33篇
    • 译文:0篇
    • 评论:364条
    个人简介
    我只是一个简单的Coder,为了兴趣和理想奋斗在生活的道路上
    希望大家多多交流,共同进步,有疑问或想法请留言或者私信,也可以加群:157797573

    博客专栏
    最新评论
    你还在一个人孤独的奋斗吗?
    其实,你并不寂寞,欢迎加入CSDN Java群,这里是你的另一个家!    点击加入:CSDN Java群