線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1075
推到 Plurk!
推到 Facebook!

這段計算刷卡時間的程式速度很慢

尚未結案
ycl2005
一般會員


發表:15
回覆:14
積分:5
註冊:2005-10-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-11-21 12:13:50 IP:211.21.xxx.xxx 未訂閱
Dear All, 這段程式跑出來的結果,尚能符合需求, 但是速度真的很慢,請各位先進指點, 謝謝。 hrs_card_read(刷卡匯入檔) emp_no,VARCHAR(10) 員工編號 card_date,VARCHAR(8) 刷卡日期 card_time,VARCHAR(6) 刷卡時間 hrs_punchdata(每日刷卡記錄) card_date,VARCHAR(8) 刷卡日期 emp_no,VARCHAR(10) 員工編號 status,CHAR(1) 出勤狀況 punch_time1,VARCHAR(6) 早上上班 punch_time2,VARCHAR(6) 早上下班 punch_time3,VARCHAR(6) 下午上班 punch_time4,VARCHAR(6) 下午下班 punch_time5,VARCHAR(6) 加班上班 punch_time6,VARCHAR(6) 加班下班 刷卡匯入檔內容 1241530054 2005-11-01 06:59 1241522614 2005-11-01 07:06 1241530423 2005-11-01 07:01 1241529374 2005-11-01 06:44 1241518257 2005-11-01 06:26 1241517535 2005-11-01 06:42 1241530054 2005-11-01 12:10 1241522614 2005-11-01 12:15 1241530423 2005-11-01 12:35 1241529374 2005-11-01 13:02 1241518257 2005-11-01 13:00 1241517535 2005-11-01 13:10 1241530054 2005-11-01 16:30 1241522614 2005-11-01 17:15 1241530423 2005-11-01 18:35 1241529374 2005-11-01 18:02 1241518257 2005-11-01 19:00 1241517535 2005-11-01 20:58 程式碼 begin dBegDate:=DateStringToDateTime(Params[0]); //刷卡開始日期 dEndDate:=DateStringToDateTime(Params[1]); //刷卡結束日期 j:=Trunc(dEndDate-dBegDate 1); Date_n:= DateTimeToDateString(dBegDate); //從員工資料檔中產生每日刷卡記錄檔,只填入 刷卡日期 和員工編號。 qryInsert.Close; qryhrs_emp.Close; qryhrs_emp.SQL.Text:='select emp_no,shift_no from hrs_emp where emp_status=' QuotedStr('1'); qryhrs_emp.Open; qryhrs_emp.Last; qryhrs_emp.First; for i:=1 to j do begin qryhrs_emp.Last; qryhrs_emp.First; while not qryhrs_emp.eof do begin sEmp_no:=qryhrs_emp.fieldByName('emp_no').AsString; qryInsert.SQL.Clear; qryInsert.SQL.Add('insert into hrs_punchdata (card_date,emp_no) values ('); qryInsert.SQL.Add(QuotedStr(Date_n) ',' QuotedStr(sEmp_no) ')'); qryInsert.ExecSQL; qryhrs_emp.Next; end; Date_n:=DateTimeToDateString(DateStringToDateTime(Date_n) 1); end; //for i=1 to j do qryhrs_emp.Close; qryTemp.Close; qryTemp.SQL.Clear; qrytemp.SQL.Add('select * from hrs_punchdata where card_date between '); qrytemp.SQL.Add(Quotedstr(Params[0]) ' and ' QuotedStr(Params[1])); qryTemp.SQL.Add(' Order by card_date,emp_no asc'); qryTemp.Open; qryTemp.Last; qryTemp.First; while not qryTemp.eof do begin sCard_Date:=qryTemp.FieldbyName('card_date').AsString; sEmp_no:=qryTemp.FieldByName('emp_no').AsString; qryhrs_card_read.Close; qryhrs_card_read.SQL.Clear; qryhrs_card_read.SQL.Add('select * from hrs_card_read where card_date='); qryhrs_card_read.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryhrs_card_read.SQL.Add(' Order by card_date,emp_no,card_time asc'); qryhrs_card_read.Open; qryhrs_card_read.Last; qryhrs_card_read.First; //找無刷卡資料 if qryhrs_card_read.Eof then begin qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('update hrs_punchdata set status=' QuotedStr('5')); //status=5 代表未刷卡 qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.ExecSQL; end; while not qryhrs_card_read.Eof do begin sTemp_time:=qryhrs_card_read.FieldByName('card_time').AsString; //時間格式 hh:mm -> hhmmss sCard_date:=qryhrs_card_read.FieldByName('card_date').AsString; sEmp_no:=qryhrs_card_read.FieldByName('emp_no').AsString; ATime := StrToTime(sTemp_time); if (ATime >=strtotime('00:00')) and (aTime <=strtotime('11:59')) then begin //先找有無刷卡時間 qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('select * from hrs_punchdata'); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.Open; if qryInsert.FieldByName('punch_time1').AsString='' then sSql:= 'punch_time1=' QuotedStr(sTemp_time) else sSql:= 'punch_time2=' QuotedStr(sTemp_time); //上午第二次刷卡時間即上午下班 qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('update hrs_punchdata set ' sSql); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.ExecSQL; end; if (ATime >=strtotime('12:00')) and (aTime <=strtotime('16:00')) then begin //先找有無刷卡時間 qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('select * from hrs_punchdata'); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.Open; if qryInsert.FieldByName('punch_time3').AsString='' then sSql:= 'punch_time3=' QuotedStr(sTemp_time) else sSql:= 'punch_time4=' QuotedStr(sTemp_time); //下午第二次刷卡時間即下午下班 qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('update hrs_punchdata set ' sSql); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.ExecSQL; end; if (ATime >=strtotime('16:00')) and (aTime <=strtotime('23:59')) then begin // ShowMessage('加班下班時間' sTemp_time); sSql:= 'punch_time5=' QuotedStr('16:00') ',punch_time6=' QuotedStr(sTemp_time); qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('update hrs_punchdata set ' sSql); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.ExecSQL; end; qryhrs_card_read.Next; end; //while not qryhrs_card_read.Eof do //如果punch_time2是空值, 填入 '12:00' qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('update hrs_punchdata set '); qryInsert.SQL.Add('punch_time2=' QuotedStr('12:00')); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.SQL.Add(' and punch_time2 is null'); qryInsert.ExecSQL; //如果punch_time4是空值, 填入 '16:00' qryInsert.Close; qryInsert.SQL.Clear; qryInsert.SQL.Add('update hrs_punchdata set '); qryInsert.SQL.Add('punch_time4=' QuotedStr('16:00')); qryInsert.SQL.Add(' where card_date='); qryInsert.SQL.Add(Quotedstr(sCard_Date) ' and emp_no=' QuotedStr(sEmp_no)); qryInsert.SQL.Add(' and punch_time4 is null'); qryInsert.ExecSQL; qryTemp.Next; end; //while not qryTemp.eof do end;
kgt
高階會員


發表:17
回覆:308
積分:165
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-11-21 12:27:43 IP:210.243.xxx.xxx 未訂閱
先從資料量及是否建index來思考。
way888
初階會員


發表:32
回覆:76
積分:36
註冊:2005-05-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-11-21 16:30:53 IP:220.137.xxx.xxx 未訂閱
ycl2005 您好: 從你的code看來,大部分的邏輯判斷都是下SQL讓資料庫做判斷,這樣的過程當然會增加時間,因為這樣的邏輯判斷並非程式本身去執行的,Delphi程式本身判斷迴圈或邏輯或數學運算都是非常快速的(因為這些程式碼都已編譯成機械碼了),所以建議你將大部分的邏輯判斷交給Delphi程式本身,SQL只做SELECT、INSERT、ADD、UPDATE...等關鍵任務,這樣程式的效率就大大的提升了。
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-11-23 14:10:24 IP:203.73.xxx.xxx 未訂閱
1.我同意way888, 您的程式要Review一下囉。    2.xQuery1.Last;
  xQuery1.First;
  上述 .Last及.First都是多餘的。 
**
------
What do we live for if not to make life less difficult for each other?
kgt
高階會員


發表:17
回覆:308
積分:165
註冊:2002-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-11-23 21:38:13 IP:61.219.xxx.xxx 未訂閱
在不改程式碼的狀況, 請在資料庫上hrs_punchdata 表格加上一個Index含以下欄位 -->card_date,emp_no ----------------------------------------------------- 另外,可參考二位大大衷心的建議就更佳了。
ycl2005
一般會員


發表:15
回覆:14
積分:5
註冊:2005-10-01

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-11-26 11:42:35 IP:211.21.xxx.xxx 未訂閱
按照各位的建議,我先用DELPHI將所有狀況判斷好, 再一次寫到SQL中,速度變快了很多, 謝謝大家。
系統時間:2024-06-27 5:15:04
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!