|
最初由 lfree 发布
[B]
1.绑定变量的问题。
2。bad sql语句,顺便我们使用的8.1.7
的标准版,不支持索引:
SELECT NVL (MAX (seeno), 0) + 1
FROM r_register
WHERE dept_code = :1
AND noon_code = :2
AND reglevl_code = :3
AND TRUNC (see_date) = :4
原来索引建立在dept_code+noon_code+reglevl_code
显然不合理,最佳的修改sql
see_date between :4 and :5
在see_date建立索引(因为不支持函数索引)。 [/B]
不了解表中各列的分布以及选择性.如果你能够在不同的情况下把执行计划以及相应的统计信息贴出来就更好比较了.
下面是我做的一个实验.
test@SYSTEM_SERVER> set autotrace off
test@SYSTEM_SERVER> set timing on
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> drop table r_register
2 /
表已丢弃。
已用时间: 00: 00: 01.07
test@SYSTEM_SERVER> create table r_register
2 (
3 dept_code char(10),
4 noon_code char(10),
5 reglevl_code char(10),
6 seeno number(10),
7 see_date date
8 )
9 /
表已创建。
已用时间: 00: 00: 00.01
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> declare
2 begin
3 for i in 1..100000 loop
4 insert into r_register values(to_char(mod(i,10000)),'N'||to_char(i),'R'
||to_char(mod(i,50000)),i,sysdate-mod(i,365));
5 end loop;
6 end;
7 /
PL/SQL 过程已成功完成。
已用时间: 00: 01: 46.06
test@SYSTEM_SERVER> commit
2 /
提交完成。
已用时间: 00: 00: 00.00
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> create index idx_r_register_c on r_register(dept_code,noon_c
ode,reglevl_code)
2 /
索引已创建。
已用时间: 00: 00: 45.05
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> analyze table r_register compute statistics for table for al
l indexes
2 /
表已分析。
已用时间: 00: 00: 01.09
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> set autotrace on
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> SELECT NVL (MAX (seeno), 0) + 1
2 FROM r_register
3 WHERE dept_code = '1'
4 AND noon_code = 'N2'
5 AND reglevl_code = 'M3'
6 AND TRUNC (see_date) = to_date('20050101','yyyymmdd')
7 /
NVL(MAX(SEENO),0)+1
-------------------
1
已用时间: 00: 00: 00.01
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=4 Card=1 Bytes=49)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'R_REGISTER' (Cost=4 Ca
rd=1 Bytes=49)
3 2 INDEX (RANGE SCAN) OF 'IDX_R_REGISTER_C' (NON-UNIQUE)
(Cost=3 Card=1)
Statistics
----------------------------------------------------------
102 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
375 bytes sent via SQL*Net to client
153 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
test@SYSTEM_SERVER>
----------------
test@SYSTEM_SERVER> drop index idx_r_register_c
2 /
索引已丢弃。
已用时间: 00: 00: 01.00
test@SYSTEM_SERVER> create index idx_r_register_date on r_register(see_date)
2 /
索引已创建。
已用时间: 00: 00: 27.03
test@SYSTEM_SERVER> analyze table r_register compute statistics for table for al
l indexes
2 /
表已分析。
已用时间: 00: 00: 01.01
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> set autotrace on
test@SYSTEM_SERVER>
test@SYSTEM_SERVER> SELECT NVL (MAX (seeno), 0) + 1
2 FROM r_register
3 WHERE dept_code = '1'
4 AND noon_code = 'N2'
5 AND reglevl_code = 'M3'
6 AND see_date between to_date('20050101 00:00:00','yyyymmdd hh24:mi:ss') and
7 to_date('20050101 23:59:59','yyyymmdd hh24:mi:ss')
8 /
NVL(MAX(SEENO),0)+1
-------------------
1
已用时间: 00: 00: 00.01
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=107 Card=1 Bytes=49)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'R_REGISTER' (Cost=107
Card=1 Bytes=49)
3 2 INDEX (RANGE SCAN) OF 'IDX_R_REGISTER_DATE' (NON-UNIQU
E) (Cost=3 Card=1)
Statistics
----------------------------------------------------------
29 recursive calls
0 db block gets
279 consistent gets
0 physical reads
0 redo size
376 bytes sent via SQL*Net to client
517 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
test@SYSTEM_SERVER>
虽然从时间上看没什么差别,但从统计信息中的consistent gets来看,创建复合索引要优于单索引. |
|