mysql count() 慢_关于mysql innodb count(*)速度慢的解决办法

关于mysql innodb count(*)速度慢的解决办法

更新时间:2012年12月16日 12:35:32   作者:

innodb引擎在统计方面和myisam是不同的,Myisam内置了一个计数器,所以在使用 select count(*) from table 的时候,直接可以从计数器中取出数据。而innodb必须全表扫描一次方能得到总的数量

innodb引擎在统计方面和myisam是不同的,Myisam内置了一个计数器,所以在使用 select count(*) from table 的时候,直接可以从计数器中取出数据。而innodb必须全表扫描一次方能得到总的数量。要初步解决这个问题,需要做不同于myisam的一些工作:

1、使用第二索引(一般不使用主键索引),并且添加where条件,如:

select count(*) from product where comp_id>=0 ;

show index from product ;

id primary key

comp_id index

2、如果只需要粗略统计的话也可使用

show status from product; 来得到大约值

这种方法可在数据分页中使用!

3、使用外部计数器,比如建立一个触发器来计数或者在程序上使用缓存方式定时计数,缺陷是这些方法会额外消耗一些资源!

参考资料:

mysql高性能:http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/

mysql DBA:http://imysql.cn/2008_06_24_speedup_innodb_count

COUNT(*) for Innodb Tables

I guess note number one about MyISAM to Innodb migration is warning what Innodb is very slow in COUNT(*) queries. The part which I often however see omitted is fact it only applies to COUNT(*) queries without WHERE clause.

So if you have query like SELECT COUNT(*) FROM USER It will be much faster for MyISAM (MEMORY and some others) tables because they would simply read number of rows in the table from stored value. Innodb will however need to perform full table scan or full index scan because it does not have such counter, it also can't be solved by simple singe counter for Innodb tables as different transactions may see different number of rows in the table.

If you have query like SELECT COUNT(*) FROM IMAGE WHERE USER_ID=5 this query will be executed same way both for MyISAM and Innodb tables by performing index rage scan. This can be faster or slower both for MyISAM and Innodb depending on various conditions.

In real applications there are much more queries of second type rather than first type so it is typically not as bad problem as it may look. Most typically count of rows is needed by admin tools which may show it in table statistics, it may also be used in application stats to show something like “We have 123.345 users which have uploaded 1.344.656 images” but these are normally easy to remove.

So remember Innodb is not slow for ALL COUNT(*) queries but only for very specific case of COUNT(*) query without WHERE clause. It does not mean I would not like to see it fixed though, it is pretty annoying.

转自:http://www.sphinxsearch.org/archives/89

相关文章

1a1b05c64693fbf380aa1344a7812747.png

这篇文章主要介绍了浅谈MySQL8.0 异步复制的三种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09

4f55910a645b073bc4fc65dc10dc14bd.png

这篇文章主要介绍了MySQL中悲观锁与乐观锁的相关资料,帮助大家更好的理解和学习MySQL数据库,感兴趣的朋友可以了解下2020-09-09

0ea3c7666119d5615e582f823fb3fad6.png

这篇文章主要给大家介绍了MySQL服务自动停止的解决方法,文中给出了详细的解决过程,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-06-06

4f96a78db829b1556ff16de21e013c7a.png

本篇文章是对Mysql中OPTIMIZE TABLE的作用进行了详细的分析介绍,需要的朋友参考下2013-06-06

8cc1031babc6aff2319f1c6af8544aa0.png

下面可以参考下面的方法步骤解决。最后查到一个网友说可能和log文件有关,于是将log文件给移除了,再重启MySQL终于OK了2009-06-06

0c932a99bb7b6f23c937db507070cc7b.png

这篇文章主要介绍了Navicat 远程连接 MySQL实现步骤解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-08-08

cca732bf65a93ed2ec0ac80c638460fe.png

这篇文章主要介绍了Mysql 5.7.18 解压版下载安装及启动mysql服务的图文详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-05-05

2d9f31f2af7b675a3d153d2b7f1035a7.png

mac下安装mysql8.0.11时要求输入密码之后想修改密码。接下来通过本文给大家介绍mac下重置mysl8.0.11密码的方法,需要的朋友可以参考下2018-06-06

b452cee8ec5cd9e58ab98eba17281e59.png

这篇文章主要介绍了Mysql5.7解压版的安装和卸载及常见问题小结,需要的朋友可以参考下2017-11-11

f4838ec7e2d4da28e0b57d4e852dadd4.png

本篇文章介绍了unique与primary约束的区别分析。需要的朋友参考下2013-04-04

最新评论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值