I've got a number of queries like this...
SELECT colA, colB, colC, colD FROM table1
LEFT JOIN (
SELECT colD FROM table2 WHERE colE = 3225
) AS subquery
ON colD = colA ;
Which get listed in my server's "log-queries-not-using-indexes" log.
The EXPLAIN looks like this...
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 |
Is there any way i can restructure the Query, or are there any new indexes I should add to improve this?
FYI the Tables are structured like this...
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 ;
Thanks in advance,
Yours
phil
I would try the following:
SELECT ColA, ColB, ColC, ColD
FROM
table1
LEFT JOIN table2
ON ColE = 3225
AND ColD = ColA
Also, try creating an index that covers both ColE
and ColD
on table2
:
CREATE INDEX `IDX_table2` on `table2` (`colE` ASC, `colD` ASC);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.