You are here

Speeding up GROUP BY if you want aproximate results

MySQL Performance Blog - Fri, 07/03/2008 - 7:33am

Doing performance analyzes today I wanted to count how many hits come to the pages which get more than couple of visits per day. We had SQL logs in the database so It was pretty simple query:

  1. SELECT sum(cnt) FROM (SELECT count(*) cnt FROM performance_log_080306 GROUP BY page HAVING cnt>2) pv;

Unfortunately this query ran for over half an hour badly overloaded server and I had to kill it in the end.

The reason for slowness was of course huge temporary table was required (there were about 5 million of distinct pages visited during that day) which resulted in on disk temporary table which as we know quite slow.

Of course it would be possible to allocate more memory to the temporary table or switch to filesort method and get result faster.

I however picked another road which is quite helpful in similar cases - I did not need exact result but approximate figure so I could trick MySQL to do group by a hash of the page instead of page itself:

  1. mysql> SELECT sum(cnt) FROM (SELECT count(*) cnt FROM performance_log_080306 GROUP BY crc32(page) HAVING cnt>3) pv
  2.     -> ;
  3. +----------+
  4. | sum(cnt) |
  5. +----------+
  6. |  1127031 |
  7. +----------+
  8. 1 row IN SET (31.22 sec)

As you can see now it completes in about 30 seconds - quite handy.

Another trick I want to share which I use a lot when I want to analyze data distribution but table is to large is to just limit it to first number of rows:

  1. mysql> SELECT avg(length(page)) FROM (SELECT page FROM performance_log_080306 LIMIT 10000) tmp;
  2. +-------------------+
  3. | avg(length(page)) |
  4. +-------------------+
  5. |           70.0444 |
  6. +-------------------+
  7. 1 row IN SET (0.08 sec)

Again this is not exact value but normally close enough to make a decision.

Entry posted by peter | One comment

Add to: delicious | digg | reddit | netscape | Google Bookmarks