Home » Performance » MySQL » Anyway to avoid temp table for this GROUP BY?
Anyway to avoid temp table for this GROUP BY? [message #3020] Mon, 28 April 2008 12:13 Go to next message
GiantCranes  is currently offline GiantCranes
Messages: 1
Registered: April 2008
Junior Member
Hi,

I have a vehicles and tags table. The following query returns vehicle_ids which have certain tags_ids. Is it possible to avoid the temp table? The query is currently taking ~400ms against 40k vehicles.

EXPLAIN SELECT id FROM vehicles, tags_vehicles
WHERE tags_vehicles.vehicle_id = vehicles.id
AND tags_vehicles.tag_id IN (10,92,6,26)
 GROUP BY vehicles.id HAVING COUNT(vehicles.id) = 4 ORDER BY vehicles.renewed_on DESC LIMIT 0, 52 


+----+-------------+---------------+--------+--------------------------------------------------------------------------+-------------------------+---------+----------------------------------------------+-------+-----------------------------------------------------------+
| id | select_type | table         | type   | possible_keys                                                            | key                     | key_len | ref                                          | rows  | Extra                                                     |
+----+-------------+---------------+--------+--------------------------------------------------------------------------+-------------------------+---------+----------------------------------------------+-------+-----------------------------------------------------------+
|  1 | SIMPLE      | tags_vehicles | range  | ix_vehicle_id_tag_id,fk_tags_vehicles_tag_id,fk_tags_vehicles_vehicle_id | fk_tags_vehicles_tag_id | 4       | NULL                                         | 40644 | Using where; Using index; Using temporary; Using filesort | 
|  1 | SIMPLE      | vehicles      | eq_ref | PRIMARY                                                                  | PRIMARY                 | 4       | carlist_development.tags_vehicles.vehicle_id |     1 |                                                           | 
+----+-------------+---------------+--------+--------------------------------------------------------------------------+-------------------------+---------+----------------------------------------------+-------+-----------------------------------------------------------+
Re: Anyway to avoid temp table for this GROUP BY? [message #3046 is a reply to message #3020 ] Tue, 06 May 2008 00:51 Go to previous message
debug  is currently offline debug
Messages: 128
Registered: March 2008
Senior Member

Can you please post SHOW CREATE TABLE for vehicles and tags_vehicles tables?


Mikhail Solovyev, MySQL Performance Expert
MySQL Performance Blog
MySQL Consulting
Previous Topic:MutiMaster for replication
Next Topic:Sphinx - only for string based search?
Goto Forum:
  



Current Time: Fri Jan 9 01:26:57 EST 2009

Total time taken to generate the page: 0.16229 seconds
.:: Contact :: Home :: MySQL Support by Percona.com ::.

Powered by: FUDforum 2.7.5.
Copyright ©2001-2006 FUD Forum Bulletin Board Software

MySQL Performance | Forum authority Badge