楼主: kerlion

[精华] 100倍以上的性能提高

[复制链接]
论坛徽章:
20
ITPUB元老
日期:2005-02-28 12:57:002012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-02-13 15:10:58马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:10:58
21#
发表于 2002-5-24 10:55 | 只看该作者
To simplify the discussion, let us focus on Nested Loop and Sort Merge first.  Hash Join is quit difficult to discuss here.

Select * from A, B
Where A1=B1

If A1 and B1 are B-Tree indexed on table A and B.  Let consider an isolated case, if you have unlimited memory and everything can be performed in memory, now we count the number of operation for Nested Loop.
We assume that A has 1,000 records and B has 200,000 records.  Let us calculate number of operations for driving path A-> B.  Which means that we open table A, for each record from A to fetch (scan) table B.
Operations= 1000*LN(200,000)/2 ~ 6103

Where LN(200,000) is depth of the B-tree index, LN(200,000)/2 is the average operation to match the right node in tree.

Let’s calculated the driving path for B->A
Operations= 200,000*LN(1,000)/2 ~ 690775

So, it is almost 113 time slower then the A->B.  So, normally, the Nested Loop problem is caused by wrong driving path.  
Let’s consider the following SQL, you will know more why Oracle and other database cannot always make right decision.
Select * from A, B
Where A1=B1 and A1<:var

Actually Oracle will improve your SQL by adding a new condition.
Select * from A, B
Where A1=B1 and A1<:var and B1<:var

So, A->B or B-> A are also be consider, :var is an unknown factor, it also hard to predict how many records will be filtered by A1<:var or B1<:var .  So, the decision is not always correct.  Sometimes you change it Hash Join may find a big improvement, that may not be caused by Hash Join operation, but it may caused by Hash changing your wrong driving path only.  As it is too long, I would leave it here and discuss Sort Merge next time.

Forgive me, if any mistakes I made here, no prove read before publish!

使用道具 举报

回复
论坛徽章:
20
ITPUB元老
日期:2005-02-28 12:57:002012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-02-13 15:10:58马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:10:58
22#
发表于 2002-5-27 16:00 | 只看该作者
Let me finish the Sort Merge Join. I use the same example as Nested Loop.
Select * from A, B
Where A1=B1

If Sort Merge is selected by Oracle optimizer, Oracle will sort table A and B first.  Assume that data in table A and B is in random order, then the number of operations for sorting will be:
Table A ~ N*LN(N) where N is number of in A table
Table B ~ M*LN(M) where M is number of in B table

Number of operations total for A and B= Power(1,000,3/2)+ Power(200,000,3/2) =6907 + 2441214 = 2448121
(To simplify the discussion, let me ignore the Merge operations)

You can see the initial overhead for Sort Merge is larger than Nested Loop, but the growth rate is the almost the same ~ N*LN(N) .  But the point is that sometimes your records inserted are already in a natural order which require less than N*LN(N) move operations for sorting.  So, Sort Merge may faster for those tables which records are sorted(or partially sorted).  Memory is also an additional overhead for Sorting; so many users environment may need extra memory allocation.  Nested Loop is relative stable, less overhead and need less resources (if the driving path is correct), but Sort Merge has less risk for taking wrong driving path, since the Sort A or B first will not affect the path significantly.  So, there is no one single winner, so sometimes you have to teach your Oracle to pick the right plan by Hints or SQL Rewrite.   Hash Join is even more complicated.  Let’s discuss later!

使用道具 举报

回复
论坛徽章:
1
ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41
23#
发表于 2002-8-29 22:50 | 只看该作者
其实我觉的两张大表A X条记录, B   Y 条记录
LOOP  COST X*Y
MERG   可能用的是数据结构中的某一个如 COST LOG N算法分别对两个结果进行排许在MERG
具体那一种算法不得而知
道理有点象乘法与加法的关系
1+2>1*2
5+6<5*6

使用道具 举报

回复
论坛徽章:
2
授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34
24#
发表于 2003-2-12 13:37 | 只看该作者
有个小问题,ORACLE里总是最右边的表是驱动表,也就是说Select * from A, B
where A1=B1 还应该看A表和B表返回的数据行哪个多,如果B表返回的数据行少则B表做驱动表,按照上面的SQL写;如果A表返回的数据少,则应该写成Select * from B,  A Where A1=B1
再按照你的算法进行计算,而不仅仅是根据本身表中的数据来计算。

使用道具 举报

回复
论坛徽章:
0
25#
发表于 2003-7-5 18:13 | 只看该作者

nice

ok

使用道具 举报

回复
论坛徽章:
86
ITPUB元老
日期:2005-02-28 12:57:002012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20咸鸭蛋
日期:2012-05-08 10:27:19版主8段
日期:2012-05-15 15:24:112013年新春福章
日期:2013-02-25 14:51:24
26#
发表于 2003-7-5 18:24 | 只看该作者

o

最初由 aleckqian 发布
[B]有个小问题,ORACLE里总是最右边的表是驱动表,也就是说Select * from A, B
where A1=B1 还应该看A表和B表返回的数据行哪个多,如果B表返回的数据行少则B表做驱动表,按照上面的SQL写;如果A表返回的数据少,则应该写成Select * from B,  A Where A1=B1
再按照你的算法进行计算,而不仅仅是根据本身表中的数据来计算。 [/B]


如果是  rule-based 则跟 顺序有关

如果是 cost-based 则跟顺序无关

使用道具 举报

回复
论坛徽章:
0
27#
发表于 2003-7-7 11:51 | 只看该作者

疑惑

感觉各位大虾还是没有讨论清楚,能不能继续讨论?

使用道具 举报

回复
论坛徽章:
3
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33
28#
发表于 2003-7-8 14:09 | 只看该作者
请问krlion,   use_nl,use_hash怎么用,

能具体的把你使用了NESTED LOOP或HASH的SQL语句写出来吗?

使用道具 举报

回复
论坛徽章:
3
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34
29#
发表于 2004-9-27 23:57 | 只看该作者

遇到一个SQL语句

同样的SQL语句,过一段时间后Oracle改变了执行计划,
一时没有办法,特别慢,只好先用 /* use_merge(A B) */
强制设置执行计划,不是最好的,但能在几十秒中返回结果,
否则的话,这个SQL语句1.5小时都不能出来结果。

涉及多表连接,其中一张500M,一张50M,其他几张表0.5--7M
,很小,不加提示时,以前好好的,不清楚何故,执行计划变了,
区别是做了表分析,其他没干什么。

现在建立index已无用,目前的index是足够的,还需要继续分析。
有些原理没有弄清楚。

使用道具 举报

回复
论坛徽章:
7
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:342008新春纪念徽章
日期:2008-02-13 12:43:03奥运会纪念徽章:羽毛球
日期:2008-05-28 20:12:222009新春纪念徽章
日期:2009-01-04 14:52:282010新春纪念徽章
日期:2010-03-01 11:06:12
30#
发表于 2004-9-28 08:55 | 只看该作者
做了表分析,你这SQL语句的优化模式就变为CBO了呀!

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表