[20170516]nvl與非NULL約束2.txt
[20170516]nvl與非NULL約束2.txt
--//接著上午的測試看看COALESCE看看過濾的情況.
1.環境:
SCOTT@book> @ &r/ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
SCOTT@book> create table t as select rownum id,'test' name , 0 flag_num ,lpad('0',1,'0') flag_varchar from dual connect by level<=2e4;
Table created.
SCOTT@book> update t set flag_num=1, flag_varchar='1' where id=1e4 or id=2e4;
2 rows updated.
SCOTT@book> commit ;
Commit complete.
--//分析表略.
--//選擇lpad函式,這樣資料型別varchar2.
SCOTT@book> @ &r/desc t
Name Null? Type
-------------- -------- -------------
1 ID NUMBER
2 NAME CHAR(4)
3 FLAG_NUM NUMBER
4 FLAG_VARCHAR VARCHAR2(1)
2.設定:
SCOTT@book> alter table t modify FLAG_VARCHAR not null;
Table altered.
SCOTT@book> create index i_t_flag_varchar on t(flag_varchar);
Index created.
--//在flag_varchar欄位建立直方圖:
SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => nvl('',user),TabName => 't',Estimate_Percent => NULL,Method_Opt => 'FOR ALL COLUMNS SIZE 1 for columns flag_varchar size 254 ',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.
SCOTT@book> select * from t where nvl(flag_varchar,0)='1';
ID NAME FLAG_NUM F
---------- -------------------- ---------- -
10000 test 1 1
20000 test 1 1
SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 9y06mwu7wncxx, child number 0
-------------------------------------
select * from t where nvl(flag_varchar,0)='1'
Plan hash value: 932711470
------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 2 (100)| | 2 |00:00:00.01 | 5 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 1 | 13 | 2 (0)| 00:00:01 | 2 |00:00:00.01 | 5 |
|* 2 | INDEX RANGE SCAN | I_T_FLAG_VARCHAR | 1 | 1 | | 1 (0)| 00:00:01 | 2 |00:00:00.01 | 3 |
------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T@SEL$1
2 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("FLAG_VARCHAR"='1')
--//能很好的使用索引.測試使用COALESCE的情況如何?
SCOTT@book> select * from t where COALESCE(flag_varchar,'0')='1';
ID NAME FLAG_NUM F
---------- ---------- ---------- -
10000 test 1 1
20000 test 1 1
SCOTT@book> @&r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID bbq0q2zs1s9j1, child number 1
-------------------------------------
select * from t where COALESCE(flag_varchar,'0')='1'
Plan hash value: 1601196873
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 17 (100)| | 2 |00:00:00.01 | 55 |
|* 1 | TABLE ACCESS FULL| T | 1 | 2 | 26 | 17 (0)| 00:00:01 | 2 |00:00:00.01 | 55 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(COALESCE("FLAG_VARCHAR",'0')='1')
Note
-----
- cardinality feedback used for this statement
--//注意看過濾條件,COALESCE就沒有這樣的特點.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2139140/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20170516]nvl與非NULL約束.txtNull
- 【SQL】15 SQL 約束(Constraints)、NOT NULL 約束、UNIQUE 約束、PRIMARY KEY 約束、FOREIGN KEY 約束、CHECK 約束、DEFAULT約束SQLAINull
- TreeSet的null值與元素型別的約束Null型別
- CHECK約束中的NULL條件Null
- Oracle唯一約束中NULL的處理OracleNull
- Oracle外來鍵約束中NULL的處理OracleNull
- 資料遷移判斷非空約束
- NULL和唯一約束UNIQUE的對應關係Null
- not-null約束對執行計劃的影響Null
- [20170503]]函式COALESCE優於NVL 2.txt函式
- Javaweb-約束-外來鍵約束JavaWeb
- 線上重定義拷貝表結構的NOT NULL約束問題Null
- nvl、nvl2與nullif函式用法區別Null函式
- 運籌優化(十一)--無約束非線性規劃優化
- Django模型之欄位與約束Django模型
- Oracle定義約束 外來鍵約束Oracle
- SQL約束SQL
- Oracle約束Oracle
- oracle 約束Oracle
- MySQL 約束MySql
- 03約束
- 綜合約束
- 非功能性約束之效能(1)-效能銀彈:快取快取
- 運籌優化(十二)--帶約束非線性規劃(NLP)優化
- (10)邏輯綜合新增約束(環境約束)
- MySQL學習筆記——建立與約束MySql筆記
- Oracle約束的學習與測試Oracle
- [20121020]主外來鍵約束以及NULL問題.txtNull
- nvl, nvl2, nullifNull
- Unique約束,Primary Key約束與索引的關係學習與測試_20091213.doc索引
- 約束介紹
- 差分約束
- mysql 查詢欄位為null或者非nullMySqlNull
- [20120410]9i下索引與空值查詢以及非空約束.txt索引
- Oracle-空值null和數字相加的問題-nvl函式OracleNull函式
- 用decode和nvl處理null值時需要注意的地方Null
- C# 泛型 引用型別約束 值型別約束C#泛型型別
- not null與check is not nullNull