Well, now I'm running a request against a field that I know isn't indexed anywhere :
1. select '1' from sysibm.sysdummy1 where exists( select * from mytable where price between 1 and 2)
2. select '1' from mytable where price between 1 and 2 fetch first row only
VE gives virtually identical explication and identical times for a 20million line table.
The first example is actually more involved because of a join with sysdummy1. In both cases a temporary table is created with the lines that fulfil the search criteria.
-----Message d'origine-----
De : midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] De la part de Charles Wilt
Envoyé : lundi 19 janvier 2009 16:22
À : Midrange Systems Technical Discussion
Objet : Re: SQL to indicate existance of at least one line
On Mon, Jan 19, 2009 at 8:03 AM, Nathan Andelin <nandelin@xxxxxxxxx> wrote:
From: David FOXWELL
select '1' from sysibm.sysdummy1
where exists (select * from mytable where field1 = 'A'
and field2='000002')
Even though sysibm.sysdummy1 contains only 1 record, performance will be dependent on the number of rows and COLUMNS generated by the inner select statement. The constant "1" returned from the first select statement will be compared against each.
You are mistaken. The EXISTS predicate, doesn't look at all rows.
After it finds the first one that matches it returns true.
select '1' from mytable where field1 = 'A'
and field2='000002' fetch first row only;
You've reduced it to just one select statement that generates only one column. Good for you.
Not really, EXISTS is designed to provide the answer to the question, "does one or more rows exists that match a certain criteria". By using it, the SQE knows what you want to do. With the second statement, SQE doesn't know.
On my system, using the second form with a file containing about 35 million records of which 286 million match the criteria, there is a noticeable difference in speed; using EXISTS is faster. With the second statement I notice the following debug message:
CPI4325 - Temporary result file built for query.
Cause . . . . . : A temporary result file was created to contain the results
of the query for reason code 8. This process took 0 minutes and 1.0
seconds. The temporary file created contains 0 records. The reason codes and
their meanings follow:
13 - The query involves a static cursor or the SQL FETCH FIRST clause.
HTH,
Charles
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at
http://archive.midrange.com/midrange-l.
As an Amazon Associate we earn from qualifying purchases.