Oracle SQL 查询优化.Part4
一、插入 insert 操作:
1. 复制表结构但不新增数据:
-- 复制表结构但不插入数据
create table emp_new as select * from emp where 1 = 2;
select * from emp_new;
2. 利用 with check option,配合视图,能够为插入数据操作做一些限制:
-- with check optiom 限制数据的插入
insert into (select empno, deptno, empname, empsalary, empdesc
from emp
where emp.deptno <> 'dept02'-- with check option
)
values('emp008', 'dept02', 'Ross', 7000, '对行业发展趋势有较强的洞察力。有统筹全局能力');运行报错:ORA-01402:视图 WITH CHECK OPTIDN违反 where子句
3. 多表插入:
这里讲三种多表插入:a. 无条件 insert;b. 有条件 insert;c. insert first。
- 无条件 insert all
先将 emp 和 emp_bak 清空,再运行下边 sql:
-- 无条件 insert all
insert all
into emp_new1
into emp_new2
select * from emp;此语是将 emp 的数据同一时候插入到 emp_new1、emp_new2 表里,运行 select * from emp_new1 的结果例如以下(emp_new2 的数据集也是如此):
- 有条件 insert all
-- 有条件 insert all
insert all
when empsalary < 5000
then into emp_new1
when empsalary > 3000
then into emp_new2
select * from emp;运行上边 sql 后,工资(empsalary)小于 5000 的员工信息插入 emp_new1。工资(empsalary)大于 3000 的员工信息插入 dept_new2。当中 empsalary 为 4000 的同一时候插入了 emp_new1 和 emp_new2,有时候须要插入指定的表。这个在下边讲:
- insert first
insert first 假设前边有条件符合。后边的表就不会插入相应的行:
-- insert first
insert first
when empsalary < 5000
then into emp_new1
when empsalary > 3000
then into emp_new2
when empsalary > 3000
then into emp_new3
select * from emp;
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
emp_new2 不会插入 empsalary 为 4000 的这条记录,emp_new3 没有记录插入。
二、更新 update 操作:
1. 利用 select 子查询进行 update。须要注意避免全表更新:
数据准备,先将 emp_new1 中全部记录的 empdesc 置为 “未填写”
如今准备依据 emp_new1 表中的记录去更新 emp 表中 empno 相应记录的 empdesc,好多人会写成例如以下 sql:
-- 利用 select 进行 update 进行了全表更新
update emp
set emp.empdesc =
(select empdesc
from emp_new1
where emp.empno = emp_new1.empno
);
上边的结果说明,这个 update 操作的 sql 进行了全表扫描。
对 empno 没有匹配到的记录,均被更新为 null。
其实应该加上 where 语句才是正确的:
update emp
set emp.empdesc =
(select empdesc
from emp_new1
where emp.empno = emp_new1.empno
)
where exists (select 1
from emp_new1
where emp.empno = emp_new1.empno
)
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
2. merge into 语句:
针对上边的语句,能够用 merge into 语句
-- merge into 的实现方法
merge into emp
using (select *
from emp_new1
) e
on (e.empno = emp.empno)
when matched then
update set emp.empdesc = e.empdesc;执行结果和 1 中结果一样,假设做推断。推荐使用 merge into 方法。
由于 merge into 仅仅訪问一次 emp_new1。
三、删除 delete 操作
1. 删除反复记录:
方法有非常多种,这里仅仅说一种,利用分析函数分组。推断 分组序号是否大于 1.
delete
from emp
where rowid in (select rid
from (select rowid as rid,
row_number() over(partition by empsalary order by empno asc) as seq
from emp
)
where seq > 1
);
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
可是,假设须要删除全然同样的两条数据中的一条,须要在 partition by 后边加上全部列名,否则删除哪条数据并不确定,删掉记录是和 order by 语句相关的。
Oracle SQL 查询优化.Part4的更多相关文章
- Oracle SQL 硬解析和子游标
Oracle SQL 硬解析和子游标 What reasons will be happening sql hard parse and generating new child cursors 在一 ...
- Oracle SQL性能优化技巧大总结
http://wenku.baidu.com/link?url=liS0_3fAyX2uXF5MAEQxMOj3YIY4UCcQM4gPfPzHfFcHBXuJTE8rANrwu6GXwdzbmvdV ...
- Oracle SQL执行缓慢的原因以及解决方案
以下的文章抓哟是对Oracle SQL执行缓慢的原因的分析,如果Oracle数据库中的某张表的相关数据已是2亿多时,同时此表也创建了相关的4个独立的相关索引.由于业务方面的需要,每天需分两次向此表中 ...
- Oracle SQL Developer 连接 MySQL
1. 在ORACLE官网下载Oracle SQL Developer第三方数据库驱动 下载页面:http://www.oracle.com/technetwork/developer-tools/sq ...
- Oracle sql连接
inner-join left-outer-join right-outer-join full- ...
- 解决Oracle SQL Developer无法连接远程服务器的问题
在使用Oracle SQL Developer连接远程服务器的时候,出现如下的错误 在服务器本地是可以正常连接的.这个让人想起来,跟SQL Server的一些设计有些类似,服务器估计默认只在本地监听, ...
- Oracle sql语句执行顺序
sql语法的分析是从右到左 一.sql语句的执行步骤: 1)词法分析,词法分析阶段是编译过程的第一个阶段.这个阶段的任务是从左到右一个字符一个字符地读入源程序,即对构成源程序的字符流进行扫描然后根据构 ...
- Oracle SQL explain/execution Plan
From http://blog.csdn.net/wujiandao/article/details/6621073 1. Four ways to get execution plan(anyti ...
- 处理 Oracle SQL in 超过1000 的解决方案
处理oracle sql 语句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超过1000项就会报错.这主要是oracle考虑性能问题做的限制.如果要解 ...
随机推荐
- Something about the microsoft HttpContext domain design
1. HttpContext.Current.Request, Response 2.HttpHandler-> ProcessRequest 3.HttpModule-> Init, I ...
- formal parameter
formal parameter : [3.16] object declared as part of a function declaration or definition that acqui ...
- SDOI2017round1酱油记day0
嗯... 现在是21:12...准备睡了. 睡前写下day0一天如何过的: 早上5点起床到教室早自习,迷迷糊糊的宣誓,背东西,英语听写: 都停课了为什么还要上早自习! 我!想!去!机!房! OI才是我 ...
- 页面css代码
博主原来的页面css代码 (这个是原来的那种效果,差不多弄出来会是这种效果http://www.cnblogs.com/thmyl/) /*simplememory*/ #google_ad_c1, ...
- Codeforces 323C Two permutations
题目描述 You are given two permutations pp and qq , consisting of nn elements, and mm queries of the for ...
- Akka之Circuit Breaker
这周在项目中遇到了一个错误,就是Circuit Breaker time out.以前没有接触过,因此学习了下akka的断路器. 一.为什么使用Circuit Breaker 断路器是为了防止分布式系 ...
- TIDB 安装
https://my.oschina.net/dmdgeeker/blog/718564 SQL 层一直是用 Golang 在开发,存储引擎 TiKV 用的 Rust
- 【Todo】Python中文及Java中文问题及解决方法总结 & 及各种字符编码问题跟踪贴
Python中文编码问题看这里吧:http://www.cnblogs.com/charlesblc/p/6159109.html Mysql中文编码问题可以看这篇:http://www.cnblog ...
- GCC + GDB 调试方法
首先编译程序 多加一个 -g c++ test.cpp -o a -Wall -g 执行时使用 gdb a 此时输入 l 显示所有的代码 l 输入b 加入断点到某一行(break) b 108 运行 ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(三)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(三) 1.准备3个Realm MyR ...