Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why sub-select so much slower than CSV list
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Vista
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01438268
Message ID:
01438270
Views:
56
>Can someone please explain why
>When querying a VFP free table, using a sub-select filter is 500 times slower than using a string list of comma separated values.
>*
>ANSI is OFF, EXACT is OFF, ENGINEBHAVIOR 70. Though it doesn't seem to make a difference otherwise
>To illustrate, please run the following code in a prg:
>*
>
LOCAL i, lcList, lcQval, nSec, lcCmd
>* Create source data cursor
>CREATE CURSOR tmpSource (fs1 C(10), fs2 C(10))
>FOR i = 1 TO 1000000
>	INSERT INTO tmpSource (fs1, fs2) ;
>		VALUES (;
>			'F1' + PADR(TRANSFORM(m.i), 8, "0"), ;
>			'F2' + PADR(TRANSFORM(m.i), 8, "0"))
>ENDFOR
>INDEX ON fs1 TAG fs1
>*
>* Create and populate cursor and list of explicit query values
>lcStrList = ""
>CREATE CURSOR tmpFilter (t1 C(10))
>FOR i = 1 TO 10
>	lcQval = 'F1' + PADR(TRANSFORM(m.i * 100), 8, "0")
>	* Populate cursor for sub-select
>	INSERT INTO tmpFilter (t1) VALUES (m.lcQval)
>	* Build list of explicit values
>	lcStrList = m.lcStrList + ["] + m.lcQval + [",]
>ENDFOR
>lcStrList = RTRIM(m.lcStrList, 1, ",")
>*
>* Now, the test
>* From explicit list of (comma separated) values
>nSec = SECONDS()
>lcCmd = "SELECT * FROM tmpSource WHERE fs1 IN (" + m.lcStrList + ") INTO CURSOR tmp"
>&lcCmd
>? TRANSFORM(SECONDS() - m.nsec) + " seconds to select " + TRANSFORM(RECCOUNT()) + " records - From (list)"
>*
>* From sub-select
>nSec = SECONDS()
>SELECT * FROM tmpSource WHERE fs1 IN (SELECT t1 FROM tmpFilter) INTO CURSOR tmp
>? TRANSFORM(SECONDS() - m.nsec) + " seconds to select " + TRANSFORM(RECCOUNT()) + " records - From Sub-select"
>*
>USE IN tmp
>USE IN tmpSource
>USE IN tmpFilter
>
Sub-selects are slow by definition. You may use EXISTS instead and see if the performance improves.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform