2010-10-14 38 views
3

我從我的Rails應用程序生成以下SQL,它試圖獲取從MySQL有活廣告在市場上的應用程序&所有汽車模型的列表:爲什麼這個SQL生成一個臨時表並且運行速度如此之慢?

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 < NOW() AND pub_end_date > NOW() AND models.manufacturer_id = 50) 
GROUP BY models.id ORDER BY models.name; 

當我運行一個解釋,這是我所得到的:

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 

我不明白爲什麼查詢生成的臨時表/使用文件排序 - 所有引用的按鍵被索引。一直試圖弄清楚這幾天,現在沒有任何意義。

任何幫助非常感謝!

EXPLAIN型號:

+---------------------+-------------+------+-----+---------+----------------+ 
| 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 |    | 
+---------------------+-------------+------+-----+---------+----------------+ 

SHOW個索引模式:

+--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+ 
| 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  |   | 
+--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+ 

模型表格狀態:

+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+ 
| 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 |    |   | 
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+ 

EXPLAIN ADS

+------------------+--------------------------+------+-----+---------------------+----------------+ 
| 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    |    | 
+------------------+--------------------------+------+-----+---------------------+----------------+ 

SHOW_TABLE STATUS FROM concept_development WHERE NAME LIKE'ads';

+------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ 
| 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 |   | 
+------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ 

SHOW個索引ADS

+-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+ 
| 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  |   | 
+-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+ 

EXPLAIN汽車

+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+ 
| 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  |    | 
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+ 

SHOW TABLE STATUS FROM concept_development WHERE NAME LIKE '汽車'

+-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ 
| 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 |   | 
+-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+ 

從汽車節目索引;

+-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ 
| 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  |   | 
+-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ 
+1

'models.name'是否也被索引? – 2010-10-14 19:02:44

+0

剛剛添加它,同樣的問題,但:-( – Jason 2010-10-14 19:13:15

+0

我知道這可能是一個痛苦,但我們確實需要更多的信息來幫助解決這個問題。請看這裏http://pastie.org/1221602 – 2010-10-14 19:16:59

回答

0

您對pub_end_date一個指標,但不是在pub_start_date和你的WHERE子句引用了兩個。

看起來它並未使用pub_end_date索引,但這可能是因爲它也需要檢查pub_start_date。

+0

嗨,Alan,剛剛在pub_start_date上添加了一個索引 - 沒有喜悅我很害怕 – Jason 2010-10-18 16:26:38

2

從MySQL文檔:

Temporary tables can be created under conditions such as these: 
    * If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue, a temporary table is created. 

http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html

+0

呵呵,文件的排序可能來自ORDER BY子句,並且沒有models.name的索引 – mellowsoon 2010-10-17 22:57:57

+0

有沒有辦法重寫那麼這樣就不會創建tmp表?我剛剛在models.name上添加了一個索引 – Jason 2010-10-18 16:32:16

+0

我可以嘗試使用一些不同的索引,但我需要查看錶格模式。嘗試使用「SHOW CREATE TABLE模型」; (也可以重複其他2個表格)。這使我更容易重新創建表格。 – mellowsoon 2010-10-18 17:15:12

2

更改所有文本列VARCHAR。如果您需要將它們保持爲「文本」,則需要將模式雪花化並從此查詢中排除說明表。

如果任何表中的任何一列是文本或blob,MySQL會自動創建一個磁盤上的臨時表,而不是內存中的臨時表。臨時表本身並不會殺死你,而是將它寫入磁盤。

http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html

有些條件防​​止使用在內存中的臨時表中的,在這種情況下,服務器使用一個磁盤上的表來代替:

  • 一個BLOB或TEXT列的存在在表
0

這不是解釋爲什麼但你如何重寫你的查詢不使用組?我想你只是加入這些表格,以確保存在一個感興趣的廣告。因此,如何:

SELECT `models`.* 
FROM `models` 
WHERE models.manufacturer_id = 50 
AND EXISTS ( SELECT * FROM `autos` 
       INNER JOIN `ads` ON `ads`.id = `autos`.ad_id 
       WHERE autos.model_id = models.id 
       AND ads.ad_status_id = 4 
       AND ads.pub_start_date < NOW() 
       AND ads.pub_end_date > NOW() 
      ) 
ORDER BY models.name; 

性能問題可能進行相關的group by,在這種情況下,這將提高性能。

如果你可以在mysql中做到這一點(如果它在你的邊緣情況下工作的話),也許它會更好一點,and NOW() between ads.pub_start_date and ads.pub_end_date

相關問題