I'm currently fighting with MySQL. The following takes too long:
mysql> SELECT COUNT(id) FROM q_archive; +-----------+ | COUNT(id) | +-----------+ | 2738048 | +-----------+ 1 row in set (17.95 sec)
I would like it to take significantly less time, even with memcached being in use it gets hit too often. I've added an index to the table - but I didn't expect that to help, and I wasn't disappointed.
Ho hum.
Maybe another case where flat-files are best. Sure counting them would take a while, but once I've counted them I can just `cat sum`.
This is probably a case where tweaking memory of MySQL would help. But I'm fairly certain if I start messing with that I'll get into trouble with other parts of my site.
ObFilm: The Evil Dead
Something is horribly wrong with your instance. Here is a comparable select from a database of mine.
You could be right Chris.
The system is a little heavily loaded, but even so I'd like it to be faster. I'm just a little unsure where to start..
I am not sure that would solve your problem, since you are using count(id)
A collection of triggers might be a good solution. Thanks.
I'm going to tweak the memory settings a little first to see if that helps improve performance generally - it seems that many operations are taking longer than they should, but this is a pathological case.
Obviously something is screwed up but I'm not too familiar with these kind of problems...
Scott: COUNT(1) is about the same, and what I'm doing. I know that is faster than COUNT(*).
David - I didn't realize the row-count was generally available in the meta-data, though I guess I might have guessed, that suggests something more serious is wrong with my database setup.
Would you try if anything changes in your case?
Giuseppe - I'm happy to try both ways. Once I've finished running " optimize table q_archive;" I'll post what the difference is.
I'm not sure if the speedup there is real though - it seems to be more a caching artifact.
Regarding your comment on using `cat sum', though, notice that you _can_ do something like that with a database; you only need to have a "static view", in more robust databases, or a real table that is updated from time to time, or by a trigger on every insert/delete, so no real gain in dropping the database for flat files there.
Rob: I've converted from InnoDB to MyISAM and the performance has just improved considerably.
I will have to read more about the pros/cons of the two table types, but thanks very much for giving me the prod.
(The table in question gets about 50,000 inserts 4 times an hour. But otherwise is queried N times a second - so read performance, and the COUNT(id) query is more important to me than then insert performance.)
Sounds like MyISAM is more your cup of tea though.
I can't remember if it was as accurate though. Try it and see.