日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle正确使用索引,通过案例学调优之--Oracle中null使用索引

發(fā)布時(shí)間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle正确使用索引,通过案例学调优之--Oracle中null使用索引 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

通過案例學(xué)調(diào)優(yōu)之--Oracle中null使用索引

默認(rèn)情況下,Oracle數(shù)據(jù)庫,null在Index上是不被存儲(chǔ)的,當(dāng)在索引列以“is null”的方式訪問時(shí),無法使用索引;本案例,主要向大家演示如何在存在null的索引列上,使用“is null”訪問索引。

案例分析:

1、建立表和普通索引13:52:23?SCOTT@?prod?>create?table?t2?(x?int,y?int);

Table?created.

14:00:11?SCOTT@?prod?>insert?into?t2?values?(1,1);

1?row?created.

Elapsed:?00:00:00.04

14:00:21?SCOTT@?prod?>insert?into?t2?values?(1,null);

1?row?created.

Elapsed:?00:00:00.00

14:00:31?SCOTT@?prod?>insert?into?t2?values?(null,1);

1?row?created.

Elapsed:?00:00:00.00

14:00:37?SCOTT@?prod?>insert?into?t2?values?(null,null);

1?row?created.

Elapsed:?00:00:00.00

14:00:44?SCOTT@?prod?>commit;

Commit?complete.

Elapsed:?00:00:00.04

14:06:41?SCOTT@?prod?>select?*?from?t2;

X??????????Y

----------?----------

1??????????1

1

1

14:36:12?SCOTT@?prod?>create?index?t2_ind?on?t2(x);

Index?created.

14:49:38?SCOTT@?prod?>select?index_name,table_name,num_rows?from?user_indexes?where?index_name='T2_IND';

INDEX_NAME?????????????????????TABLE_NAME???????????????????????NUM_ROWS

------------------------------?------------------------------?----------

T2_IND?????????????????????????T2??????????????????????????????????????3

在索引中只有3行,在最后一行字段全為null值,沒有被存儲(chǔ)!

14:36:27?SCOTT@?prod?>exec?dbms_stats.gather_index_stats(user,'T2_IND');

PL/SQL?procedure?successfully?completed.

14:37:29?SCOTT@?prod?>select?*?from?t2?where?x=1;

X??????????Y

----------?----------

1??????????1

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?1173409066

--------------------------------------------------------------------------------------

|?Id??|?Operation???????????????????|?Name???|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT????????????|????????|?????2?|?????8?|?????2???(0)|?00:00:01?|

|???1?|??TABLE?ACCESS?BY?INDEX?ROWID|?T2?????|?????2?|?????8?|?????2???(0)|?00:00:01?|

|*??2?|???INDEX?RANGE?SCAN??????????|?T2_IND?|?????2?|???????|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

2?-?access("X"=1)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

4??consistent?gets

0??physical?reads

0??redo?size

519??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

14:37:45?SCOTT@?prod?>select?*?from?t2?where?x?is?not?null;

X??????????Y

----------?----------

1??????????1

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?463061910

--------------------------------------------------------------------------------------

|?Id??|?Operation???????????????????|?Name???|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT????????????|????????|?????2?|?????8?|?????2???(0)|?00:00:01?|

|???1?|??TABLE?ACCESS?BY?INDEX?ROWID|?T2?????|?????2?|?????8?|?????2???(0)|?00:00:01?|

|*??2?|???INDEX?FULL?SCAN???????????|?T2_IND?|?????2?|???????|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

2?-?filter("X"?IS?NOT?NULL)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

4??consistent?gets

0??physical?reads

0??redo?size

519??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

14:38:00?SCOTT@?prod?>select?*?from?t2?where?x?is?null;

X??????????Y

----------?----------

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?1513984157

--------------------------------------------------------------------------

|?Id??|?Operation?????????|?Name?|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT??|??????|?????2?|?????8?|?????3???(0)|?00:00:01?|

|*??1?|??TABLE?ACCESS?FULL|?T2???|?????2?|?????8?|?????3???(0)|?00:00:01?|

--------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?filter("X"?IS?NULL)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

8??consistent?gets

0??physical?reads

0??redo?size

508??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

當(dāng)x通過“is?null”訪問時(shí),Oracle選擇了“full?table?scan”方式。

2、通過建立常量復(fù)合索引14:38:55?SCOTT@?prod?>create?index?t2_ind?on?t2(x,0);

Index?created.

14:49:38?SCOTT@?prod?>select?index_name,table_name,num_rows?from?user_indexes?where?index_name='T2_IND';

INDEX_NAME?????????????????????TABLE_NAME???????????????????????NUM_ROWS

------------------------------?------------------------------?----------

T2_IND?????????????????????????T2??????????????????????????????????????4

索引塊上存儲(chǔ)了表中所用的行。

14:39:50?SCOTT@?prod?>select?*?from?t2?where?x?is?null;

X??????????Y

----------?----------

1

Elapsed:?00:00:00.00

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?1173409066

--------------------------------------------------------------------------------------

|?Id??|?Operation???????????????????|?Name???|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT????????????|????????|?????2?|?????8?|?????2???(0)|?00:00:01?|

|???1?|??TABLE?ACCESS?BY?INDEX?ROWID|?T2?????|?????2?|?????8?|?????2???(0)|?00:00:01?|

|*??2?|???INDEX?RANGE?SCAN??????????|?T2_IND?|?????2?|???????|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

2?-?access("X"?IS?NULL)

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

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

對(duì)于x通過“is?null”訪問時(shí),也能通過索引訪問了!

3、建立復(fù)合索引(其他列為null)13:59:40?SCOTT@?prod?>create?index?x_ind?on?t2(x,y);

Index?created.

14:08:29?SCOTT@?prod?>EXEC?dbms_stats.gather_table_stats(ownname=>USER,tabname=>'T2');

PL/SQL?procedure?successfully?completed.

14:09:22?SCOTT@?prod?>EXEC?dbms_stats.gather_index_stats(ownname=>USER,indname=>'X_IND');

PL/SQL?procedure?successfully?completed.

14:09:58?SCOTT@?prod?>select?index_name,num_rows?from?user_indexes?where?index_name='X_IND';

INDEX_NAME???????????????????????NUM_ROWS

------------------------------?----------

X_IND???????????????????????????????????3

14:10:50?SCOTT@?prod?>select?count(*)?from?t2;

COUNT(*)

----------

4

14:11:28?SCOTT@?prod?>set?autotrace?on

14:12:33?SCOTT@?prod?>select?*?from?t2?where?x=1;

X??????????Y

----------?----------

1??????????1

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?3708139238

--------------------------------------------------------------------------

|?Id??|?Operation????????|?Name??|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT?|???????|?????2?|?????8?|?????1???(0)|?00:00:01?|

|*??1?|??INDEX?RANGE?SCAN|?X_IND?|?????2?|?????8?|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?access("X"=1)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

2??consistent?gets

0??physical?reads

0??redo?size

512??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

14:12:47?SCOTT@?prod?>select?*?from?t2?where?x?is?not?null;

X??????????Y

----------?----------

1??????????1

1

Elapsed:?00:00:00.00

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?3776680409

--------------------------------------------------------------------------

|?Id??|?Operation????????|?Name??|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT?|???????|?????2?|?????8?|?????1???(0)|?00:00:01?|

|*??1?|??INDEX?FULL?SCAN?|?X_IND?|?????2?|?????8?|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?filter("X"?IS?NOT?NULL)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

2??consistent?gets

0??physical?reads

0??redo?size

512??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

14:13:08?SCOTT@?prod?>select?*?from?t2?where?x?is?null;

X??????????Y

----------?----------

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?1513984157

--------------------------------------------------------------------------

|?Id??|?Operation?????????|?Name?|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT??|??????|?????2?|?????8?|?????3???(0)|?00:00:01?|

|*??1?|??TABLE?ACCESS?FULL|?T2???|?????2?|?????8?|?????3???(0)|?00:00:01?|

--------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?filter("X"?IS?NULL)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

8??consistent?gets

0??physical?reads

0??redo?size

508??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

如果,復(fù)合索引列其他列也為null,在查詢使用’is?null‘條件時(shí),仍然為“full?table?scan”。

14:13:52?SCOTT@?prod?>select?*?from?t2?where?x=1?and?y?is?null;

X??????????Y

----------?----------

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?3708139238

--------------------------------------------------------------------------

|?Id??|?Operation????????|?Name??|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT?|???????|?????1?|?????4?|?????1???(0)|?00:00:01?|

|*??1?|??INDEX?RANGE?SCAN|?X_IND?|?????1?|?????4?|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?access("X"=1?AND?"Y"?IS?NULL)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

2??consistent?gets

0??physical?reads

0??redo?size

471??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

1??rows?processed

14:16:16?SCOTT@?prod?>select?*?from?t2?where?x?is?null?and?y=1;

X??????????Y

----------?----------

1

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?3708139238

--------------------------------------------------------------------------

|?Id??|?Operation????????|?Name??|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

--------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT?|???????|?????1?|?????4?|?????1???(0)|?00:00:01?|

|*??1?|??INDEX?RANGE?SCAN|?X_IND?|?????1?|?????4?|?????1???(0)|?00:00:01?|

--------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?access("X"?IS?NULL?AND?"Y"=1)

filter("Y"=1)

Statistics

----------------------------------------------------------

1??recursive?calls

0??db?block?gets

1??consistent?gets

0??physical?reads

0??redo?size

471??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

1??rows?processed

4、建立復(fù)合索引(其他列為 not null)

15:13:38?SCOTT@?prod?>desc?t2;

Name??????????????????????????????????????????????????????????????Null?????Type

-----------------------------------------------------------------?--------?--------------------------------------------

X??????????????????????????????????????????????????????????????????????????NUMBER(38)

Y??????????????????????????????????????????????????????????????????????????NUMBER(38)

15:13:43?SCOTT@?prod?>alter?table?t2?modify?(y?NUMBER(38)?not?null);

Table?altered.

15:14:01?SCOTT@?prod?>desc?t2;

Name??????????????????????????????????????????????????????????????Null?????Type

-----------------------------------------------------------------?--------?--------------------------------------------

X??????????????????????????????????????????????????????????????????????????NUMBER(38)

Y?????????????????????????????????????????????????????????????????NOT?NULL?NUMBER(38)

15:12:54?SCOTT@?prod?>insert?into?t2?values?(1,1);

1?row?created.

Elapsed:?00:00:00.02

15:13:02?SCOTT@?prod?>insert?into?t2?values?(null,1);

1?row?created.

Elapsed:?00:00:00.00

15:13:12?SCOTT@?prod?>insert?into?t2?values?(null,2);

1?row?created.

Elapsed:?00:00:00.00

15:13:36?SCOTT@?prod?>commit;

Commit?complete.

15:15:00?SCOTT@?prod?>create?index?t2_ind?on?t2?(x,y);

Index?created.

15:15:29?SCOTT@?prod?>exec?dbms_stats.gather_table_stats(user,'T2',cascade=>true);

PL/SQL?procedure?successfully?completed.

15:16:09?SCOTT@?prod?>select?index_name,table_name,num_rows?from?user_indexes?where?index_name='T2_IND';

INDEX_NAME?????????????????????TABLE_NAME???????????????????????NUM_ROWS

------------------------------?------------------------------?----------

T2_IND?????????????????????????T2??????????????????????????????????????3

15:17:20?SCOTT@?prod?>set?autotrace?trace

15:17:26?SCOTT@?prod?>SELECT?*?from?t2?where?x?is?null

Elapsed:?00:00:00.00

Execution?Plan

----------------------------------------------------------

Plan?hash?value:?2876512201

---------------------------------------------------------------------------

|?Id??|?Operation????????|?Name???|?Rows??|?Bytes?|?Cost?(%CPU)|?Time?????|

---------------------------------------------------------------------------

|???0?|?SELECT?STATEMENT?|????????|?????2?|????10?|?????1???(0)|?00:00:01?|

|*??1?|??INDEX?RANGE?SCAN|?T2_IND?|?????2?|????10?|?????1???(0)|?00:00:01?|

---------------------------------------------------------------------------

Predicate?Information?(identified?by?operation?id):

---------------------------------------------------

1?-?access("X"?IS?NULL)

Statistics

----------------------------------------------------------

0??recursive?calls

0??db?block?gets

2??consistent?gets

0??physical?reads

0??redo?size

510??bytes?sent?via?SQL*Net?to?client

415??bytes?received?via?SQL*Net?from?client

2??SQL*Net?roundtrips?to/from?client

0??sorts?(memory)

0??sorts?(disk)

2??rows?processed

在復(fù)合索引中,如果其他列為not?null,則在“is?null”條件下,仍然可以使用索引訪問。

結(jié)論:

對(duì)于普通的索引,null值不能進(jìn)行索引的正確理解應(yīng)該是,對(duì)于某一行,索引的所有列的值都是null值時(shí),該行才不能被索引。

總結(jié)

以上是生活随笔為你收集整理的oracle正确使用索引,通过案例学调优之--Oracle中null使用索引的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。