简体   繁体   English

innodb表计数优化

[英]innodb table count optimization

I've a big innodb table which contain 10.000.000 rows 我有一个大的innodb表,其中包含10.000.000行

this query SELECT count(id) FROM table_name takes 4-6 seconds to execute 此查询SELECT count(id)FROM table_name执行需要4-6秒

I need to decrease the query execution time 我需要减少查询执行时间

1) can someone advice how to achieve this without changing the table to MyIsam 1)有人可以建议如何实现此目标而不将表更改为MyIsam

2) In case we need to use a MySQL cache how can we turn that ON on the server? 2)如果需要使用MySQL缓存,如何在服务器上将其打开?

Lie about the count in the application. 说谎关于应用程序中的计数。 Really, in rare cases you need exact number. 确实,在极少数情况下,您需要确切的数字。 Approximate row count (but as fast as select count(*) in MyISAM) you can get from 您可以从中获得大概的行数(但与MyISAM中的select count(*)一样快)

SELECT MAX(id) - MIN(id) AS count FROM table

if you still need the exact number you can create a table with the count number and update it with a trigger ON INSERT and ON DELETE 如果您仍然需要确切的数字,则可以创建带有计数数字的表,并使用触发器ON INSERT和ON DELETE对其进行更新

If your table does not change often, using the MySQL query cache is a good solution. 如果您的表不经常更改,那么使用MySQL查询缓存是一个很好的解决方案。

[mysqld]
query_cache_type = 1
query_cache_size = 10M

Also add index on field 'id' if it doesnt exist. 如果字段“ id”不存在,还添加索引。 Also if you are writing an application store count in a seperate table as you insert or update. 同样,如果您在插入或更新时将应用程序商店计数写入单独的表中。 Its handy. 方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM