成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Oracle數據庫效率技巧:避免錯誤的索引

數據庫 Oracle
Oracle數據庫中是數據庫索引在Oracle數據庫中發揮著很大的作用,正是數據庫索引的使用加快了Oracle數據庫的工作效率,當然使用索引時必須得選擇正確了,相反,如果使用了錯誤的數據庫索引,不但不能夠加快Oracle數據庫工作效率,還會帶來不好的影響。

導讀:有的時候,使用錯誤的索引會導致Oracle數據庫的效率明顯下降,通過一些方法或者是技巧可以有效的避免這個問題,下文中就為大家帶來避免使用錯誤的數據庫索引的,以提高Oracle數據庫的工作效率。

  這個例子中,如果我想使用idx_a而不是idx_b.

  SQL> create table test
  2 (a int,b int,c int,d int);
  Table created.
  SQL> begin
  2 for i in 1..50000
  3 loop
  4 insert into mytest values(i,i,i,i);
  5 end loop;
  6 commit;
  7 end;
  8 /
  PL/SQL procedure successfully completed.
  SQL> create index idx_a on mytest(a,b,c);
  Index created.
  SQL> create index idx_b on mytest(b);
  Index created.

  如表mytest,有字段a,b,c,d,在a,b,c上建立聯合索引idx_a(a,b,c),在b上單獨建立了一個索引idx_b(b)。

  在正常情況下,where a=? and b=? and c=?會用到索引idx_a,where b=?會用到索引idx_b

  比如:

  SQL> analyze table mytest compute statistics;
  Table analyzed.
  SQL> select num_Rows from user_tables where table_name='MYTEST';
  NUM_ROWS
  ----------
  50000
  SQL> select distinct_keys from user_indexes where index_name='IDX_A';
  DISTINCT_KEYS
  -------------
  50000
  SQL> set autotrace traceonly
  SQL> select d from mytest
  2 where a=10 and b=10 and c=10;
  Execution Plan
  ----------------------------------------------------------
  Plan hash value: 1542625214
  --------------------------------------------------------------------------------
  ------
  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
  |
  --------------------------------------------------------------------------------
  ------

 

  | 0 | SELECT STATEMENT | | 1 | 16 | 2 (0)| 00:0
  0:01 |
  | 1 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 16 | 2 (0)| 00:0
  0:01 |
  |* 2 | INDEX RANGE SCAN | IDX_A | 1 | | 1 (0)| 00:0
  0:01 |
  --------------------------------------------------------------------------------
  ------
  Predicate Information (identified by operation id):
  ---------------------------------------------------
  2 - access("A"=10 AND "B"=10 AND "C"=10)
  Statistics
  ----------------------------------------------------------
  1 recursive calls
  0 db block gets
  4 consistent gets
  0 physical reads
  0 redo size
  508 bytes sent via SQL*Net to client
  492 bytes received via SQL*Net from client
  2 SQL*Net roundtrips to/from client
  0 sorts (memory)
  0 sorts (disk)
  1 rows processed
  SQL> select d from mytest
  2 where b=500;
  Execution Plan
  ----------------------------------------------------------
  Plan hash value: 530004086
  --------------------------------------------------------------------------------
  ------
  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
  |
  --------------------------------------------------------------------------------
  ------
  | 0 | SELECT STATEMENT | | 1 | 8 | 2 (0)| 00:0
  0:01 |
  | 1 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 8 | 2 (0)| 00:0
  0:01 |
  |* 2 | INDEX RANGE SCAN | IDX_B | 1 | | 1 (0)| 00:0
  0:01 |
  --------------------------------------------------------------------------------
  ------
  Predicate Information (identified by operation id):
  ---------------------------------------------------
  2 - access("B"=500)
  Statistics
  ----------------------------------------------------------
  1 recursive calls
  0 db block gets
  4 consistent gets
  0 physical reads
  0 redo size
  508 bytes sent via SQL*Net to client
  492 bytes received via SQL*Net from client
  2 SQL*Net roundtrips to/from client
  0 sorts (memory)
  0 sorts (disk)
  1 rows processed
 

