2011-01-20 111 views
2

我想嘗試一段時間的夥計們,我無法設法優化使用臨時;從我的EXPLAIN查詢中使用filesort。 行,所以,我有這個疑問:使用MYSQL進行臨時優化;使用filesort !!! - >幫助

SELECT a.adid, a.adtitle, a.addesc, scat.subcatname 
FROM ads a 
INNER JOIN cities ct ON a.cityid = ct.cityid 
INNER JOIN subcats scat ON a.subcatid = scat.subcatid 
INNER JOIN cats cat ON scat.catid = cat.catid 
WHERE a.enabled = '1' 
AND a.verified ='1' 
GROUP BY a.adid 
ORDER BY a.createdon DESC 
LIMIT 16; 

當我請務必解釋一下...我得到這個:

EXPLAIN SELECT a.adid, a.adtitle, a.addesc, scat.subcatname 
FROM ads a 
INNER JOIN cities ct ON a.cityid = ct.cityid 
INNER JOIN subcats scat ON a.subcatid = scat.subcatid 
INNER JOIN cats cat ON scat.catid = cat.catid 
WHERE a.enabled = '1' 
AND a.verified ='1' 
GROUP BY a.adid 
ORDER BY a.createdon DESC LIMIT 16; 

+----+-------------+-------+--------+----------------------------------+----------+---------+--------------------------------+------+---------------------------------+ 
| id | select_type | table | type | possible_keys     | key  | key_len | ref       | rows | Extra       | 
+----+-------------+-------+--------+----------------------------------+----------+---------+--------------------------------+------+---------------------------------+ 
| 1 | SIMPLE  | cat | system | PRIMARY       | NULL  | NULL | NULL       | 1 | Using temporary; Using filesort | 
| 1 | SIMPLE  | scat | ref | PRIMARY,catid     | catid | 2  | const       | 7 |         | 
| 1 | SIMPLE  | a  | ref | subcatid,cityid,verified,enabled | subcatid | 2  | bakecai_incontri.scat.subcatid | 954 | Using where      | 
| 1 | SIMPLE  | ct | eq_ref | PRIMARY       | PRIMARY | 2  | bakecai_incontri.a.cityid  | 1 | Using index      | 
+----+-------------+-------+--------+----------------------------------+----------+---------+--------------------------------+------+---------------------------------+ 
4 rows in set (0.00 sec) 

我已經嘗試了一些關鍵的,但沒有結果:比如我創建索引createdon和adid但是mysql不會使用它。

這裏是我的表中的鍵:

SHOW INDEX FROM ads; 

+-------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
|Table| Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| ads |   0 | PRIMARY   |   1 | adid  | A   |  8592 |  NULL | NULL |  | BTREE  |   | 
| ads |   1 | subcatid   |   1 | subcatid | A   |   9 |  NULL | NULL |  | BTREE  |   | 
| ads |   1 | cityid    |   1 | cityid  | A   |   103 |  NULL | NULL |  | BTREE  |   | 
| ads |   1 | verified   |   1 | verified | A   |   2 |  NULL | NULL | YES | BTREE  |   | 
| ads |   1 | enabled   |   1 | enabled  | A   |   2 |  NULL | NULL | YES | BTREE  |   | 
| ads |   1 | idx_createdon_adid |   1 | createdon | A   |  8592 |  NULL | NULL |  | BTREE  |   | 
| ads |   1 | idx_createdon_adid |   2 | adid  | A   |  8592 |  NULL | NULL |  | BTREE  |   | 
| ads |   1 | srch_text   |   1 | adtitle  | NULL  |   1 |  NULL | NULL | YES | FULLTEXT |   | 
| ads |   1 | srch_text   |   2 | addesc  | NULL  |   1 |  NULL | NULL | YES | FULLTEXT |   | 
| ads |   1 | srch_text   |   3 | nome  | NULL  |   1 |  NULL | NULL | YES | FULLTEXT |   | 
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
10 rows in set (0.00 sec) 

下面是表的一些簡要描述:

DESCRIBE ads 
    -> ; 
+-----------+----------------------+------+-----+---------------------+----------------+ 
| Field  | Type     | Null | Key | Default    | Extra   | 
+-----------+----------------------+------+-----+---------------------+----------------+ 
| adid  | int(10) unsigned  | NO | PRI | NULL    | auto_increment | 
| nome  | varchar(255)   | YES |  | NULL    |    | 
| eta  | varchar(255)   | YES |  | NULL    |    | 
| adtitle | varchar(100)   | YES | MUL | NULL    |    | 
| addesc | text     | YES |  | NULL    |    | 
| email  | varchar(50)   | YES |  | NULL    |    | 
| phone  | varchar(255)   | YES |  | NULL    |    | 
| showemail | enum('0','1','2') | YES |  | NULL    |    | 
| code  | varchar(35)   | YES |  | NULL    |    | 
| first  | varchar(255)   | YES |  | NULL    |    | 
| cityid | smallint(5) unsigned | NO | MUL | 0     |    | 
| subcatid | smallint(5) unsigned | NO | MUL | 0     |    | 
| price  | decimal(10,2)  | NO |  | 0.00    |    | 
| hits  | int(10) unsigned  | NO |  | 0     |    | 
| ip  | varchar(15)   | YES |  | NULL    |    | 
| link  | varchar(255)   | YES |  | http://    |    | 
| verified | enum('0','1')  | YES | MUL | NULL    |    | 
| abused | int(10) unsigned  | NO |  | 0     |    | 
| enabled | enum('0','1')  | YES | MUL | NULL    |    | 
| createdon | datetime    | NO | MUL | 0000-00-00 00:00:00 |    | 
| expireson | datetime    | NO |  | 0000-00-00 00:00:00 |    | 
| timestamp | timestamp   | NO |  | CURRENT_TIMESTAMP |    | 
+-----------+----------------------+------+-----+---------------------+----------------+ 
22 rows in set (0.02 sec) 

