25
mysql> select count(*) from id_renewal ;
+----------+
| count(*) |
+----------+
| 13633246 |
+----------+

Is taking more than 10 mins to exec the output. Can this be tuned in server parameters? As I should run this query every hr for reports, query taking 10mins is not feasible for my business folks...

Any options to KEEP in memory like in oracle?

mysql> show variables like '%cache%';
+------------------------------+----------------------+
| Variable_name                | Value                |
+------------------------------+----------------------+
| binlog_cache_size            | 32768                |
| have_query_cache             | YES                  |
| key_cache_age_threshold      | 300                  |
| key_cache_block_size         | 1024                 |
| key_cache_division_limit     | 100                  |
| max_binlog_cache_size        | 18446744073709547520 |
| query_cache_limit            | 1048576              |
| query_cache_min_res_unit     | 4096                 |
| query_cache_size             | 134217728            |
| query_cache_type             | ON                   |
| query_cache_wlock_invalidate | OFF                  |
| table_definition_cache       | 512                  |
| table_open_cache             | 2048                 |
| thread_cache_size            | 16                   |
+------------------------------+----------------------+
14 rows in set (0.00 sec)

mysql> show global status like '%Qc%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 118       |
| Qcache_free_memory      | 133367960 |
| Qcache_hits             | 71077421  |
| Qcache_inserts          | 137390744 |
| Qcache_lowmem_prunes    | 18066     |
| Qcache_not_cached       | 120209332 |
| Qcache_queries_in_cache | 427       |
| Qcache_total_blocks     | 990       |
+-------------------------+-----------+



mysql> select count(*) from idea_sub_renewal ;
+----------+
| count(*) |
+----------+
| 13633246 |
+----------+


top - 17:40:19 up 148 days, 17:51, 10 users,  load average: 0.83, 0.91, 1.00
Tasks: 257 total,   1 running, 251 sleeping,   0 stopped,   5 zombie
Cpu(s):  2.0%us,  0.6%sy,  0.0%ni, 97.1%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st
Mem:   8167348k total,  8124120k used,    43228k free,    33928k buffers
Swap: 16386260k total,   709864k used, 15676396k free,  4615456k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                          
26329 sym       19   0 1195m  64m 7456 S 13.3  0.8  66:34.40 java                                                                                                                              
12079 mysql     15   0 1725m 463m 4840 S  6.3  5.8  24227:57 mysqld                                                                                                                            
  477 sym       18   0  674m  62m 7260 S  1.0  0.8   2:25.59 java                                                                                                                              
26948 powerdev  16   0 12896 1232  824 S  0.7  0.0   0:07.90 top                                                                                                                               
18843 sym       19   0 1271m 494m 7364 S  0.3  6.2  10:19.89 java                                                                                                                              
26379 sym       21   0 1203m 299m 7464 S  0.3  3.8   1:36.90 java                                                                                                                              
29872 sym       18   0 1238m 869m 7816 S  0.3 10.9   7:42.33 java    
9
  • 1
    do you have any index at that table? can you show us a SHOW CREATE TABLE LIKE id_renewal?
    – Puggan Se
    Jul 7, 2012 at 12:26
  • 10 minutes is too much, even for 10M rows. Do you have any index on the table? Do you have intensive writing (insert/delete/update) on the table? Jul 7, 2012 at 12:53
  • @Sash: Why should COUNT(1) be any different than COUNT(*)? Jul 7, 2012 at 13:18
  • Well in 5.5.25 I face a big flaw in replication. For which I have logged a bug with mysql - bugs.mysql.com/bug.php?id=65837 . I turned the engine to innodb and below is its table status, id_renewal | InnoDB | 10 | Compact | 13644275 | 263 | 3600809984 | 0 | 1547698176 | 1113587712 | 13644478 | 2012-07-06 03:19:21 | NULL | NULL | latin1_swedish_ci | NULL | still it returns in 10 mins and above. Below are for variables in buffer.. | key_buffer_size-268435456 read_buffer_size -1048576
    – mannoj
    Jul 7, 2012 at 14:06
  • What happens when you count an indexed field on that table? How much time does that take? SELECT count(id) from id_renewal ;
    – SuperMykEl
    Jul 7, 2012 at 17:58

5 Answers 5

31

With InnoDB COUNT() works slowly for tables with million rows. But you can use a hack to see how many rows in table, if you aren't using WHERE - use EXPLAIN.

mysql> explain select count(1) from history;
+----+-------------+---------+-------+---------------+-----------+---------+------+----------+-------------+
| id | select_type | table   | type  | possible_keys | key       | key_len | ref  | rows     | Extra       |
+----+-------------+---------+-------+---------------+-----------+---------+------+----------+-------------+
|  1 | SIMPLE      | history | index | NULL          | history_1 | 12      | NULL | 17227419 | Using index |
+----+-------------+---------+-------+---------------+-----------+---------+------+----------+-------------+
1 row in set (0.01 sec)

In column 'rows' you can see the number of rows.

5
  • 2
    But does this hack shows you real count of rows in realtime, not cached value? I guess MySQL doesn't need to touch the table to give you result of SELECT count("data_not_stored_in_table").
    – AntonioK
    Dec 26, 2014 at 7:31
  • 1
    For InnoDB tables this will almost never show the corect value, but random values (and very big differences between the random values) Mar 9, 2018 at 15:15
  • Totally new option to know about, thanks, We had 355870477 rows in table and thus count(*)/count(1) was dying. Dec 6, 2018 at 5:41
  • 1
    how about if i need to use where
    – peiman F.
    Dec 22, 2018 at 13:47
  • 1
    @peimanF. with where the only way is to use standard count() query and wait.
    – cronfy
    Dec 24, 2018 at 6:50
2

try select (distinct id) from tbl , id beeing the primary key field. So mysql (8) is using the index, without distinct the same query takes 16 seconds for a 100000 records instead of 0.096 seconds!

0
1

Try ::

Select count(1) from table
1
  • | innodb_buffer_pool_size | 33554432 | | innodb_log_buffer_size | 4194304 |
    – mannoj
    Jul 9, 2012 at 11:31
0

change mysql to latest version like 5.5.25 inno db. - myisam db may be slow when update and delete

distribute mysql data and log disk. - disk load distribution is very important

check key_buffer_size, read_buffer_size - not query cache

change disk to SSD - boosting speed

check mysql logs for max connection, slow query and others - speed comes when every logs are cleared

Show... your table definitions for reviewing indexes.

7
  • I Just turned the engine to innodb and below is its table status, id_renewal | InnoDB | 10 | Compact | 13644275 | 263 | 3600809984 | 0 | 1547698176 | 1113587712 | 13644478 | 2012-07-06 03:19:21 | NULL | NULL | latin1_swedish_ci | NULL |
    – mannoj
    Jul 7, 2012 at 13:42
  • increase key_buffer_size 4 times, read_buffer_size and read_rnd_buffer_size 16 times. in my case, select count(*) 1 mil records in 1 seconds.
    – OpenCode
    Jul 7, 2012 at 14:19
  • you have 8G memory - that is too small for inno db. increase it. and in root, change # sysctl -w vm.swappiness=20
    – OpenCode
    Jul 7, 2012 at 14:29
  • @OpenCode how do you figure that 8GB is too small?
    – SuperMykEl
    Jul 7, 2012 at 17:57
  • i change my db servers memory 4G, 8G, 12G due to inno db's poor performance. with 40 mil records tables db box. my experience. See inno db table size and memory size. with 20 mil records db, 8G is too small. With 100 mil record db box, I plan to change memory 12G to 24G.
    – OpenCode
    Jul 8, 2012 at 4:37
0

In my case, using any indexed column in a stupid looking query made the trick:

select count(*) from `macs`; // 5.3s
select count(*) from `macs` WHERE isp_id IS NOT NULL OR isp_id IS NULL; // 420ms

Would be great if anyone could explain this...?

1
  • I think even if you ran the first query twice without making any changes, it would be faster (due to some internal caching mechanism of mysql)
    – Zolbayar
    Jul 27 at 7:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.