哈哈哈哈,踏破铁鞋无处觅,得来全不费功夫!
无意中在ExEx浏览到一贴,速度超快.
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20123177.html
摘录如下:
-----------------------------------------------------------------------------------------
Comment from heathprovost 05/24/2001 09:01PM PST
Your speed problem has nothing at all to do with your query string or
and settings there of. It is a problem with the way you are using
the ADOQuery component AFTER you run your query. This is a VERY
common mistake that people make when using ADOExpress components.
The purpose of the ado components is to allow Delphi programmers
to treat an ADO recordset as a TDataSet so that they can bind data
controls to it. This is of course very useful, but ALOT of stuff
is going on under the hood to allow this to happen. Alot of VERY
slow stuff. If, however, you dont need to bind to data controls,
you should never use the properties of an ADOQuery or ADOTable directly.
Instead you use the recordset property of the ADO component
and make direct ADO calls to do your work. What you had like this:
qrtest.first ;
with QrTest do
begin
slStoreData := TStringList.Create ;
while not EOF do
begin
slStoreData.Add(qrtest.Fields[0].Value);
next ;
end;
end ;
should be rewritten like this (add the ADOInt unit to your uses clause):
var
//these two vars will be used to cache the ADOQuery props
rs: _Recordset;
fld: field;
....
rs := qrtest.recordset; //set recordset to cache var
fld := rs.Fields[0]; //set desired field to cache var
//now use only methods of rs and fld
rs.MoveFirst;
with rs do
begin
while not EOF do
begin
//need to use vartostr() to make this safe
sl.Add(vartostr(fld.Value));
movenext;
end;
end ;
//clear the cache vars by setting them to nil
rs := nil;
fld := nil;
The code you gave took about 18 minutes on my computer.
The code above only took 6 seconds
Heath
ps: if you need to output more than 1 field, just create a cache var for each field before looping the recordset.
Comment from heathprovost 05/24/2001 09:04PM PST
btw, i created a test table to do my analysis.
It was on MS SQL 2000 and contained 5 fields and 236000 records.
That is what my timings were based on. Your timings may vary a bit but it shouldnt matter a whole lot.
-------------------------------------------------------------------------------------------------------