使用“in”进行相关删除和“左外连接” [英] correlated delete with "in" and "left outer join"

查看:82
本文介绍了使用“in”进行相关删除和“左外连接”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用postgresl 7.3.2并且查询执行速度非常慢。


有2个表:Item和LogEvent。 ItemID(一个int4)是Item的主键

,也是LogEvent中的一个字段。 LogEvent中的一些ItemID没有

对应于Item中的ItemID,并且我们需要定期从LogEvent中清除

不匹配的ItemID。


查询是:


从LogEvent中删除其中EventType!=''i''和ItemID in

(从中选择不同的e.ItemID来自LogEvent e左外连接项目i

on e.ItemID = i.ItemID其中e.EventType!=''i''和i.ItemID为null);


我明白使用in不是很有效。


是否有其他方法可以在没有in的情况下编写此查询?


----- ----------------------(播出结束)----------------------- ----

提示2:您可以使用取消注册命令一次性取消所有列表

(发送取消注册YourEmailAddressHere到 ma ******* @ postgresql.org

I''m using postgresl 7.3.2 and have a query that executes very slowly.

There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key
of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not
correspond to ItemIDs in Item, and periodically we need to purge the
non-matching ItemIDs from LogEvent.

The query is:

delete from LogEvent where EventType != ''i'' and ItemID in
(select distinct e.ItemID from LogEvent e left outer join Item i
on e.ItemID = i.ItemID where e.EventType != ''i'' and i.ItemID is null);

I understand that using "in" is not very efficient.

Is there some other way to write this query without the "in"?

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

推荐答案

2004年2月27日上午11:26,< mi ** @ linkify.com>写道:
On Feb 27, 2004, at 11:26 AM, <mi**@linkify.com> wrote:
我正在使用postgresl 7.3.2并且查询执行速度非常慢。


< snip />

我明白使用in效率不高。

是否有其他方法可以在没有in的情况下编写此查询?
I''m using postgresl 7.3.2 and have a query that executes very slowly.
<snip />
I understand that using "in" is not very efficient.

Is there some other way to write this query without the "in"?




NOT EXISTS()是有时更有效率。如果可能的话,将

升级到7.4.1。自7.3.2以来改进的许多事情之一是使用IN的查询效率。


Michael Glaesemann

grzm myrealbox com

---------------------------(播出结束)------- --------------------

提示8:解释分析是你的朋友



NOT EXISTS ( ) is sometimes more efficient. If at all possible, upgrade
to 7.4.1. One of the many things that have improved since 7.3.2 is the
efficiency of queries using IN.

Michael Glaesemann
grzm myrealbox com
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


2004年2月26日星期四, mi**@linkify.com 写道:
On Thu, 26 Feb 2004 mi**@linkify.com wrote:
我正在使用postgresl 7.3.2并且查询执行速度非常慢。

有两个表:Item和LogEvent。 ItemID(int4)是Item的主键,也是LogEvent中的一个字段。 LogEvent中的一些ItemID不对应于Item中的ItemID,并且我们需要定期从LogEvent中清除
不匹配的ItemID。

查询是:

从LogEvent中删除其中的EventType!=''i'和
中的ItemID(从LogEvent e中选择不同的e.ItemID左外连接项目i
在e.ItemID = i.ItemID其中e.EventType!=''i''和i.ItemID为空);

我明白使用in效率不高。

是否有其他方法可以在没有in的情况下编写此查询?
I''m using postgresl 7.3.2 and have a query that executes very slowly.

There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key
of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not
correspond to ItemIDs in Item, and periodically we need to purge the
non-matching ItemIDs from LogEvent.

The query is:

delete from LogEvent where EventType != ''i'' and ItemID in
(select distinct e.ItemID from LogEvent e left outer join Item i
on e.ItemID = i.ItemID where e.EventType != ''i'' and i.ItemID is null);

I understand that using "in" is not very efficient.

Is there some other way to write this query without the "in"?




也许

从LogEvent中删除,其中EventType!=''我'并且不存在

(从项目i中选择*,其中i.ItemID = LogEvent.ItemID);


---------------------------(广播结束)------------- --------------

提示9:如果您的

加入专栏,计划员将无视您选择索引扫描的愿望s数据类型不匹配



Perhaps
delete from LogEvent where EventType != ''i'' and not exists
(select * from Item i where i.ItemID=LogEvent.ItemID);

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column''s datatypes do not match


Stephan Szabo写道:
Stephan Szabo wrote:
2004年2月26日星期四 mi ** @ linkify.com 写道:

On Thu, 26 Feb 2004 mi**@linkify.com wrote:

我正在使用postgresl 7.3.2并且查询执行速度非常慢。

有2个表:Item和LogEvent。 ItemID(int4)是Item的主键,也是LogEvent中的一个字段。 LogEvent中的一些ItemID不对应于Item中的ItemID,并且我们需要定期从LogEvent中清除
不匹配的ItemID。

查询是:

从LogEvent中删除其中的EventType!=''i'和
中的ItemID(从LogEvent e中选择不同的e.ItemID左外连接项目i
在e.ItemID = i.ItemID其中e.EventType!=''i''和i.ItemID为空);

我明白使用in效率不高。

是否还有其他方法可以在没有in的情况下编写此查询?
I''m using postgresl 7.3.2 and have a query that executes very slowly.

There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key
of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not
correspond to ItemIDs in Item, and periodically we need to purge the
non-matching ItemIDs from LogEvent.

The query is:

delete from LogEvent where EventType != ''i'' and ItemID in
(select distinct e.ItemID from LogEvent e left outer join Item i
on e.ItemID = i.ItemID where e.EventType != ''i'' and i.ItemID is null);

I understand that using "in" is not very efficient.

Is there some other way to write this query without the "in"?



或许
从LogEvent中删除EventType!=''我'并且不存在
(从项目i中选择*,其中i.ItemID = LogEvent.ItemID);


Perhaps
delete from LogEvent where EventType != ''i'' and not exists
(select * from Item i where i.ItemID=LogEvent.ItemID);




也许我'我没有正确读取他的子查询,但左外部

连接将从LogEvent产生一行,无论Item中是否存在

匹配行,对吗?所以它不会减少到:


DELETE FROM LogEvent WHERE EventType<> ''我';


???


Mike Mascari


--- ------------------------(播出结束)--------------------- ------

提示1:订阅和取消订阅命令转到 ma ***** **@postgresql.org



Maybe I''m not reading his subquery correctly, but the left outer
join will produce a row from LogEvent regardless of whether or not a
matching row exists in Item, correct? So doesn''t it reduce to:

DELETE FROM LogEvent WHERE EventType <> ''i'';

???

Mike Mascari

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org


这篇关于使用“in”进行相关删除和“左外连接”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