Mysql查询优化

 

mysqlreport 查看数据库的执行情况

 

1 下载 mysqlreport

2 安装的时候报错,因为没有安装DBD

 

解决方式:

shell> perl -MCPAN -e shell 

cpan> install DBI 

cpan> install DBD::mysql 安装成功。

 

3 使用

http://blog.chinaunix.net/uid-24426415-id-77301.html

 

4连接数据库 

perl mysqlreport --user=root --password=111111 --host=192.168.0.231

 

数据如下:

 

Use of uninitialized value $is in multiplication (*) at mysqlreport line 829.
Use of uninitialized value in formline at mysqlreport line 1227.
MySQL 5.1.46-community-  uptime 7 2:5:41        Mon Aug 10 10:50:40 2015

__ Key _________________________________________________________________
Buffer used    13.07M of  25.00M  %Used:  52.27
  Current       4.68M            %Usage:  18.72
Write hit      99.97%
Read hit       99.97%

__ Questions ___________________________________________________________
Total           2.19M     3.6/s
  DMS           2.45M     4.0/s  %Total: 112.00
  -Unknown      2.13M     3.5/s           97.07
  Com_          1.58M     2.6/s           72.05
  COM_QUIT    232.59k     0.4/s           10.62
  QC Hits      52.45k     0.1/s            2.39
Slow 3 s          669     0.0/s            0.03  %DMS:   0.03  Log:  ON
DMS             2.45M     4.0/s          112.00
  INSERT        1.17M     1.9/s           53.30         47.59
  DELETE      957.53k     1.6/s           43.72         39.04
  SELECT      237.60k     0.4/s           10.85          9.69
  UPDATE       65.82k     0.1/s            3.01          2.68
  REPLACE      24.61k     0.0/s            1.12          1.00
Com_            1.58M     2.6/s           72.05
  set_option  911.78k     1.5/s           41.63
  commit      362.44k     0.6/s           16.55
  show_create  75.36k     0.1/s            3.44

__ SELECT and Sort _____________________________________________________
Scan          184.37k     0.3/s %SELECT:  77.60
Range         102.60k     0.2/s           43.18
Full join         123     0.0/s            0.05
Range check         0       0/s            0.00
Full rng join       0       0/s            0.00
Sort scan       1.76k     0.0/s
Sort range      1.60k     0.0/s
Sort mrg pass  10.06k     0.0/s

__ Query Cache _________________________________________________________
Memory usage   15.53M of  18.00M  %Used:  86.29
Block Fragmnt  11.79%
Hits           52.45k     0.1/s
Inserts       129.96k     0.2/s
Insrt:Prune   24.78:1     0.2/s
Hit:Insert     0.40:1

__ Table Locks _________________________________________________________
Waited              1     0.0/s  %Total:   0.00
Immediate     441.17k     0.7/s

__ Tables ______________________________________________________________
Open               12 of  669    %Cache:   1.79
Opened        132.09k     0.2/s

__ Connections _________________________________________________________
Max used          115 of  700      %Max:  16.43
Total         232.95k     0.4/s

__ Created Temp ________________________________________________________
Disk table      8.08k     0.0/s
Table         144.33k     0.2/s    Size:  16.0M
File            1.25k     0.0/s

__ Threads _____________________________________________________________
Running             1 of   32
Cached              8 of   34      %Hit:  99.90
Created           222     0.0/s
Slow                0       0/s

__ Aborted _____________________________________________________________
Clients           707     0.0/s
Connects        2.05k     0.0/s

__ Bytes _______________________________________________________________
Sent            2.46G    4.0k/s
Received      146.85M   239.8/s

__ InnoDB Buffer Pool __________________________________________________
Usage          46.00M of  46.00M  %Used: 100.00
Read hit       99.47%
Pages
  Free              0            %Total:   0.00
  Data          2.94k                     99.90 %Drty:   0.00
  Misc              3                      0.10
  Latched                                  0.00
Reads           1.92G    3.1k/s
  From file    10.16M    16.6/s            0.53
  Ahead Rnd    618194     1.0/s
  Ahead Sql     54258     0.1/s
Writes         62.87M   102.7/s
Flushes         2.00M     3.3/s
Wait Free           0       0/s

__ InnoDB Lock _________________________________________________________
Waits           26345     0.0/s
Current             0
Time acquiring
  Total        743022 ms
  Average          28 ms
  Max             168 ms

__ InnoDB Data, Pages, Rows ____________________________________________
Data
  Reads        44.94M    73.4/s
  Writes        2.12M     3.5/s
  fsync       124.44k     0.2/s
  Pending
    Reads           0
    Writes          0
    fsync           0

Pages
  Created     557.21k     0.9/s
  Read         44.94M    73.4/s
  Written       2.00M     3.3/s

Rows
  Deleted       1.15M     1.9/s
  Inserted      9.48M    15.5/s
  Read          3.02G    4.9k/s
  Updated     114.56k     0.2/s

 

 

 

6 数据库索引使用:

  1)解释查询,用explain来查看sql的执行情况 

  2)使用组合索引,优化多个查询条件 ,最左索引原则的使用,不要重复创建索引

 

7 开启数据库慢日志记录功能,及时监控sql的瓶颈

8 因为建立索引数据的插入和更新速度会变慢,需要估算表中 查询和删改查 各占比例 

  如果查询比例比较大,则建立索引是值得的

9 如果一个查询过慢,需要确定是否是表锁定,用:SHOW PROCESSLIST  

 

10 查询数据库锁表: SHOW OPEN TABLES WHERE In_use > 0;

 

 

 

你可能感兴趣的:(Mysql查询优化)