mysql innodb count(*)速度慢的几个解决方案

2019-02-11  本文已影响16人  小伟_be27

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、如果只需要粗略统计的话也可使用

explain 查出大概的行数

上一篇 下一篇

猜你喜欢

热点阅读