#p#

 

  但是在這樣一個條件下:where a=? and b=? and c=? group by b會用到哪個索引呢?在索引的分析數據不正確(很長時間沒有分析)或根本沒有分析數據的情況下,oracle往往會使用索引idx_b。通過執行計劃的分析,這個索引的使用,將大大耗費查詢時間。

  比如在索引有統計信息,分析數據正確的情況下:

  SQL> select max(d) from mytest
  2 where a=50 and b=50 and c=50
  3 group by b;
  Execution Plan
  ----------------------------------------------------------
  Plan hash value: 422688974
  --------------------------------------------------------------------------------
  -------
  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
  e |
  --------------------------------------------------------------------------------
  -------
  | 0 | SELECT STATEMENT | | 1 | 16 | 2 (0)| 00:
  00:01 |
  | 1 | SORT GROUP BY NOSORT | | 1 | 16 | 2 (0)| 00:
  00:01 |
  | 2 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 16 | 2 (0)| 00:
  00:01 |
  |* 3 | INDEX RANGE SCAN | IDX_A | 1 | | 1 (0)| 00:
  00:01 |
  --------------------------------------------------------------------------------
  -------
  Predicate Information (identified by operation id):
  ---------------------------------------------------
  3 - access("A"=50 AND "B"=50 AND "C"=50)
  Statistics
  ----------------------------------------------------------
  1 recursive calls
  0 db block gets
  3 consistent gets
  0 physical reads
  0 redo size
  513 bytes sent via SQL*Net to client
  492 bytes received via SQL*Net from client
  2 SQL*Net roundtrips to/from client
  0 sorts (memory)
  0 sorts (disk)
  1 rows processed

 

  但如果索引分析數據不正確:

  SQL> select num_rows from user_tables
  2 where table_name='MYTEST';
  NUM_ROWS
  ----------
  50000
  SQL> analyze index idx_a delete statistics;
  Index analyzed.
  SQL> analyze index idx_b delete statistics;
  Index analyzed.
  SQL> select distinct_keys from user_indexes
  2 where index_name in ('IDX_A','IDX_B');
  DISTINCT_KEYS
  -------------
  SQL> select max(d) from mytest where a=50 and b=50 and c=50 group by b;
  Execution Plan
  ----------------------------------------------------------
  Plan hash value: 3925507835
  --------------------------------------------------------------------------------
  -------
  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
  e |
  --------------------------------------------------------------------------------
  -------
  | 0 | SELECT STATEMENT | | 1 | 16 | 2 (0)| 00:
  00:01 |
  | 1 | SORT GROUP BY NOSORT | | 1 | 16 | 2 (0)| 00:
  00:01 |
  |* 2 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 16 | 2 (0)| 00:
  00:01 |
  |* 3 | INDEX RANGE SCAN | IDX_B | 1 | | 1 (0)| 00:
  00:01 |
  --------------------------------------------------------------------------------
  -------
  Predicate Information (identified by operation id):
  ---------------------------------------------------
  2 - filter("A"=50 AND "C"=50)
  3 - access("B"=50)
  Statistics
  ----------------------------------------------------------
  0 recursive calls
  0 db block gets
  3 consistent gets
  0 physical reads
  0 redo size
  513 bytes sent via SQL*Net to client
  492 bytes received via SQL*Net from client
  2 SQL*Net roundtrips to/from client
  0 sorts (memory)
  0 sorts (disk)
  1 rows processed

#p#

 

  我們可以通過如下的技巧避免使用idx_b,而使用idx_a。

  where a=? and b=? and c=? group by b||'' --如果b是字符類型

  where a=? and b=? and c=? group by b+0 --如果b是數字類型

  通過這樣簡單的改變,往往可以是查詢時間提交很多倍

  當然,我們也可以使用no_index提示,相信很多人沒有用過,也是一個不錯的方法:

  SQL> select /*+ no_index(mytest,idx_b) */ max(d) from mytest where a=50 and b=50 and c=50 group by b;
  Execution Plan
  ----------------------------------------------------------
  Plan hash value: 422688974
  --------------------------------------------------------------------------------
  -------
  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
  e |
  --------------------------------------------------------------------------------
  -------
  | 0 | SELECT STATEMENT | | 1 | 16 | 9 (0)| 00:
  00:01 |
  | 1 | SORT GROUP BY NOSORT | | 1 | 16 | 9 (0)| 00:
  00:01 |
  | 2 | TABLE ACCESS BY INDEX ROWID| MYTEST | 1 | 16 | 9 (0)| 00:
  00:01 |
  |* 3 | INDEX RANGE SCAN | IDX_A | 500 | | 1 (0)| 00:
  00:01 |
  --------------------------------------------------------------------------------
  -------
  Predicate Information (identified by operation id):
  ---------------------------------------------------
  3 - access("A"=50 AND "B"=50 AND "C"=50)
  Statistics
  ----------------------------------------------------------
  1 recursive calls
  0 db block gets
  3 consistent gets
  0 physical reads
  0 redo size
  513 bytes sent via SQL*Net to client
  492 bytes received via SQL*Net from client
  2 SQL*Net roundtrips to/from client
  0 sorts (memory)
  0 sorts (disk)
  1 rows processed

上文中主要是以代碼的形式為大家講解的,看起來可能是不太容易理解,大家要深入其中去學習,這個技巧是非常實用的,希望大家能夠從中收獲。

【編輯推薦】

  1. 詳解Oracle數據庫備份不同的恢復特性
  2. 由淺入深講解MySQL數據庫索引的選擇性
  3. Oracle數據庫11g創非集群基準測試世界紀錄
  4. Oracle數據庫開發技術經驗淺談
責任編輯:迎迎 來源: IT專家網論壇
相關推薦

2010-04-07 17:45:22

Oracle位圖索引

2011-03-16 08:54:45

Oracle數據庫索引

2010-10-27 14:15:44

Oracle數據庫效率

2010-04-06 11:19:28

Oracle數據庫

2011-04-02 09:23:19

MySQL數據庫查詢效率

2011-04-02 09:33:13

MySQL數據庫查詢效率

2011-04-02 09:33:08

MySQL數據庫查詢效率

2009-05-18 13:18:54

字符Oracle字符串

2010-04-21 11:43:33

Oracle數據庫

2010-03-18 09:28:14

Oracle數據庫遷移

2011-09-02 10:06:51

OracleSqlLoad常用技巧

2023-11-16 17:12:33

數據庫oracle

2011-03-01 16:30:55

Oracle

2011-03-11 16:25:53

Oracle數據庫

2011-03-17 14:09:25

Oracle數據庫字符

2010-04-19 13:31:42

Oracle索引

2011-04-13 16:48:56

索引數據庫

2010-05-10 18:54:12

Oracle數據庫索引

2011-03-17 16:35:20

Oracle數據庫段管理

2011-03-17 16:19:42

Oracle數據庫管理
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品污www一区二区三区 | 一区二区小视频 | 欧美一级二级视频 | 国产成人叼嘿视频在线观看 | 亚洲区视频 | 久久婷婷国产麻豆91 | 精品欧美乱码久久久久久1区2区 | 91视频网| 欧美一区二区三区视频在线 | 亚洲国产黄 | 国产福利在线播放 | 精品国产一区二区国模嫣然 | 九九亚洲精品 | 日韩av啪啪网站大全免费观看 | 亚洲 精品 综合 精品 自拍 | 久久大 | 久久精品国产一区二区三区不卡 | 三区在线 | 午夜精品久久久久久久久久久久 | 国产精品免费大片 | 国产日韩一区二区 | 久亚州在线播放 | a毛片视频网站 | 在线观看亚洲 | 蜜桃精品视频在线 | 国产一区二区在线看 | 成人在线日韩 | 亚洲欧美日韩久久 | 九色国产 | 日本电影韩国电影免费观看 | www.婷婷| 99re视频在线 | 美女国内精品自产拍在线播放 | 国产观看 | 国产精品久久久久久久久久三级 | 欧美黑人体内she精在线观看 | 免费在线观看一区二区 | 国产高清精品在线 | 91色视频在线观看 | 亚洲精品久久久久久久久久久 | 天堂在线中文字幕 |