我有一个包含 38k 行的表,我使用此查询将 items 表中的项目 ID 与 posts_domains 表中的项目 ID 进行比较。
select * from `items`
where `items`.`source_id` = 2 and `items`.`source_id` is not null
and not exists (select *
from `posted_domains`
where `posted_domains`.`item_id` = `items`.`id` and `domain_id` = 1)
order by `item_created_at` asc limit 1
此查询花费了 8 秒。我不知道是我的查询有问题还是我的 mysql 配置错误。该查询由 Laravel 关系生成,例如
$items->doesntHave('posted', 'and', function ($q) use ($domain) {
$q->where('domain_id', $domain->id);
});
最佳答案
关联子查询可能相当慢(因为它们经常重复执行,对于外部查询中的每一行执行一次),这可能会更快。
select *
from `items`
where `items`.`source_id` = 2
and `items`.`source_id` is not null
and item_id not in (
select DISTINCT item_id
from `posted_domains`
where `domain_id` = 1)
order by `item_created_at` asc
limit 1
我说可能是因为MySQL中where中的子查询也相当慢。
这个 LEFT JOIN 可能是最快的。
select *
from `items`
LEFT JOIN (
select DISTINCT item_id
from `posted_domains`
where `domain_id` = 1) AS subQ
ON items.item_id = subQ.item_id
where `items`.`source_id` = 2
and `items`.`source_id` is not null
and subQ.item_id is null
order by `item_created_at` asc
limit 1;
由于这是一个无匹配场景,因此从技术上讲它甚至不需要是子查询;并且可能比直接左连接更快,但这将取决于索引,也可能取决于实际数据值。
关于Mysql长执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281866/