![](/img/trans.png)
[英]Brain-Dead MySQL Select Optimization (Using Temporary, Using Filesort)
[英]MYSQL optimization for Using temporary; Using filesort!!! -> HELP
伙計我現在正在嘗試一段時間,我無法優化使用臨時; 使用我的EXPLAIN查詢中的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時......我明白了:
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)
我嘗試了一些KEY但沒有結果:例如我為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)
任何幫助,我很感激!
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)
您需要使用強制索引 - 在這里您可以找到更多信息 。 但是在你的情況下它沒有多大幫助,因為你為連接和組使用不同的索引,所以mysql沒有機會“重用”它們。
好的,它改進但現在查詢掃描所有行: - 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)
據我所知,我必須做一個正確的指數? 但是哪一列/列?
只是為了笑容......改變
SELECT STRAIGHT_JOIN(您的查詢的其余部分)
通過使子類別表小,優化器引擎將嘗試使用它作為查詢的基礎而不是查詢中的第一個表...這個關鍵字“STRAIGHT_JOIN”在過去幫助我做了14個gov't查詢從30多個小時到完成不到2個小時的過程,將超過100個記錄加入到15個以上的查找表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.