我有以下查询,执行时,它只运行几个小时,最终服务器超时。
SELECT l.mloc_id, COUNT( l.tabc_id ) , l.tabc_id, e.establishment_id, e.name, l.LocationName, l.naics, l.cuisine, l.cuisine2, l.service, l.www, l.owner, l.email, l.status
FROM vg_ydrink2.m_loc AS l
JOIN vg_ydk.permit_beverage AS pb ON ( l.tabc_id = pb.permit_code
AND pb.create_date = (
SELECT MAX( create_date )
FROM vg_ydk.permit_beverage
WHERE permit_code = l.tabc_id ) )
JOIN vg_ydk.establishment AS e ON ( pb.establishment_id = e.establishment_id )
GROUP BY l.mloc_id
ORDER BY `l`.`tabc_id` ASC
LIMIT 1 , 30
未报告任何错误。它永远不会完成。
问题在于子选择,因为在删除它时,查询执行没有问题。我也不认为这是导致问题的组函数,因为我已经尝试保留子查询但是删除了MAX,但这也没有用。
有什么想法? 提前谢谢。
***编辑:在使用了create_date索引之后,我意识到由于其值的性质,它的基数一直非常低。因此,查询不会将其视为索引,因此会不断加载太多行。 我已经完全改变了查询,所以这不再是一个问题了。如果有人遇到类似问题,请确保已使用的字段已正确编入索引。如果需要,分析/优化/修复您的桌子。
微米。
答案 0 :(得分:0)
尝试此变体:
select l.mloc_id,
COUNT(l.tabc_id),
l.tabc_id,
e.establishment_id,
e.name,
l.LocationName,
l.naics,
l.cuisine,
l.cuisine2,
l.service,
l.www,
l.owner,
l.email,
l.status
from vg_ydrink2.m_loc as l
inner join (
select permit_code,
max(create_date) as MaxCreateDate
from vg_ydk.permit_beverage
group by permit_code
) pbm on l.tabc_id = pbm.permit_code
inner join vg_ydk.permit_beverage as pb on pbm.permit_code = pb.permit_code
and pbm.MaxCreateDate = pb.create_date
inner join vg_ydk.establishment as e on pb.establishment_id = e.establishment_id
group by l.mloc_id
order by `l`.`tabc_id` asc
LIMIT 1,
30