我有很多像这样的问题......
SELECT colA, colB, colC, colD FROM table1
LEFT JOIN (
SELECT colD FROM table2 WHERE colE = 3225
) AS subquery
ON colD = colA ;
在我的服务器的“log-queries-not-using-indexes”日志中列出了哪些。
EXPLAIN看起来像这样......
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
---+-------------+------------+-------+---------------+---------+---------+------+------+-------------
1 | PRIMARY | table1 | range | PRIMARY | PRIMARY | 3 | NULL | 58 | Using where
1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 1 |
2 | DERIVED | table2 | const | PRIMARY | PRIMARY | 3 | | 1 |
我有什么办法可以重构Query,还是我应该添加任何新的索引来改进这个?
仅供参考表格的结构如下......
CREATE TABLE `table1` (
`colA` MEDIUMINT( 9 ) NOT NULL ,
`colB` VARCHAR( 100 ) DEFAULT NULL ,
`colC` VARCHAR( 6 ) DEFAULT NULL ,
some more columns removed to simplify things...
PRIMARY KEY ( `colA` ) ,
KEY `colB` ( `colB` ) ,
KEY `colC` ( `colC` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 ;
CREATE TABLE `table2` (
`colE` mediumint( 9 ) NOT NULL auto_increment ,
`colD` mediumint( 9 ) default '0',
some more columns removed to simplify things...
PRIMARY KEY ( `colE` ) ,
KEY `colD` ( `colD` )
) ENGINE = MyISAM DEFAULT CHARSET = latin1 ;
提前致谢,
此致
菲尔
答案 0 :(得分:0)
我会尝试以下方法:
SELECT ColA, ColB, ColC, ColD
FROM
table1
LEFT JOIN table2
ON ColE = 3225
AND ColD = ColA
另外,尝试在ColE
上创建涵盖ColD
和table2
的索引:
CREATE INDEX `IDX_table2` on `table2` (`colE` ASC, `colD` ASC);