Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this SQL generating a temporary table and running so slow?
    primarykey
    data
    text
    <p>I have the following SQL generated from my Rails app, it is trying to get a list of all auto models that have live adverts in a marketplace app &amp; from mysql:</p> <pre><code>SELECT `models`.* FROM `models` INNER JOIN `autos` ON autos.model_id = models.id INNER JOIN `ads` ON `ads`.id = `autos`.ad_id WHERE (ads.ad_status_id = 4 AND pub_start_date &lt; NOW() AND pub_end_date &gt; NOW() AND models.manufacturer_id = 50 ) GROUP BY models.id ORDER BY models.name; </code></pre> <p>When I run an explain, this is what I get:</p> <pre><code>Id 1 1 1 Select Type SIMPLE SIMPLE SIMPLE Table models autos ads Type ref ref eq_ref Possible Keys PRIMARY,manufacturer_id model_id,ad_id PRIMARY,quick_search,ad_status_id Key manufacturer_id model_id PRIMARY Key Length 5 4 4 Ref const concept_development.models.id concept_development.autos.ad_id Rows 70 205 1 Extra Using where; Using temporary; Using filesort Using where Using where </code></pre> <p>I cannot understand why the query is generating a temporary table / using file-sort - all of the referenced keys are indexes. Been trying to figure this out for a few days now and getting nowhere.</p> <p>Any help is very much appreciated! </p> <p>EXPLAIN models:</p> <pre><code>+---------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(32) | YES | | NULL | | | manufacturer_id | int(11) | YES | MUL | NULL | | | vehicle_category_id | int(11) | NO | MUL | 1 | | | synonym_names | longtext | YES | | NULL | | +---------------------+-------------+------+-----+---------+----------------+ </code></pre> <p>SHOW INDEXES FROM models:</p> <pre><code>+--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+ | models | 0 | PRIMARY | 1 | id | A | 2261 | NULL | NULL | | BTREE | | | models | 1 | manufacturer_id | 1 | manufacturer_id | A | 205 | NULL | NULL | YES | BTREE | | | models | 1 | vehicle_category_id | 1 | vehicle_category_id | A | 7 | NULL | NULL | | BTREE | | +--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+ </code></pre> <p>MODEL TABLE STATUS:</p> <pre><code>+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+ | models | MyISAM | 10 | Dynamic | 2261 | 26 | 61000 | 281474976710655 | 84992 | 0 | 2751 | 2010-09-28 18:42:45 | 2010-09-28 18:42:45 | 2010-09-28 18:44:00 | latin1_swedish_ci | NULL | | | +--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+ </code></pre> <p>EXPLAIN ADS</p> <pre><code>+------------------+--------------------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------------------+------+-----+---------------------+----------------+ | id | int(10) | NO | PRI | NULL | auto_increment | | fp_urn | int(10) | NO | MUL | 0 | | | user_id | int(10) | NO | MUL | 0 | | | ad_status_id | int(3) unsigned | NO | MUL | 1 | | | style_id | int(10) | NO | | 3 | | | search_tags | varchar(255) | YES | | NULL | | | title | varchar(255) | NO | | | | | description | text | YES | | NULL | | | currency | enum('EUR','GBP') | NO | | EUR | | | price | decimal(8,2) | NO | MUL | 0.00 | | | proposal_type | enum('Offered','Wanted') | NO | | Offered | | | category_id | int(10) | YES | | 0 | | | contact | varchar(50) | NO | MUL | | | | area_id | int(10) | NO | | 0 | | | origin_id | int(10) | NO | | 0 | | | reject_reason_id | int(3) | NO | | 0 | | | date_created | timestamp | NO | | 0000-00-00 00:00:00 | | | last_modified | timestamp | NO | | CURRENT_TIMESTAMP | | | pub_start_date | datetime | YES | | 0000-00-00 00:00:00 | | | pub_end_date | datetime | YES | | 0000-00-00 00:00:00 | | | bumped_up_date | datetime | YES | | 0000-00-00 00:00:00 | | | state | smallint(6) | YES | | NULL | | | eproofed | tinyint(1) | NO | | 0 | | | is_featured | int(1) | NO | | 0 | | | num_featured_imp | int(10) | YES | | 0 | | | num_direct_imp | int(10) | YES | | 0 | | | is_top_listed | int(1) | NO | | 0 | | | delta | tinyint(1) | NO | | 0 | | | ext_ref_id | varchar(50) | YES | | NULL | | | email_seller | tinyint(1) | YES | | 1 | | | sort_by | int(10) | YES | | 8 | | | permalink | varchar(500) | YES | | NULL | | | external_url | varchar(255) | YES | | NULL | | +------------------+--------------------------+------+-----+---------------------+----------------+ </code></pre> <p>SHOW TABLE STATUS FROM concept_development WHERE NAME LIKE 'ads';</p> <pre><code>+------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ | ads | InnoDB | 10 | Compact | 656350 | 232 | 152748032 | 0 | 87736320 | 340787200 | 1148382 | 2010-09-29 09:55:46 | NULL | NULL | utf8_general_ci | NULL | checksum=1 delay_key_write=1 row_format=DYNAMIC | | +------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ </code></pre> <p>SHOW INDEXES FROM ADS</p> <pre><code>+-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+ | ads | 0 | PRIMARY | 1 | id | A | 521391 | NULL | NULL | | BTREE | | | ads | 1 | NewIndex1 | 1 | ad_status_id | A | 15 | NULL | NULL | | BTREE | | | ads | 1 | NewIndex1 | 2 | pub_end_date | A | 260695 | NULL | NULL | YES | BTREE | | | ads | 1 | NewIndex1 | 3 | category_id | A | 521391 | NULL | NULL | YES | BTREE | | | ads | 1 | NewIndex1 | 4 | style_id | A | 521391 | NULL | NULL | | BTREE | | | ads | 1 | NewIndex2 | 1 | user_id | A | 130347 | NULL | NULL | | BTREE | | | ads | 1 | NewIndex3 | 1 | price | A | 7667 | NULL | NULL | | BTREE | | | ads | 1 | contact | 1 | contact | A | 260695 | NULL | NULL | | BTREE | | | ads | 1 | fp_urn | 1 | fp_urn | A | 521391 | NULL | NULL | | BTREE | | +-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+ </code></pre> <p>EXPLAIN autos</p> <pre><code>+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+ | id | int(10) | NO | PRI | NULL | auto_increment | | ad_id | int(10) | YES | MUL | NULL | | | style_id | int(10) | YES | MUL | NULL | | | manufacturer_id | int(10) | NO | MUL | NULL | | | model_id | int(10) | NO | MUL | NULL | | | registration | varchar(10) | YES | | NULL | | | year | int(4) | YES | | NULL | | | fuel_type | enum('Petrol','Diesel') | NO | | Petrol | | | colour | varchar(75) | YES | | NULL | | | mileage | varchar(25) | NO | | Not Entered | | | mileage_units | enum('mls','kms') | NO | | mls | | | num_doors | varchar(25) | NO | | Not Entered | | | num_owners | int(2) | YES | | NULL | | | engine_size | varchar(10) | YES | | NULL | | | transmission_type | enum('Manual','Automatic') | NO | | Manual | | | body_type | enum('Saloon','Hatchback') | NO | | Saloon | | | condition | varchar(75) | NO | | NA | | | extra_features | text | YES | | NULL | | | tax_expiry | varchar(7) | YES | | NULL | | | nct_expiry | varchar(7) | YES | | NULL | | | variation | text | YES | | NULL | | | tax_class | enum('Agricultural','Bus') | NO | | Private | | | co2 | int(9) | YES | | NULL | | +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+ </code></pre> <p>SHOW TABLE STATUS FROM concept_development WHERE NAME LIKE 'autos'</p> <pre><code>+-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ | autos | InnoDB | 10 | Compact | 196168 | 136 | 26804224 | 0 | 26279936 | 340787200 | 485405 | 2010-09-17 22:09:45 | NULL | NULL | utf8_general_ci | NULL | checksum=1 delay_key_write=1 row_format=DYNAMIC | | +-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ </code></pre> <p>show indexes from autos;</p> <pre><code>+-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ | autos | 0 | PRIMARY | 1 | id | A | 294937 | NULL | NULL | | BTREE | | | autos | 1 | ad_id | 1 | ad_id | A | 294937 | NULL | NULL | YES | BTREE | | | autos | 1 | style_id | 1 | style_id | A | 10 | NULL | NULL | YES | BTREE | | | autos | 1 | manufacturer_id | 1 | manufacturer_id | A | 194 | NULL | NULL | | BTREE | | | autos | 1 | model_id | 1 | model_id | A | 830 | NULL | NULL | | BTREE | | +-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload