全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1314
推到 Plurk!
推到 Facebook!

adoquery效率问题?

尚未結案
xxxxzxx
一般會員


發表:21
回覆:49
積分:24
註冊:2004-11-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-05-06 01:36:17 IP:220.163.xxx.xxx 未訂閱
请各位大大帮看看,以下代码可以工作但效率不高~大概有1-2秒的延迟,是什么问题呢?小弟先谢过. procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); var s1,s2,s3,xfsj,xf:string; i:integer; s4:Tstringlist; begin s1:=Socket.ReceiveText; if trim(s1)<>'d' then begin s4:=Tstringlist.Create ; s4.Delimiter :=' '; s4.DelimitedText :=s1; s2:=s4[0]; s3:=s4[1]; s4.Clear; ////////////////// read.close; read.SQL.Clear; read.SQL.Add('select accounts,pws,[money] from jbb'); read.SQL.Add('where accounts=''' s2 ''' and pws=''' s3 ''' and [money]>0.5 and inuse=false'); read.Open; if read.RecordCount <> 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do begin if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('$$$' ':' read.FieldByName('money').AsString); end; for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do begin if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('3'); end; read.close; read.sql.Clear; read.SQL.Add('INSERT INTO jlb(usename,intime,pcname,pcip,used) VALUES'); read.SQL.Add('(''' s2 ''',now(),''' Socket.RemoteHost ''',''' Socket.RemoteAddress ''',true)'); read.ExecSQL; read.close; read.sql.Clear; read.SQL.Add('update jbb set inuse=true,pcname=''' Socket.RemoteHost ''' where accounts=''' s2 ''''); read.ExecSQL; end else begin read.close; read.sql.Clear; read.SQL.Add('select accounts,pws from jbb'); read.SQL.Add('where accounts=''' s2 ''' and pws=''' s3 ''''); read.open; if read.RecordCount = 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('2'); end else begin read.close; read.sql.Clear; read.SQL.Add('select inuse from jbb where accounts=''' s2 ''' and inuse=true'); read.open; if read.RecordCount <> 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('4'); end else begin read.close; read.sql.Clear; read.SQL.Add('select [money] from jbb where accounts=''' s2 ''' and [money]>0.5'); read.open; if read.RecordCount = 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('6'); end; end; end; end; end else begin Disc.close; Disc.sql.Clear; Disc.SQL.Add('update jbb set inuse=false where pcname=''' Socket.RemoteHost ''' and inuse=true'); Disc.ExecSQL; Disc.close; Disc.sql.Clear; Disc.SQL.Add('select intime from jlb where pcname=''' Socket.RemoteHost ''' and used=true'); Disc.open; xfsj:=inttostr(MinutesBetween(now,Disc.fields.Fields[0].AsDateTime)) ; xf:=FloatToStr(MinutesBetween(now,Disc.fields.Fields[0].AsDateTime)*0.016); Disc.close; Disc.sql.Clear; Disc.SQL.Add('update jlb set endtime=now(),usedtime=''' xfsj ''',xfjer=''' xf ''',used=false'); Disc.SQL.Add('where pcname=''' Socket.RemoteHost ''' and used=true'); Disc.ExecSQL; end; end;
scotthsiao
高階會員


發表:13
回覆:324
積分:147
註冊:2005-02-01

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-05-06 09:13:34 IP:210.66.xxx.xxx 未訂閱
似乎是Dtatabase 的問題。 加上 index ,再試試效率會不會加快,尤其是在 where 之後存取很多欄位的表格 。
xxxxzxx
一般會員


發表:21
回覆:49
積分:24
註冊:2004-11-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-05-06 13:07:18 IP:220.163.xxx.xxx 未訂閱
谢谢scotthsiao,不过我想: 数据很少啊...大概只有100条左右.在不用index时select * from jjb where 1=1 应该也不存效率在问题啊.
bestlong
站務副站長


發表:126
回覆:734
積分:512
註冊:2002-10-19

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-05-06 15:26:52 IP:211.22.xxx.xxx 未訂閱
第一個部份
for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do begin
if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then
ServerSocket1.Socket.Connections[i].SendText('$$$'  ':'  read.FieldByName('money').AsString);
end;
for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do begin
if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then
ServerSocket1.Socket.Connections[i].SendText('3');
end;
何必掃描兩次的, 可以改成
for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do begin
  if ServerSocket1.Socket.Connections[i].SocketHandle = Socket.SocketHandle then
  begin
    ServerSocket1.Socket.Connections[i].SendText('$$$'   ':'   read.FieldByName('money').AsString);
    ServerSocket1.Socket.Connections[i].SendText('3');
  end;
end;
雪龍 http://bestlong.no-ip.com/ 學海無涯覺無盡,勤做筆記防失憶
------
http://blog.bestlong.idv.tw/
http://www.bestlong.idv.tw/
http://delphi-ktop.bestlong.idv.tw/
bestlong
站務副站長


發表:126
回覆:734
積分:512
註冊:2002-10-19

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-05-06 15:50:07 IP:211.22.xxx.xxx 未訂閱
可以再說明清楚 1~2 秒的延遲是在做什麼操作動作. 還有你的整體架構沒有交待清楚. 就算是資料量很少, 設備等級不足與不適當的設定(沒有最佳化). 反應慢是有很可能發生的. 雪龍 http://bestlong.no-ip.com/ 學海無涯覺無盡,勤做筆記防失憶
------
http://blog.bestlong.idv.tw/
http://www.bestlong.idv.tw/
http://delphi-ktop.bestlong.idv.tw/
xxxxzxx
一般會員


發表:21
回覆:49
積分:24
註冊:2004-11-15

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-05-06 23:27:10 IP:220.163.xxx.xxx 未訂閱
谢谢bestlong 1.机器配备P42.6H 512MDDR.(机器一切正常) 2.用一个循环(for)就ok,我只不过想在逻辑上分的清楚些. 3.程式完整代码如下:(请各位帮助看看万分谢谢:)) unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, ComCtrls, ScktComp, DB, ADODB ,DateUtils; type TForm1 = class(TForm) StatusBar1: TStatusBar; GroupBox1: TGroupBox; GroupBox2: TGroupBox; DBGrid1: TDBGrid; ListBox1: TListBox; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; ServerSocket1: TServerSocket; DataSource1: TDataSource; ADOConnection1: TADOConnection; read: TADOQuery; disc: TADOQuery; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Button3Click(Sender: TObject); procedure ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ServerSocket1.Open; end; procedure TForm1.Button2Click(Sender: TObject); begin ServerSocket1.Close; end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin If messagedlg('确实要关闭服务端吗?',mtWarning , mbokcancel, 0) =mrok then begin serversocket1.Close; Canclose := True; end Else Canclose := False; end; procedure TForm1.Button3Click(Sender: TObject); begin close; end; procedure TForm1.ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin ErrorCode:=0; end; procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); var s1,s2,s3,xfsj,xf:string; i:integer; s4:Tstringlist; begin s1:=Socket.ReceiveText; if trim(s1)<>'d' then begin s4:=Tstringlist.Create ; s4.Delimiter :=' '; s4.DelimitedText :=s1; s2:=s4[0]; s3:=s4[1]; s4.Clear; ////////////////// read.close; read.SQL.Clear; read.SQL.Add('select accounts,pws,[money] from jbb'); read.SQL.Add('where accounts=''' s2 ''' and pws=''' s3 ''' and [money]>0.5 and inuse=false'); read.Open; if read.RecordCount <> 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do begin if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('$$$' ':' read.FieldByName('money').AsString); ServerSocket1.Socket.Connections[i].SendText('3'); end; {for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do begin if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('3'); end;} read.close; read.sql.Clear; read.SQL.Add('INSERT INTO jlb(usename,intime,pcname,pcip,used) VALUES'); read.SQL.Add('(''' s2 ''',now(),''' Socket.RemoteHost ''',''' Socket.RemoteAddress ''',true)'); read.ExecSQL; read.close; read.sql.Clear; read.SQL.Add('update jbb set inuse=true,pcname=''' Socket.RemoteHost ''' where accounts=''' s2 ''''); read.ExecSQL; end else begin read.close; read.sql.Clear; read.SQL.Add('select accounts,pws from jbb'); read.SQL.Add('where accounts=''' s2 ''' and pws=''' s3 ''''); read.open; if read.RecordCount = 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('2'); end else begin read.close; read.sql.Clear; read.SQL.Add('select inuse from jbb where accounts=''' s2 ''' and inuse=true'); read.open; if read.RecordCount <> 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('4'); end else begin read.close; read.sql.Clear; read.SQL.Add('select [money] from jbb where accounts=''' s2 ''' and [money]>0.5'); read.open; if read.RecordCount = 0 then begin for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do if ServerSocket1.Socket.Connections[i].SocketHandle=Socket.SocketHandle then ServerSocket1.Socket.Connections[i].SendText('6'); end; end; end; end; end else begin Disc.close; Disc.sql.Clear; Disc.SQL.Add('update jbb set inuse=false where pcname=''' Socket.RemoteHost ''' and inuse=true'); Disc.ExecSQL; Disc.close; Disc.sql.Clear; Disc.SQL.Add('select intime from jlb where pcname=''' Socket.RemoteHost ''' and used=true'); Disc.open; xfsj:=inttostr(MinutesBetween(now,Disc.fields.Fields[0].AsDateTime)) ; xf:=FloatToStr(MinutesBetween(now,Disc.fields.Fields[0].AsDateTime)*0.016); Disc.close; Disc.sql.Clear; Disc.SQL.Add('update jlb set endtime=now(),usedtime=''' xfsj ''',xfjer=''' xf ''',used=false'); Disc.SQL.Add('where pcname=''' Socket.RemoteHost ''' and used=true'); Disc.ExecSQL; end; end; end. 發表人 - xxxxzxx 於 2005/05/06 23:28:51
系統時間:2024-07-02 3:01:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!