oracle 查询优化改写
-----------书籍: oracle 查询优化改写
-----------第1个“C###oracle”为登录数据库的用户名,第2个“oracleChange”为登录数据库的密码“oracleChange”为欲登录的数据库名称。
/*
create tablespace oracleChange
datafile 'F:\devlopment\databases\oracle\oracleChange\oracleChange.def' size 100M --生成数据文件并定义文件大小
autoextend on next 100M maxsize unlimited logging --设置自动扩展
extent management local autoallocate
segment space management auto;
create user C###oracle identified by oracleChange default tablespace oracleChange quota 500m on users;
---- 这里第一个PERSONNEL_MANAGE为用户名,第二个MWQ为密码,第三个DBSQL为表空间名。然后执行。
grant all privileges to C###oracle;
--- 执行该语句给PERSONNEL_MANAGE用户授权,此时PERSONNEL_MANAGE用户就可以登录了。
---创建 emp员工表
create table emp (
empno number(4) ,
ename varchar2(10),
job varchar2(9),
mgr number(4),
hiredate date ,
sal number(7,2),
comm number(7,2) default 0 ,
deptno number(2) );
-- Add comments to the columns
comment on column emp.empno is '编码';
comment on column emp.ename is '名称';
comment on column emp.job is '工作';
comment on column emp.mgr is '主管';
comment on column emp.hiredate is '聘用日期';
comment on column emp.sal is '工资';
comment on column emp.comm is '提成';
comment on column emp.deptno is '部门编码';
----------------------------------------
----插入数据
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'smith', 'clerk', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'allen', 'salesman', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600, 300, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'ward', 'salesman', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250, 500, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7556, 'jones', 'manager', 7698, to_date('02-04-1981', 'dd-mm-yyyy'), 2975, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'maritn', 'salesman', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250, 1400, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'blake', 'manager', 7839, to_date('01-01-1981', 'dd-mm-yyyy'), 2850, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7782, 'clark', 'manager', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450, null, 10);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'scott', 'analyst', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'king', 'president', null, to_date('17-11-9181', 'dd-mm-yyyy'), 5000, null, 10);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'turner', 'salesman', 7698, to_date('08-09-9181', 'dd-mm-yyyy'), 1500, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'adams', 'clerk', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'james', 'clerk', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'ford', 'analyst', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'milier', 'clerk', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300, null, 10);
*/
-----从表中检索部分行 (查询数据时 只需要添加过滤条件即可)
select * from emp where job ='salesman';
----查找空值
select * from emp where comm is null;---null 不支持加减乘除 大小比较 相等比较 否则只能为空
select * from emp where comm is not null;--- 不为空值查询
---将空值转换为实际值
select nvl(emp.comm,0),emp.* from emp where comm is null;---单列 查询
select coalesce(comm,comm) as c ,emp.* from emp where comm is null;
----查找满足多个条件的行
--- 查询 员工表 部门为10 的所有员工、所有得到提成的员工、以及部门为20的工资不超过2000美元的员工;
select *
from emp
where (deptno = 10
or comm is not null
or (sal <= 2000 and deptno = 20));
--- 从表中检索部分列 <在实际开发中;常常只需返回部分需要的列的数据>
select empno ,ename,hiredate,sal from emp where deptno =10;
--- 为列取 有意义的名称
select ename as 姓名 ,deptno as 部门编号 from emp order by 2 ;
----在where子句中引用取别名的列
select * from (select sal as 工资,comm as 提成 from emp ) x where 工资 <1000;
----拼接列
select ename || '的工作是' ||job as msg from emp where deptno =10;
---动态生成 删除表数据的语句
--- select 'truncate table '|| owner ||'.'|| table_name || ';'as 清空表 from all_tables;
----在select 语句中使用条件逻辑
select 档次,count(*) as 人数
from (select (case
when sal <=1000 then '0000-1000'
when sal <=2000 then '1000-2000'
when sal <=3000 then '2000-3000'
when sal <=4000 then '3000-4000'
when sal <=5000 then '4000-5000'
else '好高'
end ) as 档次,ename,sal from emp )
group by 档次
order by 1;
----限制 返回的行数 (rownum 依次返回的每一条数据做一个标识;)
select * from emp where rownum <=2;---取出前2行的数据
select * from (select rownum as sn ,emp.* from emp where rownum <=100) where sn=2;---取出第二行的数据
----从表中随机返回n条记录(先dbms_random 来对数据进行随机排序,然后取其中三行)
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
---------等价于下面语句 ---下面语句执行顺序: 1:select 2:rownum 3:order by ;即:先取出数据 然后生成序号 最后才是排序
select empno ,ename,dbms_random.value ran from emp where rownum <=3 order by ran;
-----模糊查询 like '% 表示任意数量的字符' '_表示任意一个字符' 'M 表示任意长度的字符'
---创建视图
create or replace view empView1 as
select 'abcedf' as vname from dual
union all
select '_bcefg' as vname from dual
union all
select '_bcedf' as vname from dual
union all
select '_\bcedf' as vname from dual
union all
select 'xyceg' as vname from dual ;
--- 要求1:查出vname 中包含字符串 ’ced‘的
select * from empView1 where vname like '%ced%';
--- 要求2:查出vname z中包含字符串“_bce” 的
select * from empView1 where vname like '\_bce%' escape '\'; ---注:escape 把'\'标识为转义字符 ;而'\'把'_'转义为'字符';而非其原意
-----以指定的次序返回查询结果
---实际提取数据或者生产报表时;一般是根据一定顺序查看
---如:查看单位所有员工信息
select empno,ename,hiredate from emp where deptno=10 order by hiredate asc;
select empno,ename,hiredate from emp where deptno=10 order by 3 asc; ---该写法 中的数字只能出现在order by 中
------
select empno ,ename,sal from emp where deptno=10 order by 3 asc;---
select empno,ename ,sal from emp where comm is not null order by 3 asc;
----注:如果order by 后使用的列名 就需要注意前后保持一致;否则开发中带来些 小麻烦
--- 按多个字段排序、 ordey by desc/asc/number
---按部门编号升序并按工资降序排序
select empno ,deptno ,ename,job from emp order by 2 asc,3 desc;
-----按子串排序
select last_name as 名称,
phone_number as 号码,
substr(phone_number, -4) as 尾号
from hr.employees
where rownum <= 5
order by 4;
----translate
---translate(expr,from_string,to_string) --from_string to_string 以字符为单位 对应字符一一替换
select translate('ab您好!bcadefg','abcdefg','1234567890') as new_str from dual;
---如果to_string 为空 则返回空值
select translate('ab您好!bcadefg','abcdefg','') as new_str from dual;
---如果to_string 对应的位置没有字符,删除from_string 中列出的字符
select translate('ab您好!bcadefg','abcdefg','1') as new_str from dual;
----按数字和字母混合字符串中的字母排序
create or replace view numberView
as
select empno || '' || ename as data from emp;
---- 查看视图
select * from numberView;
---要求:按其中字母排序
select nv.data ,translate(nv.data,'-1234567890','-') as ename from numberView nv order by 2;
select nv.data from numberView nv order by translate(nv.data,'-1234567890','-');
---处理排序空值
select ename,sal,comm,nvl(comm,-1) order_col from emp order by 4 asc;
select * from emp order by nvl(comm,-1) desc;
---使用关键字 nulls first; nulls last
select * from emp order by comm nulls first;
select * from emp order by comm nulls last;
oracle 查询优化改写的更多相关文章
- 《Oracle查询优化改写技巧与案例》学习笔记-------使用数字篇
一个系列的读书笔记,读的书是有教无类和落落两位老师编写的<Oracle查询优化改写技巧与案例>. 用这个系列的读书笔记来督促自己学习Oracle,同时,对于其中一些内容,希望大家看到以后, ...
- 【书评:Oracle查询优化改写】第14章 结尾章
[书评:Oracle查询优化改写]第14章 结尾章 一.1 相关参考文章链接 前13章的链接参考相关连接: [书评:Oracle查询优化改写]第一章 http://blog.itpub.net/26 ...
- 【书评:Oracle查询优化改写】第五至十三章
[书评:Oracle查询优化改写]第五至十三章 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知 ...
- 【书评:Oracle查询优化改写】第四章
[书评:Oracle查询优化改写]第四章 BLOG文档结构图 一.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① check的 ...
- 【书评:Oracle查询优化改写】第三章
[书评:Oracle查询优化改写]第三章 BLOG文档结构图 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① 隐含参数 ...
- 【书评:Oracle查询优化改写】第二章
[书评:Oracle查询优化改写]第二章 BLOG文档结构图 在上一篇中http://blog.itpub.net/26736162/viewspace-1652985/,我们主要分析了一些单表查询的 ...
- 《 Oracle查询优化改写 技巧与案例 》电子工业出版社
第1章单表查询 11.1 查询表中所有的行与列 11.2 从表中检索部分行 21.3 查找空值 31.4 将空值转换为实际值 41.5 查找满足多个条件的行 51.6 从表中检索部分列 61.7 为列 ...
- Oracle查询优化改写--------------------报表和数据仓库运算
一.行转列 二.列传行 '
- Oracle查询优化改写--------------------高级查询
一.给结果集分页 二.重新生成房间号 三.跳过表中n行 四.排列组合去重
随机推荐
- 2238"回文字串"报告
题目描述: 回文串,就是从前往后和从后往前看都是一样的字符串.那么现在给你一个字符串,请你找出该字符串中,长度最大的一个回文子串. 输入描述: 有且仅有一个仅包含小写字母的字符串,保证其长度不超过50 ...
- Maven基础
Maven基础 maven核心内容:依赖管理. Maven是Apache组织的开源项目,是项目构建工具.用来管理jar包之间的相互依赖关系 Maven是一个项目构建和管理的工具,提供了帮助管理,构建, ...
- sleep() 和 wait() 有什么区别?
sleep()方法是使线程停止一段时间的方法.在sleep 时间间隔期满后,线程不一定立即恢复执行.这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非"醒来"的 ...
- async 函数
同步 console.log(1); console.log(2); console.log(3); console.log(4); //异步 ajax 文件读取io操作 console.log(1) ...
- Es6 新增解构赋值
1.数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 要想实现解构,就必须是容器,或者具有可遍历的接口. 以前,为 ...
- Struts2简诉
Struts2框架是基于MVC模式的开源,MVC模式是一种开发方式,主要作用是对组件之间进行隔离,M代表业务逻辑层,V代表视图层,C代表控制层.有利于代码的后期维:Struts2框架的源码主要来于We ...
- WMvare桥接模式固定IP
一.命令行固定IP 1.配置网卡IP地址 在命令行输入"vi /etc/sysconfig/network-scripts/ifcfg-eth0"#eth0为第一块网卡,如果是第二 ...
- (转)Linux(Centos)之安装Java JDK及注意事项
场景:天下事有难易乎?为之,则难者亦易矣:不为,则易者亦难矣.人之为学有难易乎?学之,则难者亦易矣:不学,则易者亦难矣. 1 准备工作 下面配置jdk的方式在具有root权限时候能够执行.如果没有ro ...
- (转)通过Net-Speeder为搬瓦工提升网速
为了解决丢包问题,最简单粗暴的方法就是双倍发送,即同一份数据包发送两份.这样的话在服务器带宽充足情况下,丢包率会平方级降低.直接优点是降低丢包率,直接缺点是耗费双倍流量.一些延伸影响是更容易触发快速恢 ...
- Opencv探索之路(二十):制作一个简易手动图像配准工具
近日在做基于sift特征点的图像配准时遇到匹配失败的情况,失败的原因在于两幅图像分辨率相差有点大,而且这两幅图是不同时间段的同一场景的图片,所以基于sift点的匹配已经找不到匹配点了.然后老师叫我尝试 ...