zoukankan      html  css  js  c++  java
  • MySQL中OPTIMIZE TABLE和慢查询分析

    先来看看手册中关于 OPTIMIZE 的描述:

    OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...

    如果您已经删除了表的一大部分,或者如果您已经对含有可变长度行的表(含有VARCHAR, BLOB或TEXT列的表)进行了很多更改,则应使用
    OPTIMIZE TABLE。被删除的记录被保持在链接清单中,后续的INSERT操作会重新使用旧的记录位置。您可以使用OPTIMIZE TABLE来重新
    利用未使用的空间,并整理数据文件的碎片。

    在多数的设置中,您根本不需要运行OPTIMIZE TABLE。即使您对可变长度的行进行了大量的更新,您也不需要经常运行,每周一次或每月一次
    即可,只对特定的表运行。

    OPTIMIZE TABLE只对MyISAM, BDB和InnoDB表起作用。注意,在OPTIMIZE TABLE运行过程中,MySQL会锁定表。
    备注:alter table xxxx engine=innodb这个等于optimzize table xxxx的效果
    
    

    下面进行实操:

    一,原始数据

    1.数据量

    mysql> select count(*) as total from ad_visit_history; 
    +---------+ 
    | total | 
    +---------+ 
    | 1187096 | //总共有118万多条数据 
    +---------+ 
    1 row in set (0.04 sec)

    2.存放在硬盘中的表文件大小(查看数据文件存储的路径可以使用命令:show variables like '%datadir%'

    [root@ localhost linys]# ls | grep visit  | xargs -i du {} 
    382020 ad_visit_history.MYD //数据文件占了380M 
    127116 ad_visit_history.MYI //索引文件占了127M 
    12 ad_visit_history.frm //结构文件占了12K

    3.查看一下索引信息

    mysql> show index from ad_visit_history from test1; //查看一下该表的索引信息 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    | ad_visit_history | 0 | PRIMARY | 1 | id | A | 1187096 | NULL | NULL | | BTREE | | 
    | ad_visit_history | 1 | ad_code | 1 | ad_code | A | 46 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | unique_id | 1 | unique_id | A | 1187096 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 46 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 30438 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | ip_ind | 1 | ip | A | 593548 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | port_ind | 1 | port | A | 65949 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 1187096 | NULL | NULL | YES | BTREE | | 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    8 rows in set (0.28 sec)

    索引信息中的列的信息说明。

    Table :表的名称。
    Non_unique:如果索引不能包括重复词,则为0。如果可以,则为1。
    Key_name:索引的名称。
    Seq_in_index:索引中的列序列号,从1开始。
    Column_name:列名称。
    Collation:列以什么方式存储在索引中。在MySQLSHOW INDEX语法中,有值’A’(升序)或NULL(无分类)。
    Cardinality:索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数,所以即使对于小型表,该值也没有必要是精确的。基数越大,当进行联合时,MySQL使用该索引的机会就越大。
    Sub_part:如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL。
    Packed:指示关键字如何被压缩。如果没有被压缩,则为NULL。
    Null:如果列含有NULL,则含有YES。如果没有,则为空。
    Index_type:存储索引数据结构方法(BTREE, FULLTEXT, HASH, RTREE)

    二,删除一半数据

    mysql> delete from ad_visit_history where id>598000; //删除一半数据 
    Query OK, 589096 rows affected (4 min 28.06 sec)

    [root@localhost linys]# ls | grep visit | xargs -i du {} //相对应的MYD,MYI文件大小没有变化 
    382020 ad_visit_history.MYD 
    127116 ad_visit_history.MYI 
    12 ad_visit_history.frm

    按常规思想来说,如果在数据库中删除了一半数据后,相对应的.MYD,.MYI文件也应当变为之前的一半。但是删除一半数据后,.MYD.MYI尽然连1KB都没有减少,这是多么的可怕啊。

    我们在来看一看,索引信息
    mysql> show index from ad_visit_history; 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    | ad_visit_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | NULL | | BTREE | | 
    | ad_visit_history | 1 | ad_code | 1 | ad_code | A | 23 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 23 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 15333 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | ip_ind | 1 | ip | A | 299000 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | port_ind | 1 | port | A | 33222 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | NULL | YES | BTREE | | 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    8 rows in set (0.00 sec)

    对比一下,这次索引查询和上次索引查询,里面的数据信息基本上是上次一次的一本,这点还是合乎常理。

    三,用optimize table来优化一下

    ??mysql> optimize table ad_visit_history; //删除数据后的优化 
    +------------------------+----------+----------+----------+ 
    | Table | Op | Msg_type | Msg_text | 
    +------------------------+----------+----------+----------+ 
    | test1.ad_visit_history | optimize | status | OK | 
    +------------------------+----------+----------+----------+ 
    1 row in set (1 min 21.05 sec)

    1.查看一下.MYD,.MYI文件的大小

    [root@localhost linys]# ls | grep visit | xargs -i du {} 
    182080 ad_visit_history.MYD //数据文件差不多为优化前的一半 
    66024 ad_visit_history.MYI //索引文件也一样,差不多是优化前的一半 
    12 ad_visit_history.frm

    2.查看一下索引信息
    mysql> show index from ad_visit_history; 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    | ad_visit_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | NULL | | BTREE | | 
    | ad_visit_history | 1 | ad_code | 1 | ad_code | A | 42 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 42 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 24916 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | ip_ind | 1 | ip | A | 598000 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | port_ind | 1 | port | A | 59800 | NULL | NULL | YES | BTREE | | 
    | ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | NULL | YES | BTREE | | 
    +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
    8 rows in set (0.00 sec)

    从以上数据我们可以得出,ad_code,ad_code_ind,from_page_url_ind等索引机会差不多都提高了85%,这样效率提高了好多。

    四,OPTIMIZE TABLE总结

    结合mysql官方网站的信息,个人是这样理解的。当你删除数据时,mysql并不会回收,被已删除数据的占据的存储空间,以及索引位。

    而是空在那里,而是等待新的数据来弥补这个空缺,这样就有一个缺少,如果一时半会,没有数据来填补这个空缺,那这样就太浪费资源了。

    所以对于写比较频烦的表,要定期进行optimize,一个月一次,看实际情况而定了。

    mysql慢查询日志分析:

    1.如何开启慢查询日志?

    在mysql配置文件my.cnf中增加

    log-slow-queries=/var/lib/mysql/slowquery.log (指定日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log)
    long_query_time=2 (记录超过的时间,默认为10s)
    log-queries-not-using-indexes (log下来没有使用索引的query,可以根据情况决定是否开启)
    log-long-format (如果设置了,所有没有使用索引的查询也将被记录)

    2.使用mysql自带命令mysqldumpslow查看

    常用命令
    -s ORDER what to sort by (t, at, l, al, r, ar etc), 'at’ is default
    -t NUM just show the top n queries
    -g PATTERN grep: only consider stmts that include this string

    eg:
    s,是order的顺序,说明写的不够详细,俺用下来,包括看了代码,主要有 c,t,l,r和ac,at,al,ar,分别是按照query次数,时间,lock的时间和返回的记录数来排序,前面加了a的时倒序 -t,是top n的意思,即为返回前面多少条的数据 -g,后边可以写一个正则匹配模式,大小写不敏感的

    mysqldumpslow -s c -t 20 host-slow.log
    mysqldumpslow -s r -t 20 host-slow.log
    上述命令可以看出访问次数最多的20个sql语句和返回记录集最多的20个sql。

    mysqldumpslow -t 10 -s t -g “left join” host-slow.log这个是按照时间返回前10条里面含有左连接的sql语句。

    root@server# tail /var/log/slowqueries

    # Time: 130320 7:30:26
    # User@Host: db_user[db_database] @ localhost []
    # Query_time: 4.545309 Lock_time: 0.000069 Rows_sent: 219 Rows_examined: 254
    SET timestamp=1363779026;
    SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';
    我们来过一下每一行所代表的意思:
    第一行表示记录日志时的时间。其格式是 YYMMDD H:M:S。我们可以看出上面的查询记录于 2013 年 3 月 20 日上午 7:30 - 注意:这个是服务器时间,可能跟你本地时间有所不同然后,我们可以看到 MySql 用户、服务器以及主机名第三行表示总的查询时间、锁定时间、"发送"或者返回的行数、查询过程中所检查的行数接下来我们看到的是 SET timestamp=UNIXTIME; 这是查询实际发生的时间。如果你想找现在的一些慢查询,通过检查这个就不会发生你所检查的是几个月之前所发生的慢查询了。

    SET timestamp= value 才是实际的查询的执行时间。

    文章参考出处

    https://www.cnblogs.com/jimmy-muyuan/p/5874410.html

    https://www.cnblogs.com/jimmy-muyuan/p/5874400.html

  • 相关阅读:
    shell脚本编程-重定向
    web安全-剖析_基础构架剖析
    shell脚本编程-函数
    shell脚本编程-循环
    web安全-入侵基础
    shell脚本编程-检查和测试
    shell脚本编程-特殊字符
    shell脚本编程-计算方式
    python例子-抓取网站IP列表
    linux问题-APR not Found
  • 原文地址:https://www.cnblogs.com/lys_013/p/14004171.html
Copyright © 2011-2022 走看看