innodb数据库 OPTIMIZE TABLE 提示Table does not support optimize, doing recreate + analyze instead

来源:互联网 发布:软件开发就业情况 编辑:程序博客网 时间:2024/06/07 16:11
Table does not support optimize, doing recreate + analyze instead

提要:
1.MySQL官方建议不要经常(每小时或每天)进行碎片整理,一般根据实际情况,只需要每周或者每月整理一次即可。
2.OPTIMIZE TABLE只对MyISAM,BDB和InnoDB表起作用,尤其是MyISAM表的作用最为明显。此外,并不是所有表都需要进行碎片整理,一般只需要对包含上述可变长度的文本数据类型的表进行整理即可。
3.在OPTIMIZE TABLE运行过程中,MySQL会锁定表。
4.默认情况下,直接对InnoDB引擎的数据表使用OPTIMIZE TABLE,可能会显示「 Table does not support optimize, doing recreate + analyze instead」的提示信息。这个时候,我们可以用mysqld --skip-new或者mysqld --safe-mode命令来重启MySQL,以便于让其他引擎支持OPTIMIZE TABLE。

那么怎么使用--skip-new呢?
先看ps看一下mysql的进程吧!
[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ps aux | grep mysqld  
  2.   
  3. root      2359  0.0  0.0 108300  1308 pts/2    S    13:09   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql  
  4. mysql     3028 46.2 56.5 5771800 4554616 pts/2 Sl   13:09  10:13 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/122-db.err --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock --port=3306  
  5. root      3355  0.0  0.0 103240   872 pts/2    S+   13:31   0:00 grep mysqld  



第一个是守护进程,第二个就是mysql的原始进程。我们只要在这个进程最后追加--skip-new即可。

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/122-db.err --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock --port=3306 --skip-new  



先关闭mysqld,然后执行该命令。

不要关闭该命令,重新开启一个ssh窗口。

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. mysql -uroot -pxxxxx  
  2. mysql> use hr_resume_center;  
  3. Database changed  
  4. mysql> show create table hr_business_from_0\G  
  5. *************************** 1. row ***************************  
  6.        Table: hr_business_from_0  
  7. Create Table: CREATE TABLE `hr_business_from_0` (  
  8.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  9.   `resume_id` bigint(20) unsigned NOT NULL COMMENT '简历ID',  
  10.   `from_id` smallint(4) unsigned DEFAULT NULL COMMENT '来源类型: 与hr_dict_from 来源网站关联',  
  11.   `link_id` varchar(50) DEFAULT NULL COMMENT '来源网站简历标识',  
  12.   `add_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '添加记录时间',  
  13.   `sys_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新记录时间',  
  14.   `version` smallint(4) unsigned NOT NULL DEFAULT '0' COMMENT '妙招简历版本,默认都为最新的版本',  
  15.   PRIMARY KEY (`id`),  
  16.   KEY `resume_id` (`resume_id`),  
  17.   KEY `sys_time` (`sys_time`)  
  18. ) ENGINE=InnoDB AUTO_INCREMENT=5853666 DEFAULT CHARSET=utf8 COMMENT='简历来源表'  
  19. 1 row in set (0.00 sec)  
  20. mysql> show index from hr_business_from_0;  
  21. +--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
  22. | Table              | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |  
  23. +--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
  24. | hr_business_from_0 |          0 | PRIMARY   |            1 | id          | A         |     5802629 |     NULL | NULL   |      | BTREE      |         |               |  
  25. | hr_business_from_0 |          1 | resume_id |            1 | resume_id   | A         |     5802629 |     NULL | NULL   |      | BTREE      |         |               |  
  26. | hr_business_from_0 |          1 | sys_time  |            1 | sys_time    | A         |      290131 |     NULL | NULL   |      | BTREE      |         |               |  
  27. +--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
  28. 3 rows in set (0.99 sec)  
  29.   
  30. mysql> optimize table hr_business_from_0 ;  
  31. Query OK, 5853665 rows affected (2 min 30.36 sec)  
  32. Records: 5853665  Duplicates: 0  Warnings: 0  
  33.   
  34. mysql> show index from hr_business_from_0;  
  35. +--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
  36. | Table              | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |  
  37. +--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
  38. | hr_business_from_0 |          0 | PRIMARY   |            1 | id          | A         |     5898310 |     NULL | NULL   |      | BTREE      |         |               |  
  39. | hr_business_from_0 |          1 | resume_id |            1 | resume_id   | A         |     5898310 |     NULL | NULL   |      | BTREE      |         |               |  
  40. | hr_business_from_0 |          1 | sys_time  |            1 | sys_time    | A         |      842615 |     NULL | NULL   |      | BTREE      |         |               |  
  41. +--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
  42. 3 rows in set (0.00 sec)  



hr_business_from_0为innodb数据,经过优化Cardinality发生的变化。查询速度提成了10倍以上。

附加:

如果你觉得这样一个表优化速度太慢的话,这里有一键优化脚本:

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #!/bin/sh  
  2. echo -n "MySQL username: " ; read username  
  3. echo -n "MySQL password: " ; stty -echo ; read password ; stty echo ; echo  
  4.   
  5. mysql -u $username -p"$password" -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do  
  6. mysql -u $username -p"$password" -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do  
  7. if [ "$datafree" -gt 0 ] ; then  
  8. fragmentation=$(($datafree * 100 / $datalength))  
  9. echo "$database.$name is $fragmentation% fragmented."  
  10. mysql -u "$username" -p"$password" -NBe "OPTIMIZE TABLE $name;" "$database"  
  11. fi  
  12. done  
  13. done  
0 0