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

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的进程吧!
ps aux | grep mysqld

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
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
root      3355  0.0  0.0 103240   872 pts/2    S+   13:31   0:00 grep mysqld



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

/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窗口。

mysql -uroot -pxxxxx
mysql> use hr_resume_center;
Database changed
mysql> show create table hr_business_from_0\G
*************************** 1. row ***************************
       Table: hr_business_from_0
Create Table: CREATE TABLE `hr_business_from_0` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `resume_id` bigint(20) unsigned NOT NULL COMMENT '简历ID',
  `from_id` smallint(4) unsigned DEFAULT NULL COMMENT '来源类型: 与hr_dict_from 来源网站关联',
  `link_id` varchar(50) DEFAULT NULL COMMENT '来源网站简历标识',
  `add_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '添加记录时间',
  `sys_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新记录时间',
  `version` smallint(4) unsigned NOT NULL DEFAULT '0' COMMENT '妙招简历版本,默认都为最新的版本',
  PRIMARY KEY (`id`),
  KEY `resume_id` (`resume_id`),
  KEY `sys_time` (`sys_time`)
) ENGINE=InnoDB AUTO_INCREMENT=5853666 DEFAULT CHARSET=utf8 COMMENT='简历来源表'
1 row in set (0.00 sec)
mysql> show index from hr_business_from_0;
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table              | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| hr_business_from_0 |          0 | PRIMARY   |            1 | id          | A         |     5802629 |     NULL | NULL   |      | BTREE      |         |               |
| hr_business_from_0 |          1 | resume_id |            1 | resume_id   | A         |     5802629 |     NULL | NULL   |      | BTREE      |         |               |
| hr_business_from_0 |          1 | sys_time  |            1 | sys_time    | A         |      290131 |     NULL | NULL   |      | BTREE      |         |               |
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.99 sec)

mysql> optimize table hr_business_from_0 ;
Query OK, 5853665 rows affected (2 min 30.36 sec)
Records: 5853665  Duplicates: 0  Warnings: 0

mysql> show index from hr_business_from_0;
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table              | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| hr_business_from_0 |          0 | PRIMARY   |            1 | id          | A         |     5898310 |     NULL | NULL   |      | BTREE      |         |               |
| hr_business_from_0 |          1 | resume_id |            1 | resume_id   | A         |     5898310 |     NULL | NULL   |      | BTREE      |         |               |
| hr_business_from_0 |          1 | sys_time  |            1 | sys_time    | A         |      842615 |     NULL | NULL   |      | BTREE      |         |               |
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)



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

附加:

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

#!/bin/sh
echo -n "MySQL username: " ; read username
echo -n "MySQL password: " ; stty -echo ; read password ; stty echo ; echo

mysql -u $username -p"$password" -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do
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
if [ "$datafree" -gt 0 ] ; then
fragmentation=$(($datafree * 100 / $datalength))
echo "$database.$name is $fragmentation% fragmented."
mysql -u "$username" -p"$password" -NBe "OPTIMIZE TABLE $name;" "$database"
fi
done
done


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值