oracle中查询效率的优化Word文档下载推荐.docx

上传人:b****1 文档编号:3705254 上传时间:2023-05-02 格式:DOCX 页数:12 大小:18.25KB
下载 相关 举报
oracle中查询效率的优化Word文档下载推荐.docx_第1页
第1页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第2页
第2页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第3页
第3页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第4页
第4页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第5页
第5页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第6页
第6页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第7页
第7页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第8页
第8页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第9页
第9页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第10页
第10页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第11页
第11页 / 共12页
oracle中查询效率的优化Word文档下载推荐.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

oracle中查询效率的优化Word文档下载推荐.docx

《oracle中查询效率的优化Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《oracle中查询效率的优化Word文档下载推荐.docx(12页珍藏版)》请在冰点文库上搜索。

oracle中查询效率的优化Word文档下载推荐.docx

(2)当出现多个表时,关联表被称之为交叉表,交叉表作为基础表

description_infodi2

andsi.school_id=di.lookup_code(+)

SCHOOL_ID'

以student_info作为基础表,你会发现运行的速度会有很大的差距,

当基础表放在后面,这样的执行速度会明显快很多。

2.where执行顺序

where执行会从至下往上执行

fromstudent_infosi--学生信息表

wheresi.school_id=10--学院ID

and 

si.system_id=100--系ID

摆放where子句时,把能过滤大量数据的条件放在最下边

3.isnull和isnotnull

当要过滤列为空数据或不为空的数据时使用

wheresi.school_idisnull(当前列中的null为少数时用isnotnull,否则isnull)

4.使用表别名

当查询时出现多个表时,查询时加上别名,

避免出现减少解析的时间字段歧义引起的语法错误。

5.where执行速度比having快

尽可能的使用where代替having

select 

fromstudent_infosi

groupbysi.student_id

havingsi.system_id!

=100

andsi.school_id!

=10

(select 

wehresi.system_id!

andsi.school_id!

groupbysi.student_id) 

6. 

*号引起的执行效率

尽量减少使用select*来进行查询,当你查询使用*,

数据库会进行解析并将*转换为全部列。

二、替代优化

1、用>

=替代>

selectui.user_name

fromuser_infoui--员工信息表

whereui.student_id>

selectui.user_name

9

执行时>

=会比>

执行得要快 

2、用UNION替换OR(适用于索引列) 

whereui.student_id=10

union 

whereui.student_id=2 

上面语句可有效避免全表查询

whereui.student_id=10 

orui.student_id=2

如果坚持要用OR,可以把返回记录最少的索引列写在最前面

3、用in代替or

orui.student_id=20

orui.student_id=30

改成

whereui.student_idin(10,20,30)

执行会更有效率

4、UnionAll与Union

UnionAll重复输出两个结果集合中相同记录

如果两个并集中数据都不一样.那么使用UnionAll与Union是没有区别的,

unionAll

但UnionAll会比Union要执行得快

5、分离表和索引 

总是将你的表和索引建立在另外的表空间内 

决不要将这些对象存放到SYSTEM表空间里

三、一些优化技巧

1、计算表的记录数时

selectcount(si.student_id) 

fromStudent_infosi(student_id为索引)

selectcount(*)fromStudent_infosi

执行时.上面的语句明显会比下面没有用索引统计的语句要快

2.使用函数提高SQL执行速度

当出现复杂的查询sql语名,可以考虑使用函数来提高速度

查询学生信息并查询学生(李明)个人信息与的数学成绩排名

如 

selectdi.descriptionstudent_name

(selectres.order_num--排名

fromresultres

whereres.student_id=di.student_id

orderbyresult_math)order_num

anddi.description='

李明'

而且我们将上面order_num排名写成一个fuction时

createorreplacepackagebodyorder_num_pkgis

functionorder_num(p_student_idnumber)return_numberis

v_return_numbernumber;

begin

selectres.order_num--排名

intov_return_number

orderbyresult_math;

returnv_return_number;

exception

whenothersthen

null;

returnnull;

end;

endorder_num_pkg;

执行

order_num_pkg.order_num(di.student_id)order_num

执行查询时的速度也会有所提高 

3.减少访问数据库的次数

执行次数的减少(当要查询出student_id=100的学生和student_id=20的学生信息时)

selectaddress_id

wheresi.student_id=100

wheresi.student_id=20

都进行查询.这样的效率是很低的

而进行

selectsi.address_id,si2.address_id

student_infosi2 

andsi2.student_id=20 

selectdecode(si.student_id,100,address_id)

decode(si.student_id,20,address_id)

fromstudent_infosi

执行速度是提高了,但可读性反而差了..

所以这种写法个人并不太推荐

4、用Exists(NotExists)代替In(NotIn)

在执行当中使用Exists或者NotExists可以高效的进行查询

5、Exists取代Distinct取唯一值的

取出关联表部门对员工时,这时取出员工部门时,出现多条..

selectdistinctdi.dept_name 

fromdepartments_infodi--部门表

user_info 

ui--员工信息表

whereui.dept_no=di.dept_no

可以修改成

selectdi.dept_name

where 

exists(select'

X'

fromuser_infoui--员工信息表

wheredi.dept_no=ui.dept_no)

6、用表连接代替Exists

通过表的关联来代替exists会使执行更有效率

whereexists(select'

x'

fromdepartments_infodi--部门表

wheredi.dept_no=ui.dept_no

andui.dept_cat='

IT'

);

执行是比较快,但还可以使用表的连接取得更快的查询效率

fromdepartments_infodi

whereui.dept_no=di.dept_no

andui.department_type_code='

代码是经测试并进行优化所写,

以上只例子,具体使用还是要针对各个不同的具体的业务使用用Exists(NotExists)代替In(NotIn)

四、索引篇

1、运算导致的索引失效

andsi.student_id+0=100/*student_id索引将失效*/ 

2、类型转换导致的索引失效

anddi.student_id='

100'

student_id为number类型的索引,当执行下列语句,

oracle会自动转换成

anddi.student_id=to_number('

所幸,只是解析并转换类型,并没有导到失效,

但要是写成下面,将会使用其失效

andto_char(di.student_id)='

3、在索引列上进行计算引起的问题

anddi.student_id-2=10

在索引列中进行运算,将会不使用索引而使用全表扫描

而将

anddi.student_id=10+2

将会得到高效的运行速度

4、Isnotnull引起的问题(student_id为索引)

不要把存在空值的列做为索引,否则无法使用索引

whereui.student_idisnotnull--索引失效 

=-1--索引有效

5、Orderby导致索引失效(student_id为索引)

groupbyui.student_id 

而使用

=-1

将使其有效,

在orderby中只存在两种条件下可以使用索引

(ORDERBY中所有的列必须包含在相同的索引中并保持在索引中的排列顺序

ORDERBY中所有的列必须定义为非空.)

6、自动选择索引

如果表中有两个以上(包括两个)索引,其中有一个唯一性索引,而其他是非唯一性.

在这种情况下,ORACLE将使用唯一性索引而完全忽略非唯一性索引.

7、!

=导致索引失效

whereui.student_id!

=0

在Where中使用!

=将会把索引失效

8、%导致的索引失效

anddi.look_codeLike'

%12'

/*look_code为索引,索引将失效*/ 

12%'

/*索引有效*/ 

以上只例子,具体还是要针对各个不同的具体的业务使用

五、oracle中的notExists与Notin的性能巨大差异

NotExists与Notin的作用同样是排除数据,在oracle中使用notin并不象mysql中的执行那么快,如(

selectjt1.doc_num--单据号码

oalc.d

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2