【唯一】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語句的描述中對此有嚴謹的描述。
連結:
摘錄在此,供參考。
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle中distinct和group by的區別Oracle
- mysql中key 、primary key 、unique key 與index區別MySqlIndex
- mysql索引型別Normal,Unique,Full Text區別以及索引方法Btree,Hash的區別MySql索引型別ORM
- ??與?:的區別
- thinkphp validate unique 資料庫欄位唯一性校驗PHP資料庫
- 裝置唯一標識方法(Unique Identifier):如何在 Windows 系統上獲取裝置的唯一標識IDEWindows
- mysql索引型別:FULLTEXT、NORMAL、SPATIAL、UNIQUEMySql索引型別ORM
- const與static的區別
- HTTP 與 HTTPS 的區別HTTP
- getAttribute() 與 attr() 的區別
- @import與<link> 的區別Import
- Postgresql與MySQL的區別MySql
- HashSet與HashMap的區別HashMap
- HashTable與ConcurrentHashMap的區別HashMap
- maven與ant的區別Maven
- __new()__ 與 __init()__的區別
- @Autowired 與@Resource的區別
- gulp與webpack的區別Web
- free 與 CFRelease 的區別
- post與get的區別
- Git與GitHub的區別Github
- Comparable與Comparator的區別
- volatile與synchronized的區別synchronized
- Javascript中“==”與“===”的區別JavaScript
- ICMP與IGMP的區別
- UDP與TCP的區別UDPTCP
- WebApp與NativeApp的區別WebAPP
- mysql與Oracle的區別MySqlOracle
- Synchronized 與 ReentrantLock 的區別synchronizedReentrantLock
- let與var的區別
- mybatis與hibernate的區別MyBatis
- buffer與cache的區別
- grid 與 treelist 的區別
- print 與 println 的區別
- Eureka與Zookeeper的區別
- Python中 ‘==‘ 與‘is‘的區別Python
- async與defer的區別
- http與https的區別HTTP
- GET 與 POST 的區別