Oracle中使用View能缩短查询的时间么
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle中使用View能缩短查询的时间么相关的知识,希望对你有一定的参考价值。
我有一个View是多个表级联的数据,请问直接查询View能否缩短查询级联数据的时间?
普通的View不可以。因为普通的View就是相当于一个映射。实际执行时,还是从各个实体表中取数据。不过可以试试物化View,在物化View下,各个实体表取得的数据已经作成了一个物化表,从物化View中取得数据相当于从一个实体表中取得数据。这样速度会快一些。 参考技术A 不能 参考技术B 好像可以吧。
缩短并优化多个表的查询
我在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行。您也可以使用触发器,但约束更合适。
以上是关于Oracle中使用View能缩短查询的时间么的主要内容,如果未能解决你的问题,请参考以下文章