关于mysql innodb count(*)速度慢的解决办法
网络编程 2021-07-05 15:27www.168986.cn编程入门
innodb引擎在统计方面和myisam是不同的,Myisam内置了一个计数器,所以在使用 select count() from table 的时候,直接可以从计数器中取出数据。而innodb必须全表扫描一次方能得到总的数量
innodb引擎在统计方面和myisam是不同的,Myisam内置了一个计数器,所以在使用 select count() from table 的时候,直接可以从计数器中取出数据。而innodb必须全表扫描一次方能得到总的数量。要初步解决这个问题,需要做不同于myisam的一些工作
1、使用第二索引(一般不使用主键索引),并且添加where条件,如
select count() from product where p_id>=0 ;
show index from product ;
id primary key
p_id index
2、如果只需要粗略统计的话也可使用
show status from product; 来得到大约值
这种方法可在数据分页中使用!
3、使用外部计数器,比如建立一个触发器来计数或者在程序上使用缓存方式定时计数,缺陷是这些方法会额外消耗一些资源!
参考资料
mysql高性能http://.mysqlperformanceblog./2006/12/01/count-for-innodb-tables/
mysql DBA:http://imysql./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://.sphinxsearch./archives/89
1、使用第二索引(一般不使用主键索引),并且添加where条件,如
代码如下:
select count() from product where p_id>=0 ;
show index from product ;
id primary key
p_id index
2、如果只需要粗略统计的话也可使用
show status from product; 来得到大约值
这种方法可在数据分页中使用!
3、使用外部计数器,比如建立一个触发器来计数或者在程序上使用缓存方式定时计数,缺陷是这些方法会额外消耗一些资源!
参考资料
mysql高性能http://.mysqlperformanceblog./2006/12/01/count-for-innodb-tables/
mysql DBA:http://imysql./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://.sphinxsearch./archives/89
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程