MySQL查询运行时间太长(简单LEFT JOIN)

时间:2017-04-29 07:35:55

标签: mysql join runtime

我没有明显的理由遇到了以下问题。我有2张桌子:

isrc(约23k行):

Field           Type           Null    Default    Extra
isrc_ID         int(11)         NO      PRI     auto_increment
ISRC            varchar(25)     NO      UNI     
Track_Name      varchar(255)    YES         
Track_Time      time            YES     00:00:00    
ArtistName      varchar(255)    YES         
TitleVersion    varchar(255)    YES         
Date_update     timestamp       NO      CURRENT_TIMESTAMP   on update CURRENT_TIMESTAMP

isrc_performer(901行):

Field               Type           Null    Default    Extra
Perf_ID             int(11)         NO      PRI     auto_increment
isrc_ID             int(11)         NO          
PerformerName       varchar(255)    YES         
PerformerCategory   varchar(255)    YES         
PerformerRole       varchar(255)    YES         
date_update         timestamp       NO      CURRENT_TIMESTAMP   on update CURRENT_TIMESTAMP

以下简单查询运行时间过长:

SELECT          
isrc.ISRC,  
isrc.Track_Name,    
isrc.ArtistName,
isrc.TitleVersion,
isrc.Track_Time,
`isrc_performer`.`PerformerName` ,
`isrc_performer`.`PerformerCategory` ,
`isrc_performer`.`PerformerRole` ,
`isrc`.`isrc_ID`,
`isrc_performer`.`Perf_ID`

FROM `isrc`

LEFT JOIN `isrc_performer` ON (isrc_performer.isrc_ID = isrc.isrc_ID)

ORDER BY   isrc_ID     desc LIMIT 0 , 10

运行时间 - 5.117秒

对每个表的单独请求:

SELECT 
isrc.ISRC,
isrc.Track_Name,
isrc.ArtistName,
isrc.TitleVersion,
isrc.Track_Time

FROM `isrc`

ORDER BY   isrc_ID     desc LIMIT 0 , 10

运行时间 - 0.000秒

SELECT      
`isrc_performer`.`PerformerName` ,
`isrc_performer`.`PerformerCategory` ,
`isrc_performer`.`PerformerRole`,
`isrc_performer`.`Perf_ID`

FROM `isrc_performer`

ORDER BY   Perf_ID     desc LIMIT 0 , 10

运行时间 - 0.000秒

有新鲜眼光的人能指出正确的方向吗?为什么会这样?

1 个答案:

答案 0 :(得分:1)

CREATE TRIGGER checkzeros BEFORE UPDATE ON Sales FOR EACH ROW BEGIN IF NEW.SalesQty < 1 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Quantity must not be below 1'; END IF; END// 表添加索引:

isrc_performer

否则,它必须执行表的完整扫描,以便在加入时找到匹配ALTER TABLE isrc_performer ADD INDEX (isrc_ID); 的行。