2013-02-03 34 views
6

我可以使用DELPHI,dbgo數據庫組件和SQL Server數據庫服務器編寫SQL查詢嗎?終止具有處理時間限制的SQL查詢

select * from table where ...... 

process_time_limit = 5 sec

更好

你給我行的10%的時間限制,而不是等待數小時後才進行完整的查詢數據集

+0

你要什麼時,5秒鐘內到達的情況發生?你可以處理的例外情況,還是其他?你使用的是哪個版本(也許Resource Governor可以在這裏幫助)。 –

+0

另請參閱http://stackoverflow.com/questions/5077051/ado-components-commandtimeout - 'CommandTimeout'可能會有幫助,但它可能取決於其他一些因素。 –

+3

也許你應該避免開始需要花費數小時才能完成的查詢。 –

回答

3

ADO組件中:

我會給一個嘗試異步數據取。當您執行查詢時,您會記得您何時啓動並且每次觸發事件時,都會檢查EventStatus是否仍處於esOK狀態,並檢查查詢執行過程中的時間。如果已過,則可以使用數據集上的Cancel方法取消數據提取。

我的意思是使用類似下面的(未經測試)僞代碼:

var 
    FQueryStart: DWORD; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    // configure the asynchronous data fetch for dataset 
    ADOQuery1.ExecuteOptions := [eoAsyncExecute, eoAsyncFetchNonBlocking]; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    // store the query execution starting time and execute a query 
    FQueryStart := GetTickCount; 
    ADOQuery1.SQL.Text := 'SELECT * FROM Table'; 
    ADOQuery1.Open; 
end; 

procedure TForm1.ADOQuery1FetchProgress(DataSet: TCustomADODataSet; Progress, 
    MaxProgress: Integer; var EventStatus: TEventStatus); 
begin 
    // if the fetch progress is in esOK (adStatusOK) status and the time since 
    // the query has been executed (5000 ms) elapsed, cancel the query 
    if (EventStatus = esOK) and (GetTickCount - FQueryStart >= 5000) then 
    DataSet.Cancel; 
end; 
+0

嗯,'取消'的文檔說:「取消對活動記錄的修改,如果這些更改尚未發佈。」你確定它可以用於這裏的預期目的 - 取消下一次獲取? – ain

+1

感謝分享這個想法,我會試試它 – Franz