慢查日志的监控及分析

慢查日志的监控及分析

  • 1 检查慢查日志是否开启
  • 2 MySQL 慢查日志的存储格式
  • 3 MySQL 慢查日志分析工具(pt-query-digest)
    • 3.1 安装 pt-query-digest 工具
    • 3.2 查看服务器信息
    • 3.3 查看磁盘开销使用信息
    • 3.4 查看 MySQL 数据库信息
    • 3.5 分析慢查询日志
    • 3.6 查看 MySQL 的死锁信息
    • 3.7 从慢查询日志中分析索引使用情况
    • 3.8 查找数据库表中重复的索引
    • 3.9 查看不同 MySQL 配置文件的差异
    • 3.10 获取慢日志分析报告
  • 4 如何通过慢查日志发现有问题的 SQL
    • 4.1 查询次数多且每次查询占用时间长的 SQL
    • 4.2 IO 大的 SQL
    • 4.3 未命中的索引的 SQL

1 检查慢查日志是否开启

show variables like 'slow_query_log';
show variables like 'log_qyeries_not_using_indexes';
# 检查查询结果的value值是OFF还是ON,如果为OFF则通过下面的命令修改值
set global slow_query_log=on;
set global log_queries_not_using_indexes=on;
# 检查慢日志的位置信息
show variables like 'slow_query_log_file';
# 然后监控查询出来的日志文件

2 MySQL 慢查日志的存储格式

# SQL执行时间
SET timestamp=1689228091;
# SQL的执行内容
SELECT ORG_ID,ROUTE_ID,COUNT(*) VEH_COUNT FROM (SELECT ORG_ID,ROUTE_ID,PLATE FROM TD_BUS_ARRANGES  WHERE PLATE IN (SELECT * FROM TN_BUS_VEH_COUNTER_INS) GROUP BY ORG_ID,ROUTE_ID,PLATE) A GROUP BY ORG_ID,ROUTE_ID;
# 查询的执行时间
Time: 2023-07-13T06:01:31.655194Z
# 执行SQL的主机信息
User@Host: root[root] @  [192.168.130.79]  Id:     9
# SQL的查询耗时
Query_time: 40.626665  
# SQL锁定时间
Lock_time: 0.000061 
# 所发送的行数
Rows_sent: 0
# 锁扫描的行数
Rows_examined: 2002971

3 MySQL 慢查日志分析工具(pt-query-digest)

3.1 安装 pt-query-digest 工具

yum install wget
wget  https://www.percona.com/downloads/percona-toolkit/2.2.16/RPM/percona-toolkit-2.2.16-1.noarch.rpm
yum localinstall -y  percona-toolkit-2.2.16-1.noarch.rpm
# 如果出现complete,则成功安装。
wget http://percona.com/get/pt-summary

3.2 查看服务器信息

[root@localhost ~]# pt-summary
# Percona Toolkit System Summary Report ######################
        Date | 2023-07-21 08:33:47 UTC (local TZ: CST +0800)
    Hostname | localhost.localdomain
      Uptime | 8 days,  2:30,  2 users,  load average: 0.93, 0.98, 1.03
      System | VMware, Inc.; VMware Virtual Platform; vNone (Other)
 Service Tag | VMware-56 4d 30 99 c8 b7 54 9a-e1 0e 02 65 ce ac 0f df
    Platform | Linux
     Release | CentOS Linux release 7.9.2009 (Core)
      Kernel | 3.10.0-1160.el7.x86_64
Architecture | CPU = 64-bit, OS = 64-bit
   Threading | NPTL 2.17
     SELinux | Permissive
 Virtualized | VMWare
# Processor ##################################################
  Processors | physical = 16, cores = 16, virtual = 16, hyperthreading = no
      Speeds | 16x2399.998
      Models | 16xIntel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz
      Caches | 16x25600 KB
# Memory #####################################################
       Total | 31.3G
        Free | 517.5M
        Used | physical = 3.2G, swap allocated = 15.7G, swap used = 23.9M, virtual = 3.3G
     Buffers | 27.5G
      Caches | 27.6G
       Dirty | 800 kB
     UsedRSS | 2.3G
  Swappiness | 30
 DirtyPolicy | 30, 10
 DirtyStatus | 0, 0
  Locator   Size     Speed             Form Factor   Type          Type Detail
  ========= ======== ================= ============= ============= ===========
  RAM slot #0 16384 MB Unknown           DIMM          DRAM          EDO     
  RAM slot #1 16384 MB Unknown           DIMM          DRAM          EDO     
# Mounted Filesystems ########################################
  Filesystem               Size Used Type     Opts                                                         Mountpoint
  /dev/mapper/centos-home  957G  29% xfs      rw,relatime,seclabel,attr2,inode64,noquota                   /home
  /dev/mapper/centos-root   50G  26% xfs      rw,relatime,seclabel,attr2,inode64,noquota                   /
  /dev/sda1               1014M  15% xfs      rw,relatime,seclabel,attr2,inode64,noquota                   /boot
# Disk Schedulers And Queue Size #############################
        dm-0 | 128
        dm-1 | 128
        dm-2 | 128
         sda | [deadline] 128
         sr0 | [deadline] 128
# Disk Partioning ############################################
Device       Type      Start        End               Size
============ ==== ========== ========== ==================
/dev/dm-0    Disk                              53687091200
/dev/dm-1    Disk                              16911433728
/dev/dm-2    Disk                            1027830972416
/dev/sda     Disk                            1099511627776
/dev/sda1    Part       2048    2099199         1073741312
/dev/sda2    Part    2099200 2147483647      1098436836864
/dev/sr0     Disk                               4712300544
/dev/sr0p1   Part          0    9203711        18849200128
/dev/sr0p2   Part       2428      20027           36042752
# Kernel Inode State #########################################
dentry-state | 46456    25932   45      0       5743    0
     file-nr | 2048     0       3138874
    inode-nr | 45165    7180
# LVM Volumes ################################################
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao---- 957.24g                                                    
  root centos -wi-ao----  50.00g                                                    
  swap centos -wi-ao----  15.75g                                                    
# LVM Volume Groups ##########################################
  VG     VSize     VFree
  centos <1023.00g 4.00m
# RAID Controller ############################################
  Controller | No RAID controller detected
# Network Config #############################################
  Controller | VMware VMXNET3 Ethernet Controller (rev 01)
 FIN Timeout | 60
  Port Range | 60999
# Interface Statistics #######################################
  interface  rx_bytes rx_packets  rx_errors   tx_bytes tx_packets  tx_errors
  ========= ========= ========== ========== ========== ========== ==========
  lo            45000        500          0      45000        500          0
  ens192   100000000000  300000000          0 700000000000  300000000          0
  docker0  500000000000  200000000          0 70000000000  175000000          0
  br-d68411005b14          0          0          0          0          0          0
# Network Devices ############################################
  Device    Speed     Duplex
  ========= ========= =========
  br-d68411005b14                      
  docker0                         
  ens192     10000Mb/s  Full      
# Network Connections ########################################
  Connections from remote IP addresses
    192.168.9.1        70
    192.168.9.3        70
    192.168.9.5        60
    192.168.9.7       125
    192.168.9.9         6
    192.168.10.158      4
    192.168.130.1       3
    192.168.130.78      5
    192.168.130.79     35
  Connections to local IP addresses
    192.168.130.100   400
  Connections to top 10 local ports
    22                  2
    3308              350
  States of connections
    ESTABLISHED       400
    LISTEN              5
# Top Processes ##############################################
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 7583 mysql     20   0 9066304   2.0g  12244 S  50.0  6.6 626:21.40 mysqld
    1 root      20   0  193976   6256   4200 S   0.0  0.0   2:05.02 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.18 kthreadd
    4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+
    5 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/u3+
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.15 ksoftirqd/0
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.80 migration/0
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
    9 root      20   0       0      0      0 S   0.0  0.0   0:26.88 rcu_sched
# Notable Processes ##########################################
  PID    OOM    COMMAND
 1329    -17    sshd
# Simplified and fuzzy rounded vmstat (wait please) ##########
  procs  ---swap-- -----io---- ---system---- --------cpu--------
   r  b    si   so    bi    bo     ir     cs  us  sy  il  wa  st
   4  0     0    0     6   250      0      3   4   0  90   6   0
   0  1     0    0     0  5000   8000   5000   4   2  89   5   0
   0  1     0    0     0  4500   3500   1500   6   0  90   4   0
   1  0     0    0     0  5000   1750    600   3   0  92   5   0
   0  0     0    0     0  4000   1000    500   2   0  95   3   0
# The End ####################################################

3.3 查看磁盘开销使用信息

pt-diskstats

3.4 查看 MySQL 数据库信息

pt-mysql-summary --user=root --password=123456

mysql: [Warning] Using a password on the command line interface can be insecure.
# Percona Toolkit MySQL Summary Report #######################
              System time | 2023-07-21 08:38:35 UTC (local TZ: CST +0800)
# Instances ##################################################
  Port  Data Directory             Nice OOM Socket
  ===== ========================== ==== === ======
                                   0    0   
# MySQL Executable ###########################################
       Path to executable | /usr/sbin/mysqld
              Has symbols | Yes
# Report On Port 3308 ########################################
                     User | root@localhost
                     Time | 2023-07-21 16:38:34 (CST)
                 Hostname | localhost.localdomain
                  Version | 5.7.43 MySQL Community Server (GPL)
                 Built On | Linux x86_64
                  Started | 2023-07-19 11:08 (up 2+05:30:09)
                Databases | 14
                  Datadir | /var/lib/mysql/
                Processes | 350 connected, 1 running
              Replication | Is not a slave, has 0 slaves connected
                  Pidfile | /var/run/mysqld/mysqld.pid (exists)
# Processlist ################################################

  Command                        COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  Query                                 1       1         0         0
  Sleep                               350       0    350000      8000

  User                           COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  root                                350       1         0         0

  Host                           COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  192.168.10.158                        2       0         0         0
  192.168.130.1                         3       0         0         0
  192.168.130.78                        4       0         0         0
  192.168.130.79                       35       0         0         0
  192.168.9.1                          70       0         0         0
  192.168.9.3                          70       0         0         0
  192.168.9.5                          60       0         0         0
  192.168.9.7                         125       0         0         0
  192.168.9.9                           6       0         0         0
  localhost                             2       1         0         0

  db                             COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
  cbmv2                                 4       0         0         0
  datac-nacos                           2       0         0         0
  dolphinscheduler                     45       0         0         0
  giot                                  2       0         0         0
  liaocheng_gj                        300       0         0         0
  usercenter                            2       0         0         0
  State                          COUNT(*) Working SUM(Time) MAX(Time)
  ------------------------------ -------- ------- --------- ---------
                                      350       0         0         0
  starting                              1       1         0         0
# Status Counters (Wait 10 Seconds) ##########################
Variable                                Per day  Per second     11 secs
Aborted_clients                              40                        
Aborted_connects                            800                        
Bytes_received                      10000000000      125000       90000
Bytes_sent                          80000000000      900000      700000
Com_admin_commands                       125000           1           1
Com_alter_table                              20                        
Com_change_db                                45                        
Com_commit                               350000           3           1
Com_create_table                              3                        
Com_delete                                 3000                        
Com_drop_table                                2                        
Com_insert                             20000000         225         175
Com_insert_select                            10                        
Com_kill                                     15                        
Com_rollback                                150                        
Com_select                              7000000          80          15
Com_set_option                           800000           9           4
Com_show_create_table                         8                        
Com_show_databases                            5                        
Com_show_fields                               7                        
Com_show_status                              20                        
Com_show_table_status                        15                        
Com_show_tables                               3                        
Com_show_variables                            4                        
Com_show_warnings                           225                        
Com_update                              3500000          40            
Com_update_multi                            150                        
Connections                               15000                       1
Created_tmp_disk_tables                     350                       1
Created_tmp_files                         12500                        
Created_tmp_tables                        90000           1           6
Handler_commit                         20000000         225          60
Handler_delete                         10000000         125            
Handler_external_lock                  50000000         600         350
Handler_read_first                      2500000          30           3
Handler_read_key                      350000000        4000        1750
Handler_read_last                           125                        
Handler_read_next                    4000000000       50000       30000
Handler_read_prev                        300000           3            
Handler_read_rnd                       17500000         200          20
Handler_read_rnd_next                3000000000       35000       20000
Handler_rollback                             70                        
Handler_update                        300000000        3500        1750
Handler_write                          50000000         600         300
Innodb_buffer_pool_bytes_data          50000000         600     1250000
Innodb_buffer_pool_bytes_dirty          7000000          80     -755153
Innodb_buffer_pool_pages_flushed        8000000          90         100
Innodb_buffer_pool_read_ahead           5000000          60          60
Innodb_buffer_pool_read_ahead_evicted       45000                        
Innodb_buffer_pool_read_requests     9000000000      100000      100000
Innodb_buffer_pool_reads               30000000         350        1000
Innodb_buffer_pool_wait_free             200000           2            
Innodb_buffer_pool_write_requests     250000000        3000         700
Innodb_data_fsyncs                      1250000          15          15
Innodb_data_read                    600000000000     7000000    17500000
Innodb_data_reads                      40000000         450        1000
Innodb_data_writes                      9000000         100         100
Innodb_data_written                 300000000000     3000000     3500000
Innodb_dblwr_pages_written              8000000          90         100
Innodb_dblwr_writes                      175000           2           2
Innodb_log_write_requests              30000000         350          80
Innodb_log_writes                        600000           7           5
Innodb_os_log_fsyncs                     600000           7           6
Innodb_os_log_written               12500000000      150000       45000
Innodb_pages_created                     300000           3           1
Innodb_pages_read                      40000000         450        1000
Innodb_pages_written                    8000000          90         100
Innodb_row_lock_time                      12500                        
Innodb_row_lock_waits                       175                        
Innodb_rows_deleted                    10000000         125            
Innodb_rows_inserted                   22500000         250          70
Innodb_rows_read                     7000000000       90000       50000
Innodb_rows_updated                     9000000         100          60
Innodb_num_open_files                       150                        
Innodb_available_undo_logs                   60                        
Key_read_requests                            25                        
Key_reads                                     6                        
Open_table_definitions                      225                        
Opened_files                              15000                        
Opened_table_definitions                    250                        
Opened_tables                             60000                        
Qcache_not_cached                       7000000          80          15
Queries                                30000000         350         200
Questions                              30000000         350         200
Select_full_join                           9000                        
Select_range                               5000                        
Select_scan                             2500000          30           3
Slow_queries                               8000                       1
Sort_merge_passes                          7000                        
Sort_range                                15000                        
Sort_rows                              17500000         225          20
Sort_scan                                150000           1           1
Ssl_accepts                                 450                        
Ssl_finished_accepts                         45                        
Ssl_session_cache_misses                      6                        
Ssl_used_session_cache_entries               40                        
Table_locks_immediate                   6000000          70         100
Table_locks_waited                           15                        
Table_open_cache_hits                  25000000         300         175
Table_open_cache_misses                   60000                        
Table_open_cache_overflows                60000                        
Threads_created                             175                        
Uptime                                    90000           1           1
# Table cache ################################################
                     Size | 400
                    Usage | 100%
# Key Percona Server features ################################
      Table & Index Stats | Not Supported
     Multiple I/O Threads | Enabled
     Corruption Resilient | Not Supported
      Durable Replication | Not Supported
     Import InnoDB Tables | Not Supported
     Fast Server Restarts | Not Supported
         Enhanced Logging | Not Supported
     Replica Perf Logging | Disabled
      Response Time Hist. | Not Supported
          Smooth Flushing | Not Supported
      HandlerSocket NoSQL | Not Supported
           Fast Hash UDFs | Unknown
# Percona XtraDB Cluster #####################################
# Plugins ####################################################
       InnoDB compression | ACTIVE
# Query cache ################################################
         query_cache_type | OFF
                     Size | 1.0M
                    Usage | 1%
         HitToInsertRatio | 0%
# Schema #####################################################
Specify --databases or --all-databases to dump and summarize schemas
# Noteworthy Technologies ####################################
                      SSL | Yes
     Explicit LOCK TABLES | No
           Delayed Insert | No
          XA Transactions | No
              NDB Cluster | No
      Prepared Statements | No
 Prepared statement count | 0
# InnoDB #####################################################
                  Version | 5.7.43
         Buffer Pool Size | 128.0M
         Buffer Pool Fill | 90%
        Buffer Pool Dirty | 10%
           File Per Table | ON
                Page Size | 16k
            Log File Size | 2 * 48.0M = 96.0M
          Log Buffer Size | 16M
             Flush Method | 
      Flush Log At Commit | 1
               XA Support | ON
                Checksums | ON
              Doublewrite | ON
          R/W I/O Threads | 4 4
             I/O Capacity | 200
       Thread Concurrency | 0
      Concurrency Tickets | 5000
       Commit Concurrency | 0
      Txn Isolation Level | REPEATABLE-READ
        Adaptive Flushing | ON
      Adaptive Checkpoint | 
           Checkpoint Age | 2M
             InnoDB Queue | 0 queries inside InnoDB, 0 queries in queue
       Oldest Transaction | 0 Seconds
         History List Len | 80
               Read Views | 0
         Undo Log Entries | 0 transactions, 0 total undo, 0 max undo
        Pending I/O Reads | 0 buf pool reads, 0 normal AIO, 0 ibuf AIO, 0 preads
       Pending I/O Writes | 0 buf pool (0 LRU, 0 flush list, 0 page); 0 AIO, 0 sync, 0 log IO (0 log, 0 chkp); 0 pwrites
      Pending I/O Flushes | 1 buf pool, 0 log
       Transaction States | 109xnot started
# MyISAM #####################################################
                Key Cache | 8.0M
                 Pct Used | 20%
                Unflushed | 0%
# Security ###################################################
                    Users | 
            Old Passwords | 0
# Binary Logging #############################################
# Noteworthy Variables #######################################
     Auto-Inc Incr/Offset | 1/1
   default_storage_engine | InnoDB
               flush_time | 0
             init_connect | 
                init_file | 
                 sql_mode | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
         join_buffer_size | 256k
         sort_buffer_size | 256k
         read_buffer_size | 128k
     read_rnd_buffer_size | 256k
       bulk_insert_buffer | 0.00
      max_heap_table_size | 16M
           tmp_table_size | 16M
       max_allowed_packet | 4M
             thread_stack | 256k
                      log | 
                log_error | /var/log/mysqld.log
             log_warnings | 2
         log_slow_queries | 
log_queries_not_using_indexes | ON
        log_slave_updates | OFF
# Configuration File #########################################
              Config File | /etc/my.cnf

[mysqld]
default-storage-engine              = INNODB
character_set_server                = utf8
port                                = 3308
lower_case_table_names              = 1
max_connections                     = 5000
sql-mode                            = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
datadir                             = /var/lib/mysql
socket                              = /var/lib/mysql/mysql.sock
symbolic-links                      = 0
log-error                           = /var/log/mysqld.log
pid-file                            = /var/run/mysqld/mysqld.pid
# The End ####################################################

3.5 分析慢查询日志

pt-query-digest /var/lib/mysql/localhost-slow.log

# 28.7s user time, 180ms system time, 66.66M rss, 261.23M vsz
# Current date: Fri Jul 21 17:36:35 2023
# Hostname: localhost.localdomain
# Files: /var/lib/mysql/localhost-slow.log
# Overall: 181.92k total, 135 unique, 0 QPS, 0x concurrency ______________
# Attribute          total     min     max     avg     95%  stddev  median
# ============     ======= ======= ======= ======= ======= ======= =======
# Exec time          2310s    51us     33s    13ms   287us   269ms    60us
# Lock time            31s    22us      1s   167us    84us     8ms    25us
# Rows sent         34.61M       0   2.19M  199.50       0   5.73k       0
# Rows examine     301.56M       0   2.27M   1.70k   13.83  22.42k       0
# Query size        31.24M      24   1.80M  180.06  158.58   4.25k  158.58

# Profile
# Rank Query ID           Response time  Calls  R/Call  V/M   Item
# ==== ================== ============== ====== ======= ===== ============
#    1 0xF4787368A611D689 494.0212 21.4%   1041  0.4746  0.65 SELECT TD_GPS_BUS_HIS
#    2 0x80FFA60035A8F04E 304.3970 13.2%     58  5.2482  2.13 SELECT V?_OPERATIONAL_KILOMETRE V?_SHIFT_ANALYSIS V?_OPERATIONAL_KILOMETRE v?_shift_analysis td_bus_payment_detail
#    3 0x2A7DDE54305082AB 168.4274  7.3%      6 28.0712  1.13 DELETE v?_basic_shift
#    4 0x19678C5E87785D2B 159.8534  6.9%     31  5.1566  3.22 SELECT V?_SHIFT_ANALYSIS
#    5 0x4D5491F4C8E1BD0B 148.9508  6.4%     10 14.8951  0.90 SELECT td_station_add_up_volume_?_sec
#    6 0x8768BFB3856D30BA 120.3152  5.2%   3714  0.0324  0.19 SELECT t_ds_command t_ds_process_definition
#    7 0x8099F96A26863B76 113.9403  4.9%    771  0.1478  0.16 SELECT TD_BUS_PAYMENT_DETAIL
#    8 0xD2EC827E4938634E 107.4045  4.6%    257  0.4179  0.52 SELECT TD_BUS_PAYMENT_DETAIL
#    9 0xEA432256AC539465  50.6545  2.2%      4 12.6636  0.51 SELECT V?_BASIC_DRIVER
#   10 0xB52F563B59470B6A  48.6646  2.1% 170872  0.0003  0.22 SELECT TC_HOLIDAY
#   11 0xD2562A1E022249E4  46.4792  2.0%    257  0.1809  0.16 SELECT TD_BUS_PAYMENT_DETAIL
#   12 0x76C32E6F877CB100  42.8575  1.9%    766  0.0559  0.15 SELECT t_ds_worker_group
#   13 0x226F3864AFE8A3F9  39.0383  1.7%    763  0.0512  0.18 SELECT t_ds_alert
#   14 0xAF0D58FA9654C2F1  35.0482  1.5%      3 11.6827  0.07 SELECT v?_shift_analysis
#   15 0xF413B276033F1D9E  30.9013  1.3%    257  0.1202  0.23 SELECT TD_BUS_PAYMENT_DETAIL
#   16 0xCBD02E4AB231392C  30.6546  1.3%    257  0.1193  0.24 SELECT TD_BUS_PAYMENT_DETAIL tn_bus_stop_area_copy tn_bus_stop_area
#   17 0xEB1B0E22AF922FCA  29.0864  1.3%     32  0.9089  1.28 SELECT UNION V?_PASSENGER_FLOW

3.6 查看 MySQL 的死锁信息

pt-deadlock-logger --user=root --password=123456 --host=localhost

3.7 从慢查询日志中分析索引使用情况

pt-index-usage --user=root --password=123456 /var/lib/mysql/localhost-slow.log
# 这个命令会显示出那些 SQL 没有命中索引,那些索引没有被使用过等等。
ALTER TABLE `cbmv2`.`wp_material_stock` DROP KEY `ware_code_index`; -- type:non-unique
ALTER TABLE `giot`.`sys_resource` DROP KEY `idx_create_date`, DROP KEY `idx_resource_code`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`sys_dict_data` DROP KEY `idx_sort`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`td_bus_arranges` DROP KEY `LEAVE_ARRIVE_INDEX`, DROP KEY `LEAVE_TIME_INDEX`, DROP KEY `PLATE`, DROP KEY `REALARRIVETTIME`, DROP KEY `REALLEAVETIME`, DROP KEY `idx_run_date`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`td_connector_status_info` DROP KEY `index_ConnectorID`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`td_station_add_up_volume_30_sec` DROP KEY `idx_STATTIME`, DROP KEY `index_station_his`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`v3_basic_line` DROP KEY `company_id`, DROP KEY `line_id`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`v3_basic_shift` DROP KEY `ORG_ID`, DROP KEY `REALLEAVETIME`, DROP KEY `ROUTE_NAME`, DROP KEY `RUN_DATE`, DROP KEY `flag`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`v3_major_num` DROP KEY `update_date_time`; -- type:non-unique
ALTER TABLE `liaocheng_gj`.`v3_shift_analysis` DROP KEY `LEAVE_STATION_DATE`, DROP KEY `LINE_SCHEDULING_DIAGRAM`, DROP KEY `company_id`, DROP KEY `idx_leave_time`, DROP KEY `line_id`, DROP KEY `movDir`; -- type:non-unique

3.8 查找数据库表中重复的索引

pt-duplicate-key-checker --user=root --password=123456
# 这个命令会显示出数据库中那些索引是重复索引,那些索引可以进行修改。会以 SQL 的形式展示出来。

# ########################################################################
# liaocheng_gj.v3_line_hour_passenger_flow                                
# ########################################################################

# mult_name_hour_date is a duplicate of PRIMARY
# Key definitions:
#   KEY `mult_name_hour_date` (`line_name`,`stat_date`,`hour`) USING BTREE
#   PRIMARY KEY (`line_name`,`stat_date`,`hour`) USING BTREE,
# Column types:
#         `line_name` varchar(40) collate utf8mb4_unicode_ci not null comment '????'
#         `stat_date` date not null comment '????'
#         `hour` int(11) not null comment '??'
# To remove this duplicate index, execute:
ALTER TABLE `liaocheng_gj`.`v3_line_hour_passenger_flow` DROP INDEX `mult_name_hour_date`;

3.9 查看不同 MySQL 配置文件的差异

pt-config-diff /etc/my.cnf   /etc/my_master.cnf

3.10 获取慢日志分析报告

pt-query-digest --user=root --password=123456 /var/lib/mysql/localhost-slow.log > slow.report

4 如何通过慢查日志发现有问题的 SQL

4.1 查询次数多且每次查询占用时间长的 SQL

  通常为pt-query-digest分析的前几个查询;该工具可以很清楚的看出每个SQL执行的次数及百分比等信息,执行的次数多,占比比较大的SQL。

4.2 IO 大的 SQL

  注意pt-query-digest分析中的Rows examine项。扫描的行数越多,IO越大。

4.3 未命中的索引的 SQL

  注意pt-query-digest分析中的Rows examine 和Rows Send的对比。说明该SQL的索引命中率不高,对于这种SQL,我们要重点进行关注。

你可能感兴趣的:(#,MySQL,mysql)