简体   繁体   English

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

[英]How can I optimize updating Postgres using Delphi (BDE components)?

I am working in Delphi 7 and PostgreSQL 9.0, and have fifty tables in my database. 我正在使用Delphi 7和PostgreSQL 9.0,并且数据库中有五十张表。 I have a situation where I have to execute more than fifty update queries at once, one for each table. 我遇到一种情况,我必须一次执行五十个以上的更新查询,每个表一个。

I have a procedure: 我有一个程序:

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;

Is this approach an improvement?: 这种方法是改进吗?:

    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.

I would recommend neither. 我也不推荐。 I would refactor my code to create a method that accepts the query and processes it, like this pseudo code: 我将重构代码以创建一个接受查询并对其进行处理的方法,例如以下伪代码:

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+', '$')');
} 

This is maintainable, easy to read and understand, and it will perform acceptably. 这是可维护的,易于阅读和理解,并且可以令人满意地执行。

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

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