MySQL count(*)空表为何会很慢

Count(*)一个“空表”为什么会很慢呢?只有几十行数据为什么select xxx from table limit 1需要300msselect min(pk) fromtable也会慢?

见下例:

dba@localhost : test 18:14:32> show create table test_hmw\G
*************************** 1. row ***************************
    Table: test_hmw
Create Table: CREATE TABLE `test_hmw` (
 `mail` varchar(1024)DEFAULT NULL,
 `user_id` bigint(20)DEFAULT NULL,
 `id` int(10) unsigned NOTNULL AUTO_INCREMENT,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3997636 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
dba@localhost : test 18:14:45> select count(*) from test_hmw;
+----------+
| count(*) |
+----------+
| 3813472 |
+----------+
1 row in set (0.69 sec)
dba@localhost : test 18:14:56> show status like '%last_query%';
+-----------------+---------------+
| Variable_name  |Value     |
+-----------------+---------------+
| Last_query_cost | 776677.599000 |
+-----------------+---------------+
1 row in set (0.00 sec)
dba@localhost : test 18:15:14> delete from test_hmw;
Query OK, 3813472 rows affected (15.11 sec)
dba@localhost : test 18:15:56> select count(*) fromtest_hmw; 
+----------+
| count(*) |
+----------+
|    0 |
+----------+
1 row in set (0.31 sec)
dba@localhost : test 18:15:59> show status like '%last_query%';
+-----------------+---------------+
| Variable_name  |Value     |
+-----------------+---------------+
| Last_query_cost | 757879.799000 |
+-----------------+---------------+
1 row in set (0.00 sec)


我们发现count一个空表的cost和删除前是一样的(时间不一样是因为缓存);其实在Oracle里也存在这个现象,对于select xx from table where rownum<xxx或者select count(*)之类的操作是从起始的block扫描到HMW;而在MySQL里不是HMWMySQL的多版本是直接存储在表里的,因此MySQL在删除了大量的数据,而purge thread回收前进行上面的操作就会扫描大量的“空”page因此也就有了这种现象;

既然知道原因在MySQL可以通过重整表的方式再来看看cost:

dba@localhost : test 18:16:01> optimize table test_hmw;
2 rows in set (1.85 sec)
dba@localhost : test 18:16:14> select count(*) fromtest_hmw; 
+----------+
| count(*) |
+----------+
|    0 |
+----------+
1 row in set (0.00 sec)
dba@localhost : test 18:16:16> show status like '%last_query%';
+-----------------+----------+
| Variable_name  |Value  |
+-----------------+----------+
| Last_query_cost | 1.199000 |
+-----------------+----------+
1 row in set (0.00 sec)


转载于:https://my.oschina.net/HeAlvin/blog/497707

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值