Reputation: 21
I've a big innodb table which contain 10.000.000 rows
this query
SELECT count(id) FROM table_name
takes 4-6 seconds to execute
I need to decrease the query execution time
1) can someone advice how to achieve this without changing the table to MyIsam
2) In case we need to use a MySQL cache how can we turn that ON on the server?
Upvotes: 1
Views: 454
Reputation: 2257
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
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
Upvotes: 2
Reputation: 427
If your table does not change often, using the MySQL query cache is a good solution.
[mysqld]
query_cache_type = 1
query_cache_size = 10M
Also add index on field 'id' if it doesnt exist. Also if you are writing an application store count in a seperate table as you insert or update. Its handy.
Upvotes: 0