Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to use select... where EXISTS ... ?
Message
De
27/05/2003 12:10:38
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00793069
Message ID:
00793151
Vues:
40
>>Sergey,
>>
>>>It's actualy opposite. IN subquery runs once and than result is used by the main query. The EXISTS uses correlated subquery so it has to run for every record in the main query.
>>
>>This test code would seem to say that IN is doing a lot more work than EXISTS.
>>
>That's because you put f2() function in the wrong place in the EXISTS subquery. See revised code below.
CLEAR
>create cursor x1 ( i1 i, c1 c(10) )
>for i = 1 to 10000
>   insert into x1 values ( i, transform( i ) )
>endfor
>index on i1 tag i1
>
>create cursor x2 ( i1 i, c1 c(10) )
>for i = 1 to 10000 step 2
>   insert into x2 values ( i, transform( i ) )
>endfor
>index on i1 tag i1
>
>n1 = 0
>n2 = 0
>
>select *, f1() ;
>   from x1 where exists ( select i1  from x2 WHERE f2() AND x2.i1 = x1.i1 ) ;
>   into cursor x3
>
>? n1, n2
>
>n1 = 0
>n2 = 0
>
>select *, f1() ;
>   from x1 where i1 in ( select f3(i1) from x2 ) ;
>   into cursor x3
>
>? n1, n2
>
>function f1()
>n1 = n1 + 1
>return
>
>function f2()
>n2 = n2 + 1
>RETURN
>
>function f3(tnVal)
>n2 = n2 + 1
>RETURN tnVal
>
However, this test only shows how many records where processed by query and subquery. I doesn't take in account other factors. If you run speed tests you'll find out that in VFP IN subquery is faster in most cases than EXISTS.

Sergey,
Didn't do the tests. You might be right. Just thinking on SQL specification most of the time EXISTS sounds to be faster to me. Both subqueries might be called N times, however say subquery for IN is returning 1000 rows and the value compared is about the ends. Each time nearly 1000 rows should be checked. OTOH for EXISTS it's either a .T./.F. return. Subquery might still return 1000 rows but 1 or 1000 means .T. and returns immediately.
Just my 0.02 cents.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform