在处理时间限制的情况下中止 SQL 查询

问题描述 投票:0回答:2

我可以使用 DELPHI、dbgo DB 组件和 SQL Server 数据库服务器编写 SQL 查询(这些查询的处理时间有限)吗?

喜欢

select * from table where ......  

process_time_limit = 5 sec

最好在时间限制内给我 10% 的行,而不是等待数小时以获得完整的查询数据集

sql-server delphi ado
2个回答
3
投票

ADO 组件:

我会尝试异步数据获取。当您执行查询时,您会记得何时开始,每次

OnFetchProgress
事件触发时,您都会检查
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
投票

我在 SQL SERVER 中测试大约在 8 秒内终止查询,有些时间可能需要多一点秒

创建函数 GetUpdatedDate() 返回日期时间2 AS 开始 返回 getdate(); 结束;

select * from dbo.ApplicationLog t1 join (select (dbo.GetUpdatedDate())as currentdate)t2 on (1=1) where DATEDIFF(second, getdate(), t2.currentdate) <= 8

© www.soinside.com 2019 - 2024. All rights reserved.