1.INSERT INTO SELECT语句
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量
2.SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中
2.Oracle 索引:
B* 树索引(B* 树中的 ” B “ 不代表二叉( binary ),而代表平衡( b alanced ))
对于位图索引,一个索引条目则使用一个位图同时指向多行。位图索引适用于高度重复而且通常只读的数据(高度重复是指相对于表中的总行数,数据只有很少的几个不同值)
3.优化:
1)减少函数运算次数
select distinct(sqrt(printoperdate)) a from tprint;
select sqrt(b) from(select distinct(printoperdate) b from tprint)
2)减少数学运算次数
select sum(eventseq*1.1) into tcode from tprint;
select sum(eventseq)*1.1 into tcode from tprint;
3)代码外提:
可以将循环中不变的一些代码移到循环外面,这可能包括纯运算或者SQL。
declare
l_parm char(10);
cursor cur_big_loop
is
select * from sales;
begin
for lr in cur_big_loop loop
select parm into l_parm form dic_parameters where ny='2' and type='10';
…
end loop
end;
改为:
declare
l_parm char(10);
cursor cur_big_loop
is
select * from sales;
begin
select parm into l_parm form dic_parameters where ny='2' and type='10';
for lr in cur_big_loop loop
…
end loop
end;
4)减少查询列中子查询的次数:
select
sum(decode((select prod_category from products where prodid=a.prod_id),'elect',1,0)) “elect”,
sum(decode((select prod_category from products where prodid=a.prod_id),'hardware',1,0)) “hardware”,
sum(decode((select prod_category from products where prodid=a.prod_id),'photo',1,0)) “photo”,
sum(decode((select prod_category from products where prodid=a.prod_id),'software',1,0)) “software”,
sum(decode((select prod_category from products where prodid=a.prod_id),'algorithm',1,0)) “algorithm”
from sales a;
改为:
select
sum(decode(b.prod_category,'elect',1,0)) “elect”,
sum(decode(b.prod_category,'hardware',1,0)) “hardware”,
sum(decode(b.prod_category,'photo',1,0)) “photo”,
sum(decode(b.prod_category,'software',1,0)) “software”
from
(select (select prod_category from products where prod_id=a.prod_id) prod_category
from sales a--外部加一个select是为了在子句中用到了sale表.
) b
5)使用returning的好处。
从DML语句处理对象行的数据中间,返回指定列的数据
• INSERT INTO … VALUES (…) RETURNING COL1 INTO :COL1;
• UPDATE … SET … RETURNING COL1 INTO :COL1;
• DELETE … RETURNING COL1 INTO :COL1;
• 可以指定多个返回数据
• …… RETURNING COL1, COL2 INTO :COL1, :COL2;
• 可以指定返回数据保存到数组等数据类型中
• …… RETURNING COL1 INTO :COL1_ARRAY;
• 也适用于处理对象为多行数据的SQL
例如:
declare
seq number;
begin
insert into table1 values
(seq1.nextval,'AAA','BBB');
select seq1.currval into seq from dual;
dbms_output.put_line(seq||'的值已经取得');
end;
改为:
declare
seq number;
begin
insert into table1 values
(seq1.nextval,'AAA','BBB');
returning col1 into seq;
dbms_output.put_line(seq||'的值已经取得');
end;
6)IN OUT NOCOPY
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量
2.SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中
2.Oracle 索引:
B* 树索引(B* 树中的 ” B “ 不代表二叉( binary ),而代表平衡( b alanced ))
对于位图索引,一个索引条目则使用一个位图同时指向多行。位图索引适用于高度重复而且通常只读的数据(高度重复是指相对于表中的总行数,数据只有很少的几个不同值)
3.优化:
1)减少函数运算次数
select distinct(sqrt(printoperdate)) a from tprint;
select sqrt(b) from(select distinct(printoperdate) b from tprint)
2)减少数学运算次数
select sum(eventseq*1.1) into tcode from tprint;
select sum(eventseq)*1.1 into tcode from tprint;
3)代码外提:
可以将循环中不变的一些代码移到循环外面,这可能包括纯运算或者SQL。
declare
l_parm char(10);
cursor cur_big_loop
is
select * from sales;
begin
for lr in cur_big_loop loop
select parm into l_parm form dic_parameters where ny='2' and type='10';
…
end loop
end;
改为:
declare
l_parm char(10);
cursor cur_big_loop
is
select * from sales;
begin
select parm into l_parm form dic_parameters where ny='2' and type='10';
for lr in cur_big_loop loop
…
end loop
end;
4)减少查询列中子查询的次数:
select
sum(decode((select prod_category from products where prodid=a.prod_id),'elect',1,0)) “elect”,
sum(decode((select prod_category from products where prodid=a.prod_id),'hardware',1,0)) “hardware”,
sum(decode((select prod_category from products where prodid=a.prod_id),'photo',1,0)) “photo”,
sum(decode((select prod_category from products where prodid=a.prod_id),'software',1,0)) “software”,
sum(decode((select prod_category from products where prodid=a.prod_id),'algorithm',1,0)) “algorithm”
from sales a;
改为:
select
sum(decode(b.prod_category,'elect',1,0)) “elect”,
sum(decode(b.prod_category,'hardware',1,0)) “hardware”,
sum(decode(b.prod_category,'photo',1,0)) “photo”,
sum(decode(b.prod_category,'software',1,0)) “software”
from
(select (select prod_category from products where prod_id=a.prod_id) prod_category
from sales a--外部加一个select是为了在子句中用到了sale表.
) b
5)使用returning的好处。
从DML语句处理对象行的数据中间,返回指定列的数据
• INSERT INTO … VALUES (…) RETURNING COL1 INTO :COL1;
• UPDATE … SET … RETURNING COL1 INTO :COL1;
• DELETE … RETURNING COL1 INTO :COL1;
• 可以指定多个返回数据
• …… RETURNING COL1, COL2 INTO :COL1, :COL2;
• 可以指定返回数据保存到数组等数据类型中
• …… RETURNING COL1 INTO :COL1_ARRAY;
• 也适用于处理对象为多行数据的SQL
例如:
declare
seq number;
begin
insert into table1 values
(seq1.nextval,'AAA','BBB');
select seq1.currval into seq from dual;
dbms_output.put_line(seq||'的值已经取得');
end;
改为:
declare
seq number;
begin
insert into table1 values
(seq1.nextval,'AAA','BBB');
returning col1 into seq;
dbms_output.put_line(seq||'的值已经取得');
end;
6)IN OUT NOCOPY