[英]How to optimize the inner query mysql
insert into abc(id,item_id,item_type,l_id,c_id)
Select '',o.id,'Open',pl.id,'67' from pls pl, opens o where o.id IN
(select id from Open where not exists (select 1 from ps where type = 'Open' and item_id = opens.id)) and o.type = pl.name;
我有大量數據..幫助將不勝感激!!!
試試看:
insert into abc(id,item_id,item_type,l_id,c_id)
Select '',o.id,'Open',pl.id,'67' from pls pl
INNER JOIN opens o ON o.type = pl.name
INNER JOIN open ON o.id = open.id
where not exists (select 1 from ps where type = 'Open' and item_id = opens.id)
對於選擇切換,請使用IN進行聯接:-
SELECT DISTINCT '', o.id, 'Open', pl.id, '67'
FROM pls pl
INNER JOIN opens o ON and o.type = pl.name
INNER JOIN open op ON and o.id = op.id
LEFT OUTER JOIN ps ON ps.type = 'Open' and ps.item_id = opens.id
WHERE ps.item_id IS NULL
我添加了DISTINCT,以防打開表上的ID不唯一。 如果它是唯一的,則可以省略。
LEFT OUTER JOIN檢查PS表上是否有匹配的記錄,如果有,則返回該記錄。 如果不是,則該表中的列返回為NULL。 然后在WHERE子句中,從結果中省略非null的那些。
但是,為了提高效率,表上的索引很重要。 打開表上的類型有索引嗎? 打開表上的ID索引? 包含ps表上的type和item_id的索引?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.