Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compare two SQL Select
Message
From
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:
01508174
Views:
52
>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.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform