简体   繁体   English

FireDAC:加快查询速度的可能性 - Delphi

[英]FireDAC: possibility to make query faster - Delphi

I use bind variables in Delphi and on the other side there is Oracle database with Database link (@dblink).我在 Delphi 中使用绑定变量,而在另一侧有带有数据库链接 (@dblink) 的 Oracle 数据库。 When I build a SELECT statement without bind variables, it has no problem with performance.当我构建一个没有绑定变量的 SELECT 语句时,性能没有问题。 Only if I query SELECT statement with bind variables it takes very long (sometimes 1-2 hour).仅当我使用绑定变量查询 SELECT 语句时才需要很长时间(有时 1-2 小时)。 Is it possible in FireDAC to make the query faster without changing SQL-query?在 FireDAC 中是否可以在不更改 SQL 查询的情况下加快查询速度? I need to use bind variables to avoid SQL injection.我需要使用绑定变量来避免 SQL 注入。

This SQL is very Fast in Delphi: 
SELECT
 DLGH_START_D Datum,
 TRUNC((d.DLGH_ENDE_D - d.DLGH_START_D) * 24 * 60)||' Min '||
 TRUNC(MOD(((d.DLGH_ENDE_D - d.DLGH_START_D) * 24 * 3600), 60))||' Sek' Dauer
FROM
  dialoghistory d
WHERE
 d.DLGH_PARAMETER_C = 'Name of Parameter' AND  <--
 d.dlgh_funktion_c = 'SQLS' AND
 d.DLGH_START_D > '01.02.2020'  <--
order by 1

This SQL is very slow in Delphi:
SELECT
 DLGH_START_D Datum,
 TRUNC((d.DLGH_ENDE_D - d.DLGH_START_D) * 24 * 60)||' Min '||
 TRUNC(MOD(((d.DLGH_ENDE_D - d.DLGH_START_D) * 24 * 3600), 60))||' Sek' Dauer
FROM
  dialoghistory d
WHERE
 d.DLGH_PARAMETER_C = :B_Name AND  <--
 d.dlgh_funktion_c = 'SQLS' AND
 d.DLGH_START_D > :Datum    <--
order by 1


    ---------------------------
    //slow execution period ,  because of bind variables (1 h)
    qry := TFDQuery.CreateSQL(Application, sSqlText_.Text);
    with qry do begin 
    ...
     Param.AsString := value; //set value of bind variable
    ...
    Open;

Table dialoghistory looks like this表对话历史看起来像这样表对话历史

My Solution:我的解决方案:

tmpQuery := TFDQuery.Create(self);  // Create Query 
tmpQuery.Sql.Text := 'SELECT * FROM dlgHistory d where d.DLGH_PARAMETER = :par';

...
// Set Data Type, Param Type and Size yourself 
 with tmpQuery.Params do begin
  Clear;
   with Add do begin
    Name := 'par';
    DataType := ftString;
    Size := 128;
    ParamType := ptInput;
   end;
  end;

tmpQuery.Params[0].AsString := 'Value';  //assign a value

tmpQuery.Prepare;
tmpQuery.Open; //And it works perfect! 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM