简体   繁体   中英

slow query using avg in mysql

I have this table:

 CREATE TABLE `table1` (
  `object` varchar(255) NOT NULL,
  `score` decimal(10,3) NOT NULL,
  `timestamp` datetime NOT NULL
  KEY `ex` (`object`,`score`,`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

with 9.1 million rows and I am running the following query:

SELECT `object`, `timestamp`, AVG(score) as avgs
  from `table1`
 where timestamp >= '2011-12-14'
   AND timestamp <= '2011-12-13'
 group by `object`
 order by `avgs` ASC limit 100;

The dates come from user input. The query takes 6-10 seconds, depending on the range of dates. The run time seems to increase with the number of rows

What can I do to improve this?

I have tried:

  • fiddling with indexes (brought query time down from max 13sec to max 10sec)
  • moving storage to fast SAN (brought query time down by around 0.1sec, regardless of parameters).

The CPU and memory load on the server doesn't appear to be too high when the query is running.

The reason why fast SAN is perform much better
is because your query require copy to temporary table,
and need file-sort for a large results set.

You have five nasty factors.

  • range query
  • group-by
  • sorting
  • varchar 255 for object
  • a wrong index

Break-down timestamp to two fields,

date, time

Build another reference table for object,
so, you use integer, such as object_id (instead of varchar 255) to represent object

Rebuilt the index on

date (date type), object_id

Change the query to

where date IN('2011-12-13', '2011-12-14', ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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