我用ado+access2000做的下程序,access2000有个cy的表,里面有5000记录
我用下面这个查询好像效率不高,要耗时近2秒钟才能完成
begin
  adoquery3.Close;
  adoquery3.SQL.Clear;
  adoquery3.SQL.Add('select count(*) from cy where 语文>=:d and left(xsbh,5)="c0301"');
  ADOQUERY3.Parameters[0].Value:=90;
  adoquery3.Active:=true;
  edit1.Text:=adoquery3.Fields[0].Value;
end;
能不能将这个查询优化一下,因为在我这个程序里有16个循环,每个循环里面有14个上面类似的查询,也就是说,完成它要近4分钟!
有没有办法快点呀?
 

解决方案 »

  1.   

    我觉得还是把参数值直接写到SQL语句中好一点
      

  2.   

    我把这整个循环帖上来,好心人帮我看看吧!是不是能够优化。它时间知长了(4分钟)
    progressbar1.Max:=combobox1.Items.Count-1;
          bhtemp:=bh;
          for i:=1 to combobox1.Items.Count-1 do//一般combobox1.Items.Count为16到20左右
            begin
               progressbar1.Position:=i;
               if i<10 then
                 bh:=bh+'0'+inttostr(i)
               else
                 bh:=bh+inttostr(i);//求学校编号
               adoquery3.close;
               adoquery3.sql.clear;
                ADOQUERY3.SQL.ADD('select xxmc from xxdm where xxbm=:i');
               ADOQUERY3.Parameters[0].Value:=inttostr(i);
               adoquery3.Open;
               mc:=adoquery3.Fields[0].Value;//求学校名称
               adoquery3.close;
               adoquery3.sql.clear;
               ADOQUERY3.SQL.ADD('select count(*) from cy where left(xsbh,5)="'+bh+'"');
               adoquery3.Open;
               sbs:=adoquery3.Fields[0].Value;//求学生上报数
               adoquery3.close;
               adoquery3.sql.clear;
               ADOQUERY3.SQL.ADD('select count(*) from cy where left(xsbh,5)="'+bh+'" and '+combobox3.Text+'>=0');
               adoquery3.Open;
               sks:=adoquery3.Fields[0].Value;//求学生的实考数
               ckl:=sks/sbs*100;
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select avg( '+combobox3.Text+') from cy where left(xsbh,5)="'+bh+'"');
               adoquery3.Active:=true;
               pjf:=adoquery3.Fields[0].Value;//求平均分(这里耗时较长1.5秒)
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select mf from kcsz where nj="初一" and kc=:kc');
               ADOQUERY3.Parameters[0].Value:=combobox3.Text;
               adoquery3.Active:=true;
               mf:=adoquery3.Fields[0].Value;//求满分
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select count(*) from cy  where '+combobox3.Text+'>=:fs and left(xsbh,5)="'+bh+'"');
               ADOQUERY3.Parameters[0].Value:=mf*0.6;
               adoquery3.Active:=true;
               jgl:=adoquery3.Fields[0].Value/sks*100;求及格率(这里也要耗时1.5秒)
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select sum('+combobox3.Text+'*'+combobox3.Text+') from cy where '+combobox3.Text+'>-1 and left(xsbh,5)="'+bh+'"');
               adoquery3.Active:=true;
               x1:=adoquery3.Fields[0].Value;
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select sum('+combobox3.Text+')*sum('+combobox3.Text+')from cy where '+combobox3.Text+'>-1 and left(xsbh,5)="'+bh+'"');
               adoquery3.Active:=true;
               x2:=adoquery3.Fields[0].Value;
               bzc:=sqrt((x1-x2/sks)/(sks-1));//这里求标准差(这里也要耗时1.5秒)
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select a,b,c,d from ddqz where nj="初一" and kc=:kc');
               ADOQUERY3.Parameters[0].Value:=combobox3.Text;
               adoquery3.Active:=true;
               a:=adoquery3.Fields[0].Value;
               b:=adoquery3.Fields[1].Value;
               c:=adoquery3.Fields[2].Value;
               d:=adoquery3.Fields[3].Value;
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select count(*) from cy where '+combobox3.Text+'>=:a and left(xsbh,5)="'+bh+'"');
               ADOQUERY3.Parameters[0].Value:=a;
               adoquery3.Active:=true;
               a:= adoquery3.Fields[0].Value;
               adl:=a/sks*100;//求A等率(这里也要耗时1.5秒)
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select count(*) from cy where '+combobox3.Text+'>=:b and left(xsbh,5)="'+bh+'"');
               ADOQUERY3.Parameters[0].Value:=b;
               adoquery3.Active:=true;
               b:=adoquery3.Fields[0].Value;
               bdl:=(b-a)/sks*100;//求B等率(这里也要耗时1.5秒)
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select count(*) from cy where '+combobox3.Text+'>=:c and left(xsbh,5)="'+bh+'"');
               ADOQUERY3.Parameters[0].Value:=c;
               adoquery3.Active:=true;
               c:=adoquery3.Fields[0].Value;
               cdl:=(c-b)/sks*100;//求C等率(这里也要耗时1.5秒)
               adoquery3.Close;
               adoquery3.SQL.Clear;
               adoquery3.SQL.Add('select count(*) from cy where '+combobox3.Text+'>=:d and left(xsbh,5)="'+bh+'"');
               ADOQUERY3.Parameters[0].Value:=d;
               adoquery3.Active:=true;
               d:=adoquery3.Fields[0].Value;
               ddl:=(d-c)/sks*100;//求D等率(这里也要耗时1.5秒)
               edl:=100-adl-bdl-cdl-ddl;//求E等率
               jlz:=(6*adl+4*bdl+3*cdl+ddl-edl)/100;//求均量值
               adoquery3.close;
               adoquery3.sql.clear;
               ADOQUERY3.SQL.ADD('INSERT INTO pjb values ("'+bh+'","'+mc+'",'+inttostr(sbs)+','+inttostr(sks)+','+floattostr(ckl)+','+floattostr(pjf)+','+floattostr(jgl)+','+floattostr(bzc)+','+floattostr(adl)+','+floattostr(bdl)+','+floattostr(cdl)+','+floattostr(ddl)+','+floattostr(edl)+','+floattostr(jlz)+')');
               adoquery3.ExecSQL;//将各个值写入数据表pjb中
               bh:=bhtemp;
            end;
         showmessage('统计完成!!');
      

  3.   

    我觉得,在你的循环中重复的adoquery3.close;adoquery3.sql.clear;adoquery3.Open;和重复的SQL语句的执行、控件属性的引用都是使得程序运行速度降低的原因。不要在For循环中重复控件的属性和方法操作
                           
    你可以将查询放在一个预处理过程中,将查询的结果放在数组中.在循环过程中从数组中取值.
      

  4.   

    多说一句:加大AdoQuery的CacheSize值到1000
      

  5.   

    用存储过程,或者把这些SQL语句合并起来,因为我发现你上面这段代码很多的过滤条件是相同的。考虑用Union on将这些合并在一起执行,效率应该会好一些!
      

  6.   

    我用的是access2000的mdb数据库,能用存储过程吗?
      

  7.   

    因为上述用SQL语句所求的东西,相对比较固定,可以事先将所求的结果保存到一个表中或一个文件中,这样在查询时,直接从表或文件中读取,速度较快。
      

  8.   

    你这个程序不对,应当可以直接取出的,不用把sql放在循环中,尤其是象你这个程序,这样太慢,把整个程序优化一下,把你要做的总结一下。
    另外我觉得你需要学习一下SQL命令了,好象很多SQL可以联在一起,
      

  9.   

    1.建索引
    2.不要频繁进行Close、Open操作。对可在一次查询中得到的值,尽量在一次操作中全部返回。
      

  10.   

    access数据库不支持存储过程,但支持数据库视图查询,用法跟大型数据库的存储过程查不多,建议试一下。