DESCRIBE cities 
    -> ; 
+-----------+----------------------+------+-----+-------------------+----------------+ 
| Field  | Type     | Null | Key | Default   | Extra   | 
+-----------+----------------------+------+-----+-------------------+----------------+ 
| cityid | smallint(5) unsigned | NO | PRI | NULL    | auto_increment | 
| cityname | varchar(50)   | YES |  | NULL    |    | 
| regionid | smallint(5) unsigned | NO | MUL | 0     |    | 
| pos  | smallint(5) unsigned | NO | MUL | 0     |    | 
| enabled | enum('0','1')  | YES | MUL | NULL    |    | 
| timestamp | timestamp   | NO |  | CURRENT_TIMESTAMP |    | 
+-----------+----------------------+------+-----+-------------------+----------------+ 
6 rows in set (0.00 sec) 

DESCRIBE cats 
    -> ; 
+-----------+----------------------+------+-----+-------------------+----------------+ 
| Field  | Type     | Null | Key | Default   | Extra   | 
+-----------+----------------------+------+-----+-------------------+----------------+ 
| catid  | smallint(5) unsigned | NO | PRI | NULL    | auto_increment | 
| catname | varchar(50)   | YES |  | NULL    |    | 
| pos  | smallint(5) unsigned | NO |  | 0     |    | 
| enabled | enum('0','1')  | YES | MUL | NULL    |    | 
| timestamp | timestamp   | NO |  | CURRENT_TIMESTAMP |    | 
+-----------+----------------------+------+-----+-------------------+----------------+ 
5 rows in set (0.00 sec) 

任何幫助,我很感激!

回答

1
SELECT a.adid, a.adtitle, a.addesc, scat.subcatname 
FROM ads a 
INNER JOIN 
     subcats scat 
ON  a.subcatid = scat.subcatid 
WHERE a.enabled = '1' 
     AND a.verified ='1' 
ORDER BY a.createdon 
     DESC 
LIMIT 16 

刪除不需要的連接,並擺脫不需要的GROUP BY

爲了擺脫filesort,創建下列指標:

CREATE INDEX ix_ads_e_v_created ON ads (enabled, verified, createdon) 
+0

確定它改進了,但是...我還有一個小問題,現在查詢正在掃描所有行 – user583012 2011-01-20 14:26:34

0

你需要玩弄迫使指數 - here you will find more information。然而在你的情況下,它並沒有什麼幫助,因爲你使用不同的索引來進行連接和組合,所以mysql沒有機會「重用」它們。

0

OK,它改善,但現在查詢掃描所有的行: --hmm我想這是因爲所有的線已經啓用並驗證等於1 ......從什麼

EXPLAIN SELECT a.adid, a.adtitle, a.addesc, scat.subcatname 
    -> FROM ads a 
    -> INNER JOIN subcats scat 
    -> ON a.subcatid = scat.subcatid 
    -> WHERE a.enabled = '1' 
    -> AND a.verified ='1' 
    -> ORDER BY a.createdon DESC 
    -> LIMIT 16; 
+----+-------------+-------+--------+----------------------------------------------+--------------------+---------+-----------------------------+------+-------------+ 
| id | select_type | table | type | possible_keys        | key    | key_len | ref       | rows | Extra  | 
+----+-------------+-------+--------+----------------------------------------------+--------------------+---------+-----------------------------+------+-------------+ 
| 1 | SIMPLE  | a  | ref | subcatid,verified,enabled,ix_ads_e_v_created | ix_ads_e_v_created | 4  | const,const     | 8485 | Using where | 
| 1 | SIMPLE  | scat | eq_ref | PRIMARY          | PRIMARY   | 2  | bakecai_incontri.a.subcatid | 1 |    | 
+----+-------------+-------+--------+----------------------------------------------+--------------------+---------+-----------------------------+------+-------------+ 
2 rows in set (0.00 sec) 

我知道我必須編制索引嗎?但在哪些列/列?

+0

查詢沒有掃描所有行:它正在使用具有ref參數的索引並輸出索引中的記錄訂單,沒有`filesort`。此外,請更新您的原始問題,不要將更新發布爲答案。謝謝。 – Quassnoi 2011-01-20 14:38:45

0

只是爲了笑容......改變

SELECT STRAIGHT_JOIN(查詢的其餘部分)

通過讓子類表是小,優化引擎將嘗試使用它作爲查詢和NOT的基礎查詢中的第一個表...這一個關鍵詞「STRAIGHT_JOIN」幫助我在過去30多個小時內將超過14百萬條記錄加入到15個查找表中,以將這個過程掛起,持續時間不超過2小時去完成。

相關問題