简体   繁体   中英

How can I make this MySQL SELECT + GROUP BY query more efficient?

I have a fairly popular site that's now getting rammed with a lot of traffic, and I've been informed by my webhost that the following query is taking up to 2 seconds to run. My MySQL skills aren't that great, so I'm sure I'm doing something wrong, but I'm not sure what could be done to improve it.

For simplicity's sake, assume live_blueprints is a table with four fields:

  • isSolved [tinyint(1)]
  • levelSlug [varchar(128)]
  • solution [varchar(255)]
  • trackCount [mediumint(7)]

I realize using a string (levelSlug) instead of an int (id) is a probably bad idea, so that's one of the things I'd like to fix. Basically what I'm trying to do with this query is grab the top 49 blueprints with unique solution strings. The live_blueprints table has ~550k rows, and I think that's the main cause of the problem. The way I understand it, is that the way this is written, it'll check all 550k rows, and then group them, and then chop off the top 49 to give to me... I'm just wondering if there's a way I could do this without it having to do so much work on the rows... Perhaps even by creating a second table of just "unique" solutions.

Anyway, here's the query right now:

SELECT * 
  FROM live_blueprints
 WHERE levelSlug = 'someLevelSlug' 
    && isSolved = 1 
GROUP BY solution 
ORDER BY trackCount ASC 
   LIMIT 49

Thanks for whatever help or insight you can provide!

Ok, so to answer some questions:

The only indexes on the table are on id and levelSlug. For starters, I'm going to add an index on solution.

I did an explain, so I think this is what you're looking for, levelID is the index for levelSlug.

id > 1
select_type > SIMPLE
table > live_blueprints
type > ref
possible_keys > levelID
key > levelID
key_len > 386
ref > const
rows > 4407
Extra > Using where; Using temporary; Using filesort

What kind of indexes do you have on your table?

Cause, having an index on solution, solutionCoolness (in that order) should help a bit here.

With the where clauses you could even use an index with the columns levelSlug, isSolved, solution, solutionCoolness in that order to make it a little faster.

Either way, we need to know which indexes you have and it would help to see the explain of the query.

How about adding another column, lets call it ranking its gonna be only for your use. When adding a new solution if it already exists make this column zero, else insert 1 or something else like the track count. This way you can get rid of the group by and just short by the trackcount where the ranking is not 0.

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