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

所属分类: 数据库 / Mysql 阅读数: 1788
收藏 0 赞 0 分享
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
更多精彩内容其他人还在看

Mac 将mysql路径加入环境变量的方法

这篇文章主要介绍了Mac如何将mysql路径加入环境变量,有需要的朋友好按照下面的步骤操作即可
收藏 0 赞 0 分享

mysql 增加修改字段类型及删除字段类型

本节主要介绍了mysql如何增加修改字段类型及删除字段类型,需要的朋友可以参考下
收藏 0 赞 0 分享

Mysql主从复制(master-slave)实际操作案例

这篇文章主要介绍了Mysql主从复制(master-slave)实际操作案例,同时介绍了Mysql grant 用户授权的相关内容,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL异常处理浅析

这篇文章主要介绍了MySQL的异常处理,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL存储毫秒数据的方法

MySQL中没有可以直接存储毫秒数据的数据类型,但是不过MySQL却能识别时间中的毫秒部分。这篇文章主要介绍了MySQL存储毫秒数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

MySql中使用INSERT INTO语句更新多条数据的例子

这篇文章主要介绍了MySql中使用INSERT INTO语句更新多条数据的例子,MySQL的特有语法,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows下MySql错误代码1045的解决方法

这篇文章主要介绍了Windows下MySql错误代码1045的解决方法,文中还包含了2个Linux下的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

mysql查询今天、昨天、近7天、近30天、本月、上一月的SQL语句

这篇文章主要介绍了mysql查询今天、昨天、近7天、近30天、本月、上一月的SQL语句,一般在一些统计报表中比较常用这个时间段,需要的朋友可以参考下
收藏 0 赞 0 分享

mysql的中文数据按拼音排序的2个方法

这篇文章主要介绍了mysql的中文数据按拼音排序的2个方法,用于一些特殊环境,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL定期分析检查与优化表的方法小结

听DBA的人说,相比oracle,MySQL就是一个玩具级别的数据库,在网易门户中,DBA基本很少去管理到MySQL的东西,所以我们产品使用到的MySQL的一些配置和优化还是需要我们开发人员自己动手,下面就简单介绍一下实用的定期优化方法
收藏 0 赞 0 分享
查看更多