缩短并优化多个表的查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了缩短并优化多个表的查询相关的知识,希望对你有一定的参考价值。

我在Java中使用PreparedStatement。我有来自多个表的SQL查询。如果可能的话,我怎样才能使这个查询更短更优化? DB是“Oracle 12c数据库”。另外我的任务是使查询sql-injection安全并防止注入。

preparedStatement = connection.prepareStatement("delete from " +
            "TBL_ORG_DATA, " +
            "TBL_ORG_CONTACTS, " +
            "TBL_HEALTH_SERVICE, " +
            "TBL_EDUCATION, " +
            "TBL_LENGTH_WORK, " +
            "TBL_CONTRACTS, " +
            "TBL_EVENTS, " +
            "TBL_TRADE_UNIONS, " +
            "TBL_COMMITTEES, " +
            "TBL_HEALTH_CHECK, " +
            "TBL_ACCIDENTS, " +
            "TBL_VICTIMS, " +
            "TBL_ACCIDENTS_DAMAGE, " +
            "TBL_ATTESTATION, " +
            "TBL_ATTESTATION_FIVE_YEAR, " +
            "TBL_TRANING, " +
            "TBL_TRAINING_MANAGER, " +
            "TBL_TRANING_THREE_YEAR, " +
            "TBL_MANAGEMENT " +
            "where TBL_ORG_DATA.ORG_ID = ? AND\n" +
            "TBL_ORG_CONTACTS.ORG_ID = ? AND\n" +
            "TBL_HEALTH_SERVICE.ORG_ID = ? AND\n" +
            "TBL_EDUCATION.ORG_ID = ? AND\n" +
            "TBL_LENGTH_WORK.ORG_ID = ? AND\n" +
            "TBL_CONTRACTS.ORG_ID = ? AND\n" +
            "TBL_EVENTS.ORG_ID = ? AND\n" +
            "TBL_TRADE_UNIONS.ORG_ID = ? AND\n" +
            "TBL_COMMITTEES.ORG_ID = ? AND\n" +
            "TBL_HEALTH_CHECK.ORG_ID = ? AND\n" +
            "TBL_ACCIDENTS.ORG_ID = ? AND\n" +
            "TBL_VICTIMS.ORG_ID = ? AND\n" +
            "TBL_ACCIDENTS_DAMAGE.ORG_ID = ? AND\n" +
            "TBL_ATTESTATION.ORG_ID = ? AND\n" +
            "TBL_ATTESTATION_FIVE_YEAR.ORG_ID = ? AND\n" +
            "TBL_TRANING.ORG_ID = ? AND\n" +
            "TBL_TRAINING_MANAGER.ORG_ID = ? AND\n" +
            "TBL_TRANING_THREE_YEAR.ORG_ID = ? AND\n" +
            "TBL_MANAGEMENT.ORG_ID = ? AND\n" +                
            "TBL_ORG_DATA.DATE_PERIOD = ? AND\n" +
            "TBL_ORG_CONTACTS.DATE_PERIOD = ? AND\n" +
            "TBL_HEALTH_SERVICE.DATE_PERIOD= ? AND\n" +
            "TBL_EDUCATION.DATE_PERIOD= ? AND\n" +
            "TBL_LENGTH_WORK.DATE_PERIOD = ? AND\n" +
            "TBL_CONTRACTS.DATE_PERIOD = ? AND\n" +
            "TBL_EVENTS.DATE_PERIOD = ? AND\n" +
            "TBL_TRADE_UNIONS.DATE_PERIOD = ? AND\n" +
            "TBL_COMMITTEES.DATE_PERIOD = ? AND\n" +
            "TBL_HEALTH_CHECK.DATE_PERIOD = ? AND\n" +
            "TBL_ACCIDENTS.DATE_PERIOD = ? AND\n" +
            "TBL_VICTIMS.DATE_PERIOD = ? AND\n" +
            "TBL_ACCIDENTS_DAMAGE.DATE_PERIOD = ? AND\n" +
            "TBL_ATTESTATION.DATE_PERIOD = ? AND\n" +
            "TBL_ATTESTATION_FIVE_YEAR.DATE_PERIOD = ? AND\n" +
            "TBL_TRANING.DATE_PERIOD = ? AND\n" +
            "TBL_TRAINING_MANAGER.DATE_PERIOD = ? AND\n" +
            "TBL_TRANING_THREE_YEAR.DATE_PERIOD = ? AND\n" +
            "TBL_MANAGEMENT.DATE_PERIOD = ?");
答案

据我所知在Oracle中你必须为每个表创建单独的delete命令。

delete from a where org_id = 101 and date_period = date '2018-05-25';
delete from b where org_id = 101 and date_period = date '2018-05-25';

如果你可以修改表格,那么你可以使用约束on delete cascade。我不知道你的表是如何组织的,但是如果在其中一个表中你可以定义唯一约束,那么rest可能依赖于它,就像这里:

create table a (
    id number primary key, org_id number, date_period date, cola varchar2(5),
    constraint uq_org_id_period unique (org_id, date_period) );
insert into a values (1, 101, date '2018-05-25', 'DEL');
insert into a values (2, 101, date '2018-05-26', 'PQR');
insert into a values (3, 102, date '2018-06-17', 'CBA');

create table b (
    id number primary key, org_id number, date_period date, colb varchar2(5),
    constraint fk_org_id_period 
        foreign key (org_id, date_period) 
        references a(org_id, date_period) on delete cascade);
insert into b values (1, 101, date '2018-05-26', 'AAXXX');
insert into b values (2, 102, date '2018-06-17', 'PCCQR');
insert into b values (3, 101, date '2018-05-25', 'DEL1');
insert into b values (4, 101, date '2018-05-25', 'DEL2');
insert into b values (5, 102, date '2018-06-17', 'XYZYX');

现在,如果您运行:

delete from a where (org_id, date_period) in ((101, date '2018-05-25'));

不仅会删除表a中的第1行,还会删除b中的第3行和第4行。您也可以使用触发器,但约束更合适。

以上是关于缩短并优化多个表的查询的主要内容,如果未能解决你的问题,请参考以下文章

优化来自多个表的连接查询

大数据量表的优化查询

数据库分库分表策略

跨多个表优化mysql查询

如何使用 LIMIT syntx 优化具有 17 个连接表的复杂查询并限制每个连接的数据

sql 优化