【唯一】DISTINCT與UNIQUE的“區別”
今天一個朋友在Oracle中偶然發現UNIQUE也可以得到唯一的資料結果,問到DISTINCT與UNIQUE的區別。
答案是:他們沒有區別!
在Oracle資料庫中他們的功能是完全一樣的。
簡單演示一下(雖然多餘,不過務實一下亦好)。
1.建立測試表T
sec@ora10g> create table t (x int);
Table created.
2.初始化幾條資料
sec@ora10g> insert into t values (1);
sec@ora10g> insert into t values (2);
sec@ora10g> insert into t values (2);
sec@ora10g> insert into t values (3);
sec@ora10g> insert into t values (3);
sec@ora10g> insert into t values (3);
sec@ora10g> commit;
3.T表全貌
sec@ora10g> select * from t;
X
----------
1
2
2
3
3
3
6 rows selected.
4.使用常用的DISTINCT得到唯一值方法
sec@ora10g> select distinct x from t;
X
----------
1
2
3
5.使用UNIQUE得到唯一值方法
sec@ora10g> select unique x from t;
X
----------
1
2
3
6.執行計劃也完全一樣
sec@ora10g> select distinct x from t;
Execution Plan
----------------------------------------------------------
Plan hash value: 1793979440
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6 | 78 | 4 (25)| 00:00:01 |
| 1 | HASH UNIQUE | | 6 | 78 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T | 6 | 78 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
sec@ora10g> select unique x from t;
Execution Plan
----------------------------------------------------------
Plan hash value: 1793979440
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6 | 78 | 4 (25)| 00:00:01 |
| 1 | HASH UNIQUE | | 6 | 78 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T | 6 | 78 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
7.結論及進一步論據
結論:DISTINCT與UNIQUE效果相同。
進一步論據:Oracle官方文件在有關SELECT語句的描述中對此有嚴謹的描述。
連結:
摘錄在此,供參考。
8.小結
遇到疑問時,隨時請教Oracle官方文件是一個非常好的習慣。
簡單構造實驗是消除疑慮的最好手段。
Good luck.
secooler
10.01.25
-- The End --
答案是:他們沒有區別!
在Oracle資料庫中他們的功能是完全一樣的。
簡單演示一下(雖然多餘,不過務實一下亦好)。
1.建立測試表T
sec@ora10g> create table t (x int);
Table created.
2.初始化幾條資料
sec@ora10g> insert into t values (1);
sec@ora10g> insert into t values (2);
sec@ora10g> insert into t values (2);
sec@ora10g> insert into t values (3);
sec@ora10g> insert into t values (3);
sec@ora10g> insert into t values (3);
sec@ora10g> commit;
3.T表全貌
sec@ora10g> select * from t;
X
----------
1
2
2
3
3
3
6 rows selected.
4.使用常用的DISTINCT得到唯一值方法
sec@ora10g> select distinct x from t;
X
----------
1
2
3
5.使用UNIQUE得到唯一值方法
sec@ora10g> select unique x from t;
X
----------
1
2
3
6.執行計劃也完全一樣
sec@ora10g> select distinct x from t;
Execution Plan
----------------------------------------------------------
Plan hash value: 1793979440
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6 | 78 | 4 (25)| 00:00:01 |
| 1 | HASH UNIQUE | | 6 | 78 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T | 6 | 78 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
sec@ora10g> select unique x from t;
Execution Plan
----------------------------------------------------------
Plan hash value: 1793979440
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6 | 78 | 4 (25)| 00:00:01 |
| 1 | HASH UNIQUE | | 6 | 78 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T | 6 | 78 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
7.結論及進一步論據
結論:DISTINCT與UNIQUE效果相同。
進一步論據:Oracle官方文件在有關SELECT語句的描述中對此有嚴謹的描述。
連結:
摘錄在此,供參考。
DISTINCT | UNIQUE
Specify DISTINCT or UNIQUE if you want the database to return only one copy of each set of duplicate rows selected. These two keywords are synonymous. Duplicate rows are those with matching values for each expression in the select list.
Restrictions on DISTINCT and UNIQUE Queries These types of queries are subject to the following restrictions:
-
When you specify DISTINCT or UNIQUE, the total number of bytes in all select list expressions is limited to the size of a data block minus some overhead. This size is specified by the initialization parameter DB_BLOCK_SIZE.
-
You cannot specify DISTINCT if the select_list contains LOB columns.
8.小結
遇到疑問時,隨時請教Oracle官方文件是一個非常好的習慣。
簡單構造實驗是消除疑慮的最好手段。
Good luck.
secooler
10.01.25
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-625986/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 主鍵(PK)與非空唯一索引(Unique Index)的區別索引Index
- unique index與primary key的區別Index
- oracle Distinct|Unique 異同Oracle
- DISTINCT和GROUP BY的區別
- Oracle主鍵、唯一鍵與唯一索引的區別Oracle索引
- oracle中distinct和group by的區別Oracle
- oracle Distinct|Unique 異同【Blog 搬家】Oracle
- pk 、unique index 和 index 區別Index
- 【INDEX】Oracle中主鍵、唯一約束與唯一索引之區別IndexOracle索引
- 主鍵和唯一索引的區別索引
- mysql索引型別Normal,Unique,Full Text區別以及索引方法Btree,Hash的區別MySql索引型別ORM
- db_name和db_unique_name的區別
- 唯一索引和非唯一索引ROWID儲存的區別索引
- 唯一性約束和唯一性索引的區別索引
- 主鍵、自增主鍵、主鍵索引、唯一索引概念區別與效能區別索引
- 唯一性索引(Unique Index)與普通索引(Normal Index)差異(上)索引IndexORM
- 唯一性索引(Unique Index)與普通索引(Normal Index)差異(中)索引IndexORM
- 唯一性索引(Unique Index)與普通索引(Normal Index)差異(下)索引IndexORM
- NULL和唯一約束UNIQUE的對應關係Null
- 索引唯一性掃描(INDEX UNIQUE SCAN)索引Index
- Oracle主鍵約束、唯一鍵約束、唯一索引的區別(轉)Oracle索引
- 唯一index和非唯一index中leaf node裡rowid的一點區別。Index
- ??與?:的區別
- 查詢中的distinct與group by
- 關於唯一性索引造成堵塞和非唯一性索引造成堵塞的區別索引
- MYSQL中的普通索引,主健,唯一,全文索引區別MySql索引
- MySQL的@與@@區別MySql
- mybatis #與$的區別MyBatis
- Null 與 “” 的區別Null
- &與&&, |與||區別
- in與exist , not in與not exist 的區別
- count_sum_distinct與nullNull
- 裝置唯一標識方法(Unique Identifier):如何在 Windows 系統上獲取裝置的唯一標識IDEWindows
- CentOS 與 Ubuntu 的區別CentOSUbuntu
- artice與section的區別
- GET 與 POST 的區別
- WebSocket 與 Socket 的區別Web
- Postgresql與MySQL的區別MySql