首页 > 数据库 >实例说明optimize table在优化mysql时很重要

实例说明optimize table在优化mysql时很重要

时间:2022-12-20 11:24:26浏览次数:60  
标签:BTREE ad visit mysql table YES NULL optimize history

实例说明optimize table在优化mysql时很重要

张映 发表于 2011-03-07

分类目录: mysql

标签:mysql, optimize table, 优化

今天在看CU的时候,发现有人问有关optimize来表优化的问题,当年因为这个问题,困扰我很长一段时间,今天有空我把这个问题,用实际数据来展示出来,让大家可以亲眼来看看,optimize table的重要作用,而不是似是而非的估计了。

一,原始数据

1,数据量

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

2,存放在硬盘中的表文件大小

查看复制打印?
  1. [root@BlackGhost test1]# ls |grep visit |xargs -i du {}  
  2. 382020    ad_visit_history.MYD                    //数据文件占了380M  
  3. 127116    ad_visit_history.MYI                     //索引文件占了127M  
  4. 12    ad_visit_history.frm                              //结构文件占了12K  
[root@BlackGhost test1]# 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,查看一下索引信息

查看复制打印?
  1. mysql> show index from ad_visit_history from test1;     //查看一下该表的索引信息  
  2. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  3. | Table            | Non_unique | Key_name          | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |  
  4. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  5. | ad_visit_history |          0 | PRIMARY           |            1 | id            | A         |     1187096 |     NULL | NULL   |      | BTREE      |         |  
  6. | ad_visit_history |          1 | ad_code           |            1 | ad_code       | A         |          46 |     NULL | NULL   | YES  | BTREE      |         |  
  7. | ad_visit_history |          1 | unique_id         |            1 | unique_id     | A         |     1187096 |     NULL | NULL   | YES  | BTREE      |         |  
  8. | ad_visit_history |          1 | ad_code_ind       |            1 | ad_code       | A         |          46 |     NULL | NULL   | YES  | BTREE      |         |  
  9. | ad_visit_history |          1 | from_page_url_ind |            1 | from_page_url | A         |       30438 |     NULL | NULL   | YES  | BTREE      |         |  
  10. | ad_visit_history |          1 | ip_ind            |            1 | ip            | A         |      593548 |     NULL | NULL   | YES  | BTREE      |         |  
  11. | ad_visit_history |          1 | port_ind          |            1 | port          | A         |       65949 |     NULL | NULL   | YES  | BTREE      |         |  
  12. | ad_visit_history |          1 | session_id_ind    |            1 | session_id    | A         |     1187096 |     NULL | NULL   | YES  | BTREE      |         |  
  13. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  14. 8 rows in set (0.28 sec)  
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)

二,删除一半数据

查看复制打印?
  1. mysql> delete from ad_visit_history where id>598000;          //删除一半数据  
  2. Query OK, 589096 rows affected (4 min 28.06 sec)  
  3.   
  4. [root@BlackGhost test1]# ls |grep visit |xargs -i du {}              //相对应的MYD,MYI文件大小没有变化  
  5. 382020    ad_visit_history.MYD   
  6. 127116    ad_visit_history.MYI  
  7. 12    ad_visit_history.frm  
mysql> delete from ad_visit_history where id>598000;          //删除一半数据
Query OK, 589096 rows affected (4 min 28.06 sec)

[root@BlackGhost test1]# 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都没有减少,这是多么的可怕啊。

我们在来看一看,索引信息

  1. mysql> show index from ad_visit_history;  
  2. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  3. | Table            | Non_unique | Key_name          | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |  
  4. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  5. | ad_visit_history |          0 | PRIMARY           |            1 | id            | A         |      598000 |     NULL | NULL   |      | BTREE      |         |  
  6. | ad_visit_history |          1 | ad_code           |            1 | ad_code       | A         |          23 |     NULL | NULL   | YES  | BTREE      |         |  
  7. | ad_visit_history |          1 | unique_id         |            1 | unique_id     | A         |      598000 |     NULL | NULL   | YES  | BTREE      |         |  
  8. | ad_visit_history |          1 | ad_code_ind       |            1 | ad_code       | A         |          23 |     NULL | NULL   | YES  | BTREE      |         |  
  9. | ad_visit_history |          1 | from_page_url_ind |            1 | from_page_url | A         |       15333 |     NULL | NULL   | YES  | BTREE      |         |  
  10. | ad_visit_history |          1 | ip_ind            |            1 | ip            | A         |      299000 |     NULL | NULL   | YES  | BTREE      |         |  
  11. | ad_visit_history |          1 | port_ind          |            1 | port          | A         |       33222 |     NULL | NULL   | YES  | BTREE      |         |  
  12. | ad_visit_history |          1 | session_id_ind    |            1 | session_id    | A         |      598000 |     NULL | NULL   | YES  | BTREE      |         |  
  13. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  14. 8 rows in set (0.00 sec)  
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来优化一下

查看复制打印?
  1. mysql> optimize table ad_visit_history;                                             //删除数据后的优化  
  2. +------------------------+----------+----------+----------+  
  3. | Table                  | Op       | Msg_type | Msg_text |  
  4. +------------------------+----------+----------+----------+  
  5. | test1.ad_visit_history | optimize | status   | OK       |  
  6. +------------------------+----------+----------+----------+  
  7. 1 row in set (1 min 21.05 sec)  
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文件的大小

查看复制打印?
  1. [root@BlackGhost test1]# ls |grep visit |xargs -i du {}  
  2. 182080    ad_visit_history.MYD                                          //数据文件差不多为优化前的一半  
  3. 66024    ad_visit_history.MYI                                             //索引文件也一样,差不多是优化前的一半  
  4. 12    ad_visit_history.frm  
[root@BlackGhost test1]# ls |grep visit |xargs -i du {}
182080    ad_visit_history.MYD                                          //数据文件差不多为优化前的一半
66024    ad_visit_history.MYI                                             //索引文件也一样,差不多是优化前的一半
12    ad_visit_history.frm

2,查看一下索引信息

  1. mysql> show index from ad_visit_history;  
  2. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  3. | Table            | Non_unique | Key_name          | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |  
  4. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  5. | ad_visit_history |          0 | PRIMARY           |            1 | id            | A         |      598000 |     NULL | NULL   |      | BTREE      |         |  
  6. | ad_visit_history |          1 | ad_code           |            1 | ad_code       | A         |          42 |     NULL | NULL   | YES  | BTREE      |         |  
  7. | ad_visit_history |          1 | unique_id         |            1 | unique_id     | A         |      598000 |     NULL | NULL   | YES  | BTREE      |         |  
  8. | ad_visit_history |          1 | ad_code_ind       |            1 | ad_code       | A         |          42 |     NULL | NULL   | YES  | BTREE      |         |  
  9. | ad_visit_history |          1 | from_page_url_ind |            1 | from_page_url | A         |       24916 |     NULL | NULL   | YES  | BTREE      |         |  
  10. | ad_visit_history |          1 | ip_ind            |            1 | ip            | A         |      598000 |     NULL | NULL   | YES  | BTREE      |         |  
  11. | ad_visit_history |          1 | port_ind          |            1 | port          | A         |       59800 |     NULL | NULL   | YES  | BTREE      |         |  
  12. | ad_visit_history |          1 | session_id_ind    |            1 | session_id    | A         |      598000 |     NULL | NULL   | YES  | BTREE      |         |  
  13. +------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+  
  14. 8 rows in set (0.00 sec)  
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%,这样效率提高了好多。

四,小结

结合mysql官方网站的信息,个人是这样理解的。当你删除数据时,mysql并不会回收,被已删除数据的占据的存储空间,以及索引位。而是空在那里,而是等待新的数据来弥补这个空缺,这样就有一个缺少,如果一时半会,没有数据来填补这个空缺,那这样就太浪费资源了。所以对于写比较频烦的表,要定期进行optimize,一个月一次,看实际情况而定了。

举个例子来说吧。有100个php程序员辞职了,但是呢只是人走了,php的职位还在那里,这些职位不会撤销,要等新的php程序来填补这些空位。招一个好的程序员,比较难。我想大部分时间会空在那里。哈哈。

五,手册中关于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会锁定表。

标签:BTREE,ad,visit,mysql,table,YES,NULL,optimize,history
From: https://www.cnblogs.com/xiaoxi-jinchen/p/16993782.html

相关文章

  • [MySQL]SELECT list is not in GROUP BY clause and contains nonaggregated column..
    项目在windows上运行正常,迁移到Linux系统上就出现了这个错,经查是由于本人sql不规范且恰好mysql的sql_mode启用了only_full_group_by,当sql语句中使用了分组查询,并且查询的字......
  • MySQL使用--存储过程篇
    查看存储过程:select`name`frommysql.procwheredb='your_db_name'and`type`='PROCEDURE'SHOWprocedurestatus;查看存储过程的创建代......
  • MySQL使用--通用操作篇
    服务启动:netstartmysql(windows启动)"/etc/rc.d/init.d/mysqldstart"(linux启动)用户登录:mysql[mydb]-h127.0.0.1-uroot-ppassword;常用统计......
  • MySQL使用--表操作篇
    查看表描述:DESCRIBEMYTABLE;创建表:CREATETABLE[IFNOTEXISTS]表名称(列名称1数据类型[属性][索引],列名称2数据类型[属性][索引],列名称......
  • MySQL使用--内建函数篇
    一.字符串类:CHARSET(str)//返回字串字符集CONCAT(string2[,...])//连接字串INSTR(string,substring)//返回substring首次在string中出现的......
  • mysql: "tablename" is marked as crashed and should be repaired修复 问题分析: 当m
    问题分析:当mysql出现这类提示时,大部分的现象为查询不到正常的表记录,或者只能查询到部分。主要的原因是对应表的索引文件被损坏,导致索引文件损坏的可能原因有:频繁的读写表......
  • MySQL批量插入数据的四种方案(性能测试对比)
    本文记录个人使用MySQL插入大数据总结较实用的方案,通过对常用插入大数据的4种方式进行测试,即for循环单条、拼接SQL、批量插入saveBatch()、循环+开启批处理模式,得出比较......
  • 【MySQL】登录密码连续错误锁定账户,Failed-Login Tracking 和 Temporary Account Lock
    MySQL8.0.19及更新版本,支持连续几次输入错误密码,锁定账户功能FAILED_LOGIN_ATTEMPTS:代表尝试失败的次数PASSWORD_LOCK_TIME:代表锁定的时间,单位天。#指定策略和加密插件......
  • mysql事务隔离级别详解和实战
    A事务做了操作 没有提交 对B事务来说 就等于没做 获取的都是之前的数据但是 在A事务中查询的话 查到的都是操作之后的数据没有提交的数据只有自己看得到,并没有update......
  • 俗话:MySQL索引
    MySQL凭借着出色的性能、低廉的成本、丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库。虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程......