delphi - 如何使用 Delphi(BDE 组件)优化更新 Postgres?

标签 delphi postgresql query-optimization delphi-7

我在 Delphi 7 和 PostgreSQL 9.0 中工作,我的数据库中有五十个表。我有一种情况,我必须同时执行五十多个更新查询,每个表一个。

我有一个程序:

var
sTheQuery : string;
begin 
sTheQuery    :='update diary set remark = replace(remark,'+#39+'%'+#39+', '$')';
QueryImages.SQL.Clear;
QueryImages.SQL.Text:=sTheQuery;
QueryImages.ExecSQL;

sTheQuery    :='update bioschema set note = replace(note,'+#39+'%'+#39+', '$')';
QueryImages.SQL.Clear;
QueryImages.SQL.Text:=sTheQuery;
QueryImages.ExecSQL;
sTheQuery    :='update displaymaps set region = replace(region, '+#39+'%'+#39+', '$')';
QueryImages.SQL.Clear;
QueryImages.SQL.Text:=sTheQuery;
  QueryImages.ExecSQL;
sTheQuery    :='update ecosystem set description = replace(description,'+#39+'%'+#39+', '$')';
QueryImages.SQL.Clear;
QueryImages.SQL.Text:=sTheQuery;
  QueryImages.ExecSQL;

.
.
// total 50 times
end;

这种方法是一种改进吗?:

    var
    sTheQuery   : string;
    begin
    sTheQuery    :='update diary set remark = replace(remark,'+#39+'%'+#39+', '$');';
    sTheQuery    :=sTheQuery+'update bioschema set note = replace(note,'+#39+'%'+#39+', '$');';
    sTheQuery    :=sTheQuery+'update displaymaps set region = replace(region, '+#39+'%'+#39+', '$');';
    sTheQuery    :=sTheQuery+'update ecosystem set description = replace(description, '+#39+'%'+#39+', '$');';
    .
    .//total 50 times
    .
    QueryImages.SQL.Clear;
    QueryImages.SQL.Text:=sTheQuery;
     QueryImages.ExecSQL;
    end.

最佳答案

我都不推荐。我会重构我的代码以创建一个接受查询并处理它的方法,就像这个伪代码一样:

executeQuery(String sTheQuery) {
   QueryImages.SQL.Clear;
   QueryImages.SQL.Text:=sTheQuery;
   QueryImages.ExecSQL;
}

doit() {
    executeQuery('update diary set remark = replace(remark,'+#39+'%'+#39+', '$')');
    executeQuery('update bioschema set note = replace(note,'+#39+'%'+#39+', '$')');
    executeQuery('update displaymaps set region = replace(region, '+#39+'%'+#39+', '$')');
    executeQuery('update ecosystem set description = replace(description, '+#39+'%'+#39+', '$')');
} 

这是可维护的、易于阅读和理解的,并且性能可以接受。

关于delphi - 如何使用 Delphi(BDE 组件)优化更新 Postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8515831/

相关文章:

delphi - 在Delphi 2009中使用debug dcus选项?

delphi - Delphi中的继承: Does each of Delphi Subclasses have its own unique copy of the Superclass Private Fields?

sql - 以 'select statement' 格式从函数返回结果

java - 多线程读取文件

MySQL Update 查询需要更多时间来更新表

delphi - 在远程计算机上运行应用程序或进程

postgresql - 在 Postgres 中,当表 A 中的一行被删除时,如何从表 B 中删除一行?

postgresql - 每个数据类型有很多 "precisions"的字段?

mysql - 如何加速大量节点的路径查找?

delphi - 在delphi中使服务自安装