I'm trying to solve a problem with latency on a to a mysql-5.0 db.
- The query itself is extremely simple:
SELECT SUM(items) FROM tbl WHERE col = 'val'
- There's an index on
col
and there are not more than 10000 values to sum in the worst case (mean ofcount(items)
for all values ofcol
would be around 10). - The table has up to 2M rows.
- The query is run frequently enough that sometimes the execution time goes up to 10s, although 99% of them take << 1s
- The query is not really cachable - in almost every case, each query like this one will be followed by an insert to that table in the next minute and showing old values is out of question (billing information).
- keys are good enough - ~100% hits
The result I'm looking for is every single query < 1s. Are there any ways to improve the select time without changes to the table? Alternatively, are there any interesting changes that would help to resolve the problem? I thought about simply having a table where the current sum is updated for every col right after every insert - but maybe there are better ways to do it?