Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL-Cmd Select Top slows down query, why?
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
00953905
Message ID:
00955995
Views:
9
>>Hi all,
>>
>>I have a large table in the following format:
>>
>>Table name: table1
>>Record count: 137,866
>>Fields: filenum I,field1,field2...field124
>>index: filenum
>>
>>I've created a query form allowwing the user to select top N of records in the following manner:
>>
>>select top nSelectCount ;
>>     * ;
>>   from table1 ;
>>   where filenum >= nFilenum ;
>>   order by filenum ;
>>   into cursor curMySelection ;
>>   nofilter
>>
>>The user determines the values of nSelectCount and nFilenum through the form interface.
>>But the above query runs so much slower compared to the following query statement:
>>
>>
>>select top nSelectCount ;
>>     * ;
>>   from table1 ;
>>   where filenum >= nFilenum and filenum <= nFilenum + nSelectCount;
>>   order by filenum ;
>>   into cursor curMySelection ;
>>   nofilter
>>
>>
>>The reason I couldn't use the second statement is that the filenum is not always in sequence due records deletion, therefore the user doesn't get top nSelectCount, worse yet sometimes he gets nothing.
>>
>>Is there a way to speed up query with TOP option included the first statement?
>>
>>TIA
>
>No. VFP don't use index when do a ORDER BY clause.
>The first command extract all the records with where condition true,
>SORT the result,
>extract the first nSelectCount rows.
>
>two ways exists:
>
>1. foud the nSelectCount fileNum and do the second select
>SELECT table1
>set order to filenum
>set near on
>seek M.nFilenum
>set near off
>COUNT NEXT m.nSelectCount
>upperfilenum = IIF(EOF(),MAXFILENUMALLOWEDCOSTANT,filenum)
>
>select * ;
>   from table1 ;
>   where filenum BETWEEN M.nFilenum and M.upperfilenumt;
>   order by filenum ;
>   into cursor curMySelection NOFILTER
>
>2. You can use a in order SCAN with INSER
>* BUILD THE RESUL CURSOR
>select * from table1 ;
>   where .f. into cursor curMySelection ;
>   readwrite
>SELECT table1
>set order to filenum
>set near on
>seek M.nFilenum
>set near off
>SCAN NEXT m.nSelectCount
>  * append table1.record into curMySelection
>ENDSCAN
>
Grazie, Fabio for the reply, I shall try your methods. I had to do something different since I posted the message thus the delay in the response.
Dawa Tsering


"Do not let any unwholesome talk come out of your mouths,
but only what is helpful for building others up according to their needs,
that it may benefit those who listen."

- Ephesians 4:29-30 NIV

Dare to Question -- Care to Answer

Time is like water in a sponge, as long as you are willing you can always squeeze some.

--Lu Xun, Father of Modern Chinese Literature

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform