oraclein会走索引吗,oracle in会走索引吗
oracle 中 in ,between,大于小于,走不走索引
①.有大量重复值、且经常有范围查询(between, , ,=, =)和order by、group by发生的列,可考虑建立群集索引;
②.经常同时存取多列,且每列都含有重复值可考虑建立组合索引;
③.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列
(1)‘!=’ 将不使用索引. 记住, 索引只能告诉你什么存在于表中, 而不能告诉你什么不存在于表中. (2) ‘||’是字符连接函数. 就象其他函数那样, 停用了索引. (3) ‘+’是数学函数. 就象其他数学函数那样, 停用了索引. (4)相同的索引列不能互相比较,这将会启用全表扫描.
sql in走索引吗
这个问题跟IN无关吧。
你查询的字段需要存在相关索引系统才会走索引的。
你查询的字段必须是原表自动不能用套在函数内查询,否则不走索引。
你要确认你没有执行强制走索引语句。否则你走的索引跟你IN的字段不符也是不会走到索引的。
你查询的表要足够多数据。没上1W的数据系统都默认帮你全表查询了,你还走什么索引。
你一定要在系统表内查询,如果你吧查询出来的结果归到临时聚集再IN,因为临时聚集是走临时表空间,不会走索引的。
oracle中运行sql查询,where条件in()中的字段会不会出发索引
这个要看oracle优化器自己选择是否要使用INDEX了。这个要取决于你表name列的数据唯一性和分布。分以下2种情况。
1.name的唯一性较差:
('name1','name2','name3','name4','name5')条件访问的数据占全表数据的百分比很大,索引访问的总成本大于全表扫描的成本。这时优化器就会选择全表扫描,也就是说不会使index了。
2.name的唯一性较强
('name1','name2','name3','name4','name5')条件访问的数据占全表数据的百分比很小,这时优化器就会选择使用INDEX了,因此比没有INDEX时性能要高。
注意:ORACLE优化器了解表数据分布靠的是统计信息,因此统计信息的准确是十分重要的,否则也会产生错误的选择,导致性能下降。
oracle in和exists的区别
in 和 exists区别
in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B(大表)
1:
select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引;
select * from A where exists(select cc from B where cc=A.cc)
效率高,用到了B表上cc列的索引。
相反的
2:
select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引;
select * from B where exists(select cc from A where cc=B.cc)
效率低,用到了A表上cc列的索引。
带in的关联子查询是多余的,因为in子句和子查询中相关的操作的功能是一样的。如:
select staff_name from staff_member where staff_id in
(select staff_id from staff_func where staff_member.staff_id=staff_func.staff_id);
为非关联子查询指定exists子句是不适当的,因为这样会产生笛卡乘积。如:
select staff_name from staff_member where staff_id
exists (select staff_id from staff_func);
not in 和not exists
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;
而not extsts 的子查询依然能用到表上的索引。
所以无论哪个表大,用not exists都比not in要快。
尽量不要使用not in子句。使用minus 子句都比not in 子句快,虽然使用minus子句要进行两次查询:
select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%');
in 与 "=" 的区别
select name from student where name in ('zhang','wang','li','zhao');
与
select name from student where name='zhang' or name='li' or name='wang' or name='zhao'
的结果是相同的。
如何让Oracle In 语句走索引
SQL select * from tt;
ID NAME
---------- ------------------------------
0 OpiVRDxFYj
1 xFQDNnnzEa
2 qnInbDemzF
3 KwnfZFaDbZ
4 SOlJYxyzcx
5 qJSHeqsMIR
6 JICLOWKqHo
7 MGNqkOkHnE
8 AUwSMpmgrh
9 WkMdkhkwAg
10 mLFpnbYDOS
ID NAME
---------- ------------------------------
11 uthEjWxorG
12 EhbTSwnxml
13 vXSttovVWb
14 HgZZlGHJRc
15 npdczEItnZ
16 MTahLohdZF
17 yEVRfqoicj
18 ZanQNubbmh
19 OIpxJSVHCL
20 MFNmtSfSKc
21 rows selected.
SQL create index idx_tt_id on tt(id);
Index created.
SQL analyze table tt compute statistics;
Table analyzed.
SQL set autot trace
SQL select * from tt where id in (1,10,20);
Execution Plan
----------------------------------------------------------
Plan hash value: 831939183
--------------------------------------------------------------------------------
----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------------------
----------
| 0 | SELECT STATEMENT | | 3 | 36 | 2 (0)|
00:00:01 |
| 1 | INLIST ITERATOR | | | | |
|
| 2 | TABLE ACCESS BY INDEX ROWID| TT | 3 | 36 | 2 (0)|
00:00:01 |
|* 3 | INDEX RANGE SCAN | IDX_TT_ID | 3 | | 1 (0)|
00:00:01 |
--------------------------------------------------------------------------------
----------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("ID"=1 OR "ID"=10 OR "ID"=20)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
5 consistent gets
0 physical reads
0 redo size
689 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
3 rows processed
SQL select * from tt where name in ('npdczEItnZ','EhbTSwnxml');
Execution Plan
----------------------------------------------------------
Plan hash value: 264906180
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 24 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TT | 2 | 24 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("NAME"='EhbTSwnxml' OR "NAME"='npdczEItnZ')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
7 consistent gets
0 physical reads
0 redo size
662 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2 rows processed