Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compare two SQL Select
Message
 
 
To
22/04/2011 20:30:14
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01508173
Message ID:
01508175
Views:
33
>>If you compare the following two SQL Selects:
>>
>>1.
>>
>>select top 1 from MyTable where status = '0' and  pk_field > nSomeNumber order by pk_field
>>
>>
>>2.
>>
>>select * from MyTable where status = '0' and pk_field > nSomeNumber
>>
>>
>>The table MyTable has an index key on pk_field.
>>
>>It looks to me that even though the first SQL Select only selects 1 record but the second one selects all records, the time it takes to process the query should be about the same. My reasoning is that the first SQL Select has to select all records first anyway (which is the same as the second SQL Select) and then select top 1 record. So I think that VFP has to pull the records matching the WHERE to the local machine anyway.
>>
>>Does it make sense? Or do you think that the SQL Select 1 is considerably faster.
>
>My experience with using TOP clause, no matter the backend, is that it can sometimes be unpredictable to the performance. As you mentioned, using TOP will make the SQL engine act differently so to give you the top n records that you want. Among all SQL enhancements I had to deal with since several years, it is usually related to using that clause that would be directly related to it. The second query might be faster as it doesn't have to deal with the entire records but only with a subset of records from the index. I assume you have some on the related fields. As for top clause, its performance will vary based on the number of records in the table. So, it can be faster on one situation and slower on another one.

Thank you. You experience and logic pretty much confirms for me that using the query without TOP and then simply GO TOP on the resulting query (created without TOP 1) will not adversely affect the time.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform