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 YourEmailAddres sHere" to ma*******@postg resql.org)
Nov 22 '05
14 5720
> >The original subquery looked like: 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
Please, before continuing this thread, read my post below. What you're
all getting around to, albeit painfully, is that this subquery is
worthless as-is. This is the mysql way of finding rows in one table
with no match in another without the convenience of the "in" or "exists"
constructs.
Because we're using Postgres and have those constructs, the original
query can be rewritten simply with either:
delete from LogEvent where EventType != 'i' and ItemID not in
(select ItemID from Item)
That's it. That's the whole query. It does what he wants.
Michael
--
Michael Darrin Chaney md******@michae lchaney.com http://www.michaelchaney.com/
---------------------------(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
Michael Chaney wrote: Please, before continuing this thread, read my post below. What you're all getting around to, albeit painfully, is that this subquery is worthless as-is. This is the mysql way of finding rows in one table with no match in another without the convenience of the "in" or "exists" constructs.
Because we're using Postgres and have those constructs, the original query can be rewritten simply with either:
delete from LogEvent where EventType != 'i' and ItemID not in (select ItemID from Item)
That's it. That's the whole query. It does what he wants.
One more minor point. :-)
If you are using 7.3 or earlier, PostgreSQL will sequentially scan
the IN subquery result, which executes quite slowly and therefore
the EXISTS method Stephan stated should be used:
DELETE FROM LogEvent
WHERE EventType != 'i' AND NOT EXISTS (
SELECT 1
FROM Item
WHERE Item.ItemID = LogEvent.ItemID
);
If you are using >= 7.4, then your query above is optimal: http://www.postgresql.org/docs/7.4/s...ml#RELEASE-7-4
Just something to consider,
Mike Mascari Michael
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
On Fri, Feb 27, 2004 at 12:05:48PM -0500, Mike Mascari wrote: Michael Chaney wrote:
Please, before continuing this thread, read my post below. What you're all getting around to, albeit painfully, is that this subquery is worthless as-is. This is the mysql way of finding rows in one table with no match in another without the convenience of the "in" or "exists" constructs.
Because we're using Postgres and have those constructs, the original query can be rewritten simply with either:
delete from LogEvent where EventType != 'i' and ItemID not in (select ItemID from Item)
That's it. That's the whole query. It does what he wants.
One more minor point. :-)
If you are using 7.3 or earlier, PostgreSQL will sequentially scan the IN subquery result, which executes quite slowly and therefore the EXISTS method Stephan stated should be used:
DELETE FROM LogEvent WHERE EventType != 'i' AND NOT EXISTS ( SELECT 1 FROM Item WHERE Item.ItemID = LogEvent.ItemID );
If you are using >= 7.4, then your query above is optimal:
Not necessarily. I had a query just last week that still wouldn't
optimize with the "in" notation, but did optimize with "exists"
notation. My other post about this showed both queries for that reason,
but I still feel that, for academic purposes, the "in" clause is far
more readable.
Anyway, good point.
Michael
--
Michael Darrin Chaney md******@michae lchaney.com http://www.michaelchaney.com/
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives? http://archives.postgresql.org
On Fri, 27 Feb 2004, Mike Mascari wrote: Stephan Szabo wrote: On Fri, 27 Feb 2004, Mike Mascari wrote:
To do what I think you believe to be happening w.r.t. outer joins, you'd have to have a subquery like:
[test@lexus] select a.fookey test-# FROM test-# (SELECT foo.key AS fookey, bar.key as barkey FROM foo LEFT OUTER JOIN bar ON foo.key = bar.key) AS a test-# WHERE a.barkey IS NULL; This AFAICS is pretty much what he did, except that he didn't alias the join which is okay I believe. He had one condition in on and two conditions in where.
The original subquery looked like: 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
That is indeed the original subquery. But the 'i.ItemID is null' condition doesn't change the IN list one iota. He was somehow
.... Another example:
[test@lexus] select * from foo; key ----- 1 3 (2 rows)
[test@lexus] select * from bar; key | value -----+------- 1 | Mike 2 | Joe (2 rows)
[test@lexus] select foo.key, bar.key from foo left outer join bar on foo.key = bar.key and bar.key is null;
ON conditions and WHERE conditions are different.
Try
select foo.key, bar.key from foo left outer join bar on foo.key=bar.key
where bar.key is null;
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives? http://archives.postgresql.org
Stephan Szabo wrote: ON conditions and WHERE conditions are different.
Try select foo.key, bar.key from foo left outer join bar on foo.key=bar.key where bar.key is null;
Yep. Sorry.
Mike Mascari
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dam |
last post by:
Using SqlServer :
Query 1 :
SELECT def.lID as IdDefinition,
TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour
FROM serveur.Data_tblDEFINITIONTABLEDECODES def,
serveur.Data_tblTABLEDECODEAUNEVALEUR TDC_AUneValeur
where def.TYPEDETABLEDECODES = 4
|
by: Steve |
last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three
"Left Outer Joins" in order to return every transaction for a specific
set of criteria.
Using three "Left Outer Joins" slows the system down considerably.
I've tried creating a temp db, but I can't figure out how to execute
two select commands. (It throws the exception "The column prefix
'tempdb' does not match with a table name or alias name used in the
query.")
|
by: Steve |
last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three
"Left Outer Joins" in order to return every transaction for a specific
set of criteria.
Using three "Left Outer Joins" slows the system down considerably.
I've tried creating a temp db, but I can't figure out how to execute
two select commands. (It throws the exception "The column prefix
'tempdb' does not match with a table name or alias name used in the
query.")
|
by: Martin |
last post by:
Hello everybody,
I have the following question.
As a join clause on Oracle we use " table1.field1 = table2.field1 (+) "
On SQL Server we use " table1.field1 *= table2.field1 "
Does DB2 have the same type of operator, without using the OUTER JOIN
syntax ?
|
by: M.B |
last post by:
Guys,
Need some of your opinion on an oft beaten track
We have an option of using "goto" in C language, but most testbooks
(even K&R) advice against use of it.
My personal experience was that goto sometimes makes program some more
cleaner and easy to understand and also quite useful (in error handling
cases).
So why goto is outlawed from civilized c programmers community.
is there any technical inefficiency in that.
| |
by: SKB |
last post by:
Hi,
I want to implement the "outer join" functionality in Java. Can
somebody explain the pseudo code for the same. OR what needs to be done
to extend the hash-join Java code of equijoin(I have the hash join code
for equijoin with me).
Regds
|
by: saniac |
last post by:
I am working on a little project using pysqlite. It's going to be
exposed on the web, so I want to make sure I quote all incoming data
correctly. However, I've run into a brick wall trying to use parameters
to populate a query of the form "select * where col1 in ( ? )"
The naive approach doesn't work:
values=
sql = """select * where value in (?)"""
cu = cx.cursor()
|
by: Kevin Chambers |
last post by:
Hi all--
Quick question: has anyone come up with an easy way to take an array
and use its elements as part of a WHERE clause? For example:
<This obviously doesn't work>
SELECT *
FROM Table1
WHERE Field1 IN Array(1,2,3)
The only solution I can think of is putting the values in a temp table
|
by: nico3334 |
last post by:
I have a query that currently pulls data from a main table and a second table using LEFT OUTER JOIN. I know how to do make another LEFT OUTER JOIN with the main table, but I want to add another LEFT OUTER JOIN to the second table. So I want the third table to be joined through the second table, not the main table.
Here is my original code that joins the main table and the second table
SELECT t1.supply,
t2.inventory,
FROM MAIN_TABLE...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |