⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 探讨一下这个sql的执行(统计,join相关).txt

📁 oracle问题集
💻 TXT
📖 第 1 页 / 共 2 页
字号:



由 huaz 于 01-10-10 22:07 发表: 
MINUS IS BETTER
单纯为满足以上的要求,用MINUS会快很多! EXISTS也很快,但取反的NOT EXISTS使其速度大大降低了, NOT IN 和NOT EXISTS 
都要激活条件检索还要取反,耗时巨大,而MINUS则是执行两个简单查询子句,对结果进行相减处理。我刚刚进行过类似的处理。两个表数据量各为70万件,MINUS大约需要8分钟,而其他方法则要花几个小时。


由 ALAN.YANG 于 01-10-10 22:20 发表: 

问题是如果不是单纯的count,还要sum其他列呢?
而且table A and B 的结构并不相同!
所以,还是kezizi 的办法好!

__________________
如果ORACLE是大海
那我是在浅水区徘徊......




由 ttdb 于 01-10-10 22:21 发表: 
Re: MINUS IS BETTER
  quote:

  最初由 huaz 发布
  EXISTS也很快,但取反的NOT EXISTS使其速度大大降低了, NOT IN 和NOT EXISTS 都要激活条件检索还要取反,耗时巨大




EXISTS很快,怎么会 NOT EXISTS会很慢?
__________________
whopawho

http://www.itpub.net/search.php
今天你google了没有?



