Redian新闻
>
一个oracle performance 的问题。
avatar
一个oracle performance 的问题。# Database - 数据库
a*0
1
今天去了downtown boston的一家体检中心做移民体检,填好表格,挨了两针(Measles
,Mumps,Rubella)花了117刀,就结束了!
听说不是都要抽血的吗?
avatar
s*0
2
【 以下文字转载自 tuolaji 俱乐部 】
发信人: snowman10 (雪人), 信区: tuolaji
标 题: 天使的翅膀
发信站: BBS 未名空间站 (Mon Feb 23 17:43:46 2015, 美东)
http://okehero.com/main/play.jsp?id=O7Fs3wF1nOEO
avatar
m*i
3
oracle 9i。
我有一个大table 叫 TA 吧, 7,8 个million record 吧。
table 有primary key。 由好几个column 组成。就叫 (col1,col2,col3,col4,
col5,col6) 吧。
当我运行如下query 时,非常慢,要十几分钟。
1) select count(*) from TA a where a.col1=601
但当我运行下面的query 是 却非常快,十几秒就行了。
2) select count(*) from TA a where a.col1=601 and a.col2 like 'ABC%'
为什么呢? 有什么办法可以让 1) 也运行的快点吗?
avatar
p*m
4
You were fooled.
avatar
c*t
5
check out your execution plan. It seems the first query does full table scan.

【在 m*****i 的大作中提到】
: oracle 9i。
: 我有一个大table 叫 TA 吧, 7,8 个million record 吧。
: table 有primary key。 由好几个column 组成。就叫 (col1,col2,col3,col4,
: col5,col6) 吧。
: 当我运行如下query 时,非常慢,要十几分钟。
: 1) select count(*) from TA a where a.col1=601
: 但当我运行下面的query 是 却非常快,十几秒就行了。
: 2) select count(*) from TA a where a.col1=601 and a.col2 like 'ABC%'
: 为什么呢? 有什么办法可以让 1) 也运行的快点吗?

avatar
a*g
6
你这个体检检的不对,挨的针 也不对。

Measles

【在 a**********0 的大作中提到】
: 今天去了downtown boston的一家体检中心做移民体检,填好表格,挨了两针(Measles
: ,Mumps,Rubella)花了117刀,就结束了!
: 听说不是都要抽血的吗?

avatar
m*i
7
Why?
col1 is part of the primary key, it suppose to be indexed.

scan.

【在 c**t 的大作中提到】
: check out your execution plan. It seems the first query does full table scan.
avatar
c*t
8
if there are too many records selected, oracle may choose full scan instead.

【在 m*****i 的大作中提到】
: Why?
: col1 is part of the primary key, it suppose to be indexed.
:
: scan.

avatar
c*d
9
单字段的primary key能保证该字段被index
对于复合PK,如果PK是(col1,col2),没问题,查询可以用Index
如果PK是(col2,col1),不行,查询不可以用该Index

【在 m*****i 的大作中提到】
: Why?
: col1 is part of the primary key, it suppose to be indexed.
:
: scan.

avatar
i*d
10
对Oracle不懂, 如果是SQL server的话, coolbid说的是对的.

【在 c*****d 的大作中提到】
: 单字段的primary key能保证该字段被index
: 对于复合PK,如果PK是(col1,col2),没问题,查询可以用Index
: 如果PK是(col2,col1),不行,查询不可以用该Index

avatar
a9
11
想想就是这样的。

【在 i***d 的大作中提到】
: 对Oracle不懂, 如果是SQL server的话, coolbid说的是对的.
avatar
y*w
12
这个不绝对的,

【在 c*****d 的大作中提到】
: 单字段的primary key能保证该字段被index
: 对于复合PK,如果PK是(col1,col2),没问题,查询可以用Index
: 如果PK是(col2,col1),不行,查询不可以用该Index

avatar
B*g
13
顶,coolbid这次说的不对。

【在 y****w 的大作中提到】
: 这个不绝对的,
avatar
a9
14
那这个是啥原因呢?

【在 B*****g 的大作中提到】
: 顶,coolbid这次说的不对。
avatar
B*g
15
信息不全,不知道原因。起码也要看了execution plan,和index才能知道。

【在 a9 的大作中提到】
: 那这个是啥原因呢?
avatar
m*i
16
都在公司电脑里,我不用公司电脑上买买提。明天试着考出来看看。

【在 B*****g 的大作中提到】
: 信息不全,不知道原因。起码也要看了execution plan,和index才能知道。
avatar
c*d
17
好,更精确的说:
单字段的primary key能保证该字段被index,但是oracle未必一定使用该Index
对于复合PK,如果PK是(col1,col2),查询可以用Index,但是oracle未必一定使用该
Index
如果PK是(col2,col1),oracle查询不用该Index,除非你加入hint
这下大家满意了吧

【在 B*****g 的大作中提到】
: 顶,coolbid这次说的不对。
avatar
B*g
18
ding

【在 c*****d 的大作中提到】
: 好,更精确的说:
: 单字段的primary key能保证该字段被index,但是oracle未必一定使用该Index
: 对于复合PK,如果PK是(col1,col2),查询可以用Index,但是oracle未必一定使用该
: Index
: 如果PK是(col2,col1),oracle查询不用该Index,除非你加入hint
: 这下大家满意了吧

avatar
y*w
19
你确认oracle中即使如select count(*) from tab where col1 = xx 也不会用到如(col2,col1)的索引?

【在 c*****d 的大作中提到】
: 好,更精确的说:
: 单字段的primary key能保证该字段被index,但是oracle未必一定使用该Index
: 对于复合PK,如果PK是(col1,col2),查询可以用Index,但是oracle未必一定使用该
: Index
: 如果PK是(col2,col1),oracle查询不用该Index,除非你加入hint
: 这下大家满意了吧

avatar
gy
20
ft, 北京MM很picky呀.....哈哈哈

【在 B*****g 的大作中提到】
: 顶,coolbid这次说的不对。
avatar
gy
21


【在 c*****d 的大作中提到】
: 好,更精确的说:
: 单字段的primary key能保证该字段被index,但是oracle未必一定使用该Index
: 对于复合PK,如果PK是(col1,col2),查询可以用Index,但是oracle未必一定使用该
: Index
: 如果PK是(col2,col1),oracle查询不用该Index,除非你加入hint
: 这下大家满意了吧

avatar
B*g
22
from 9i, there is one thing called "Index Skip Scan", oracle may choose this
one over full table scan

col2,col1)的索引?
用该

【在 y****w 的大作中提到】
: 你确认oracle中即使如select count(*) from tab where col1 = xx 也不会用到如(col2,col1)的索引?
avatar
B*g
23
ding

【在 gy 的大作中提到】
: ft, 北京MM很picky呀.....哈哈哈
avatar
y*w
24
;)
如果连这个都不支持,oracle的优化器就没资格称为一个优秀的优化器。
lz那个问题,我怀疑是他的primary key太多列以至于编译器选择table scan了。更新表/索引的详细统计信息,设计小的索引,这些常规手段应该就会有效的,

this

【在 B*****g 的大作中提到】
: from 9i, there is one thing called "Index Skip Scan", oracle may choose this
: one over full table scan
:
: col2,col1)的索引?
: 用该

avatar
y*w
25
sql server在这种情况还是会积极考虑用索引的, 假如那个索引够简单,取值的选择性也够强,

【在 i***d 的大作中提到】
: 对Oracle不懂, 如果是SQL server的话, coolbid说的是对的.
avatar
a9
26
不是特别懂数据库的原理,但是像两键索引。直观感觉上,是按第一个键排序,第一个
键一样才会按第二个键排序,数据库不会建两个索引在上面吧?

择性也够强,

【在 y****w 的大作中提到】
: sql server在这种情况还是会积极考虑用索引的, 假如那个索引够简单,取值的选择性也够强,
avatar
y*w
27
那我来解释下吧,很小的一方面: 索引如表一样,是列数据的集合,只是数据结构的
组织不同,还多了对行的引用。如果索引的数据已经足以满足查询的需要,现代的sql
优化器会仔细权衡扫描索引还是扫描表更划算。
换句话说,索引的使用不仅是精确打击,也有类似表扫描的狂轰滥炸用法,只要,这已
经是所有的烂计划中最好的了。

【在 a9 的大作中提到】
: 不是特别懂数据库的原理,但是像两键索引。直观感觉上,是按第一个键排序,第一个
: 键一样才会按第二个键排序,数据库不会建两个索引在上面吧?
:
: 择性也够强,

avatar
s*g
28
If it takes more then 10 minutes, you can try to use dynamic_sampling hint
and set level >= 3 to get the optimal execution plan.
avatar
v*r
29
”col1 is indexed“ is not a guarantee that index will be used. For example,
if there are many rows equals 601, Oracle will not use index, why? because
full table scan will be considered cheaper/faster in that case (Assuming the
statistics are accurate).
Anyway, a quick diff of two execution plans will tell you why one is slower
and one is faster.
avatar
v*r
30
”col1 is indexed“ is not a guarantee that index will be used. For example,
if there are many rows equals 601, Oracle will not use index, why? because
full table scan will be considered cheaper/faster in that case (Assuming the
statistics are accurate).
Anyway, a quick diff of two execution plans will tell you why one is slower
and one is faster.
avatar
v*r
31
”col1 is indexed“ is not a guarantee that index will be used. For example,
if there are many rows equals 601, Oracle will not use index, why? because
full table scan will be considered cheaper/faster in that case.
Anyway, a quick diff of two execution plans will tell you why one is slower
and one is faster.
avatar
n*r
32
1. tell us oracle version
2. check statistics, check table row num
3. post the execution plan
most likely it goes either FTS or Index skip scan.
avatar
e*e
33
do the following and post the plan :
1.
explain plan for
select count(*) from TA a where a.col1=601;
2.
set lines 140 pages 1000;
3. check the plan
@?/rdbms/admin/utlxpls;
Do you see full table scan? If so, try this:
1.
explain plan for
select count(*) from TA a where a.col1=601 and a.col2 like 'ABC%'
2. check the plan
@?/rdbms/admin/utlxpls;
Do you see index access path?
If so, use hint to enable index access path used in the second query.
explain plan for
select /*+ index(TA index_name) */ cou
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。