带参数的Delphi PPL TTask过程,ADOQuery

时间:2018-06-22 08:54:49

标签: multithreading delphi ado

这是我上一篇文章的后续内容。我现在在Procedure中有几个ADOQueries和一个ADOConnection。所有这些都是动态创建和释放的。

问题:我希望如果使用4个线程,整个比较过程会更快,但是我的速度与没有线程的情况相同,只是现在它变得更加复杂。还是我在这里做错了什么?

代码在这里,对不起,我知道这有点多了,但是没有代码,我的问题就没有意义了,答案只能是猜测。我知道MSSQL并没有包含在“最终尝试”中,我只想首先检查一下这里是否有速度提升。

按要求粘贴的整个过程。我还更改了它,我在循环外创建对象并在循环后释放对象。

procedure TMain.SYNC(AProgressBar: TProgressBar; AData : array of RemoteDATA);
var i : integer;
    isFound : boolean;
    LStatus, LSU_Stueck, LHistLines , LPosLines, LSTLLines, LTXTLines, LKTXTLines : integer;
    ABQuery , HistQuery, PosQuery, STLQuery, TXTQuery, KTXTQuery : TADOQuery;
    MSSQL : TADOConnection;
begin
  MSSQL := TADOConnection.Create(nil);
  MSSQL.ConnectionString:='FILE NAME='+ExtractFilePath(Application.ExeName)+'\xlr_main.udl';
  MSSQL.Provider:='SQLOLEDB.1';
  MSSQL.KeepConnection:=true;
  MSSQL.LoginPrompt:=false;

  ABQuery := TADOQuery.Create(nil);
  ABQuery.Connection:=MSSQL;

  HistQuery := TADOQuery.Create(nil);
  HistQuery.Connection:=MSSQL;

  PosQuery := TADOQuery.Create(nil);
  PosQuery.Connection:=MSSQL;

  STLQuery := TADOQuery.Create(nil);
  STLQuery.Connection:=MSSQL;

  TXTQuery := TADOQuery.Create(nil);
  TXTQuery.Connection:=MSSQL;

  KTXTQuery := TADOQuery.Create(nil);
  KTXTQuery.Connection:=MSSQL;

  for i := Low(AData) to High(AData) do
    begin
      isFound:=false;
      LStatus:=0;
      LSU_Stueck:=0;
      LHistLines:=0;
      LPosLines:=0;
      LSTLLines:=0;
      LTXTLines:=0;
      LTXTLines:=0;


      ABQuery.SQL.Clear;
      ABQuery.SQL.Add('select AB,STATUS,SU_STUECK,DB_YEAR from BW_AUFTR_KOPF where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
      ABQuery.Open;

      if ABQuery.RecordCount <> 0 then
        begin
          isFound:=true;

          LStatus:=ABQuery.FieldByName('Status').AsInteger;
          LSU_Stueck:=ABQuery.FieldByName('SU_STUECK').AsInteger;
        end;


      HistQuery.SQL.Clear;
      HistQuery.SQL.Add('select COUNT(Datum) as HistLines from BW_AUFTR_HIST where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
      HistQuery.Open;

      LHistLines:=HistQuery.FieldByName('HistLines').AsInteger;

      PosQuery.SQL.Clear;
      PosQuery.SQL.Add('select COUNT(POS_NR) as PosLines from BW_AUFTR_POS where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
      PosQuery.Open;

      LPosLines:=PosQuery.FieldByName('PosLines').AsInteger;

      STLQuery.SQL.Clear;
      STLQuery.SQL.Add('select COUNT(POS_NR) as STLLines from BW_AUFTR_STL where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
      STLQuery.Open;

      LSTLLines:=STLQuery.FieldByName('STLLines').AsInteger;

      TXTQuery.SQL.Clear;
      TXTQuery.SQL.Add('select COUNT(POS_NR) as TXTLines from BW_AUFTR_TXT where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
      TXTQuery.Open;

      LTXTLines:=TXTQuery.FieldByName('TXTLines').AsInteger;

      KTXTQuery.SQL.Clear;
      KTXTQuery.SQL.Add('select COUNT(LFD_NR) as KTXTLines from BW_AUFTR_KTXT where AB='+inttostr(AData[i].AB)+' and DB_YEAR='+inttostr(AData[i].DB_YEAR));
      KTXTQuery.Open;

      LKTXTLines:=KTXTQuery.FieldByName('KTXTLines').AsInteger;


      if isFound = true then
        begin
            if (AData[i].STATUS <> LStatus) or (AData[i].SU_STUECK <> LSU_Stueck)
            or (AData[i].HISTLINES <> LHistLines) or (AData[i].POSLINES <> LPosLines) or (AData[i].STLLINES <> LSTLLines)
            or (AData[i].TXTLINES <> LTxtLines) or (AData[i].KTXTLINES <> LKTXTLines) then
              if (AData[i].STATUS < 100) and (AData[i].STATUS <> 98) then
                begin
                  setlength(CHANGED_ARRAY,length(CHANGED_ARRAY)+1);
                  CHANGED_ARRAY[High(CHANGED_ARRAY)].AB:=AData[i].AB;
                  CHANGED_ARRAY[High(CHANGED_ARRAY)].DB_YEAR:=AData[i].DB_YEAR;
                end;

            if (AData[i].STATUS = 98) or (AData[i].STATUS = 117) or (AData[i].STATUS = 900)
            or (AData[i].STATUS = 999) then
              begin
                setlength(DELETE_ARRAY,length(DELETE_ARRAY)+1);
                DELETE_ARRAY[High(DELETE_ARRAY)].AB:=AData[i].AB;
                DELETE_ARRAY[High(DELETE_ARRAY)].DB_YEAR:=AData[i].DB_YEAR;
              end;
        end
      else
        begin
          if (AData[i].STATUS <> 98) and (AData[i].STATUS <> 117) and (AData[i].STATUS <> 900)
          and (AData[i].STATUS <> 999) and (AData[i].STATUS <> 120) then
            begin
              setlength(NEW_ARRAY,length(NEW_ARRAY)+1);
              NEW_ARRAY[High(NEW_ARRAY)].AB:=AData[i].AB;
              NEW_ARRAY[High(NEW_ARRAY)].DB_YEAR:=AData[i].DB_YEAR;
            end;
        end;

      TThread.Queue(TThread.CurrentThread,
      procedure
      begin
        AProgressBar.Position:=i;
      end);

    end;

  ABQuery.Free;
  HistQuery.Free;
  PosQuery.Free;
  STLQuery.Free;
  TXTQuery.Free;
  KTXTQuery.Free;

  MSSQL.Free;
end;

我在这里启动线程,然后等待它完成,但我还必须显示进度。

  if length(RAB_ARRAY) > 10 then
    begin
      Edit1.Text:='Items '+inttostr(length(RAB_ARRAY))+' 1/4 '+inttostr(length(RAB_ARRAY) div 4)+' LeftOver '+inttostr(length(RAB_ARRAY) mod 4);

      Part:=length(RAB_ARRAY) div 4;
      LeftOver:=length(RAB_ARRAY) mod 4;

      setlength(Tasks,4);

      Tasks[0] := TTask.Create(
        procedure
          begin
           SYNC(progressThread1,copy(RAB_ARRAY,0,Part))
          end);
      Tasks[1] := TTask.Create(
        procedure
          begin
            SYNC(progressThread2,copy(RAB_ARRAY,Part-1,Part))
          end);
      Tasks[2] := TTask.Create(
        procedure
          begin
            SYNC(progressThread3,copy(RAB_ARRAY,(2*Part)-2,Part))
          end);
      Tasks[3] := TTask.Create(
        procedure
          begin
            SYNC(progressThread4,copy(RAB_ARRAY,(3*Part)-3,Part+LeftOver))
          end);

      progressThread1.Max:=Part;
      progressThread2.Max:=Part;
      progressThread3.Max:=Part;
      progressThread4.Max:=Part+LeftOver;

      Tasks[0].Start;
      Tasks[1].Start;
      Tasks[2].Start;
      Tasks[3].Start;


      while true do
        begin
          if (Tasks[0].Status = TTaskStatus.Completed) and (Tasks[1].Status = TTaskStatus.Completed)
            and (Tasks[2].Status = TTaskStatus.Completed) and (Tasks[3].Status = TTaskStatus.Completed) then
              break;
          Application.ProcessMessages;
        end;

1 个答案:

答案 0 :(得分:1)

这里的答案是,您需要分析代码和查询以确定真正的瓶颈在这里。很有可能是您的大量SELECT COUNT...查询没有合适的索引,并且SQL Server被迫扫描表,击中磁盘并提供主要的串行瓶颈。