由 kezizi 于 01-10-10 23:07 发表: 
1。单纯的取反不花时间。NOT EXISTS 比EXISTS 慢是因为EXISTS操作在发现第一条符合条件的记录时就返回,NOT EXSITS 
要把所有记录比较才得出结论。读取的数据多。担在上例中不明显。(SUB QUERY 只有一个PAGE〕可以做个试验。让B TABLE只含一条记录,这时NOT 
EXISTS 和EXISTS 应是一样的。否则ORACLE就错了 。 或者有以把EXISTS的B记录放在最后,有同样试验效果。
2。IN和EXISTS 的区别是内部算法不同。我前面说过。
3。MINUS操作我没研究过。我大概想了一下,其内部操作确实是有很多手段可以优化。我的知识老了,得补课了。

但有几点不可怀疑,这也是我为什么自信上面我的SQL 
在同样条件下不会慢很多(就算慢也是同一数量级的,不会是N倍,LOG(N〕倍,或N平方倍,N和页面数有关〕。因为:
1。至少2个TABLE的FULL SCAN 不可少。(没有INDEX〕
2。数据库内部的JOIN算法就那么4种,HASH法对两个大TABLE的JOIN是较有效的,可惜ORACLE做得不是最好。
3。MINUS操作不应该比JOIN快两倍以上,否则 所有的 JOIN 操作就在ORACLE内部应被写成两个MINUS: TABLE A JOIN TABLE B 
=TABLE A - (TABLE A-TABLE 
B〕,假设只需要一个TABLE中的数据返回。这么大的事我应该知道。如果差两被以上,应该是OPTIMIZER的错
4。我的SQL有两个SCAN,一个JOIN (很小的〕不会慢,只有GROUP BY 和 DISTINCT 我不是很有信心是不是最好的,应为有牵涉到TEMP 
TABLESPACE 操作,麻烦大了。如果GROUP BY 和DISTINCT 很慢,可以干脆不要。


由 name 于 01-10-11 00:19 发表: 
使用反结合。。。
替代not in 的最常用办法,not exist在数据量小的时候可以,太大了效果也不好。


由 ttdb 于 01-10-11 01:59 发表: 
某表


select count(*) from TABLE_人口 where not exists ( select 1 from TABLE_人口 where 
性别='男')


select count(*) from TABLE_人口 where exists ( select 1 from TABLE_人口 where 
性别='男')

的查询速度 是不一样的?(男女均分人口)呵呵,我认为not exists子查询也是碰到了第一个结果就返回,不过 就是多了一个 not操作,条件值变反了

比如表里:

性别

.
.
.
...


not exists ( select 1 from TABLE_人口 where 性别='男')
当扫描到第三条纪录时,子查询返回,整个表达时条件值为 假

exists ( select 1 from TABLE_人口 where 性别='男')
也是扫描到第三条纪录时,子查询返回,整个表达时条件值为 真
而已

当然查询结果是完全两样的(前者为0,后者为total)

以上只是就exists 和 not exists 而言,跟贴子 的主题扯远了,sorry啦
__________________
whopawho

http://www.itpub.net/search.php
今天你google了没有?



由 kezizi 于 01-10-11 13:21 发表: 
You are comparing the situation between Exists=True and Not Exists=False
I was talking about the difference between Exists=True and Not Exists=True. 

Different topic


由 ttdb 于 01-10-11 21:35 发表: 
 
Different Condition,Diffenrent topic -->Different Result

how can we draw the conclusions:
"
NOT EXISTS 比EXISTS 慢是因为EXISTS操作在发现第一条符合条件的记录时就返回,NOT EXSITS 要把所有记录比较才得出结论。
"

"
EXISTS也很快,但取反的NOT EXISTS使其速度大大降低了
"

查询速度的快慢 跟具体 子查询有关,怎能 说 exists 就比 not exists快?

not exists ( select 1 from TABLE_人口 where 性别='变态') 

当然很慢。
__________________
whopawho

http://www.itpub.net/search.php
今天你google了没有?



由 kezizi 于 01-10-11 21:57 发表: 
你把我的意思搞混了,我说的和你说的是一个事。
我的意思是:
“EXISTS也很快,但取反的NOT EXISTS使其速度大大降低了 “这句话不对。
如果有人作试验出现EXISTS 比NOT EXISTS快的话,只因为是数据的差别,不是EXISTS 和NOT EXISTS的差别。


由 ttdb 于 01-10-11 23:10 发表: 
 
sorry,

i am really confused ,xixi

原来我们的意见是一致的,哈
__________________
whopawho

http://www.itpub.net/search.php
今天你google了没有?



由 ALAN.YANG 于 01-10-12 00:49 发表: 
想不到无心插柳柳成荫啊!
这个论题反应如此热烈,实在出乎意料!
期望与各位大小虾共同进步!
__________________
如果ORACLE是大海
那我是在浅水区徘徊......




由 jlandzpa 于 01-10-12 08:34 发表: 
 建议用外连接。
建议用外连接代替 not in,
据说速度可以提高30倍,
我也感觉确实快很多。



由 ALAN.YANG 于 01-10-12 16:37 发表: 
Re: 建议用外连接。
  quote:

  最初由 jlandzpa 发布
  建议用外连接代替 not in,
  据说速度可以提高30倍,
  我也感觉确实快很多。
   




想知道具体就这个sql 如何写呢?
__________________
如果ORACLE是大海
那我是在浅水区徘徊......




由 kezizi 于 01-10-12 21:11 发表: 
The outer join, which I mentioned in my original"method 2", should be write like 
this
-------------------------------------
select * from a, b 
where a.c = b.c + 
and b.c is null
-------------------------------------
the above outer join returns same result as
select * from a where a.c not in (select c from b)
-------------------------------------
please let us know the performance. I guess it will not be much faster than the 
3 queries


由 ALAN.YANG 于 01-10-12 21:19 发表: 
  quote:

  最初由 kezizi 发布
  The outer join, which I mentioned in my original"method 2", should be write 
  like this
  -------------------------------------
  select * from a, b 
  where a.c = b.c + 
  and b.c is null
  -------------------------------------
  the above outer join returns same result as
  select * from a where a.c not in (select c from b)
  -------------------------------------
  please let us know the performance. I guess it will not be much faster than 
  the 3 queries 




I will test it ! Give me times !
__________________
如果ORACLE是大海
那我是在浅水区徘徊......




由 smile_lan 于 01-11-20 00:35 发表: 
  quote:

  最初由 kezizi 发布
  select * from a, b 
  where a.c = b.c + 
  and b.c is null
  -------------------------------------





in this sql , why add 'and b.c is null' 
if b.c is alwayse not null, does it posible?


由 ALAN.YANG 于 01-11-20 01:33 发表: 


帮你提一提吧,看看谁能回答.

我是无能为力啦.我给你这么一问,也觉的怪怪的.

奇怪我就怎么没去想个为什么呢?惭愧!惭愧!


__________________
如果ORACLE是大海
那我是在浅水区徘徊......




由 CYT 于 01-11-20 13:46 发表: 
'b.c is null' is the critical condition in this query, otherwise you'll get all 
the rows from a. Besides, why all of you use count(*) instead of counnt(1)? It 
will take tons of unnecessary info into the memory, and slow down the query.
__________________
重在掺和


由 ALAN.YANG 于 01-11-20 16:13 发表: 


Yes ! You are right!

Use count(col1) takes few times than count(*).

It's a mistake .


__________________
如果ORACLE是大海
那我是在浅水区徘徊......




由 smile_lan 于 01-11-20 16:18 发表: 
thank you
i 'll test it 



所有时间均为 +8 hours . 现在时间是 01:52.
Powered by: vBulletin Version 2.0.1
Translated and hacked by:Ztbbs.
Copyright © Jelsoft Enterprises Limited 2000, 2001.
Copyright by itpub.net 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -