Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to use select... where EXISTS ... ?
Message
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00793069
Message ID:
00793141
Views:
34
>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.
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform