今天做Schema评审的时候发现一个很奇怪的现象,也许是用工具生成的SQL语句,清一色的如下:
CREATE TABLE table_name (
id NUMBER NOT NULL,
......
......
) ;
CREATE INDEX table_name_PK ON table_name(ID) ;
ALTER TABLE table_name
ADD CONSTRAINT table_name_PK PRIMARY KEY (ID)
USING INDEX table_name_PK ;
通常来说主键(Primary Key,PK)的index是unique index,而现在变成了non-unique index,这有什么不同呢?
于是我建了两张1000万数据的表,并用两种不同的index设定为PK的index,语句如下:
create table tab1000w01
as
select level id,'killkill Hello world' data
from dual connect by level<=1000*10000;
create table tab1000w02
as
select level id,'killkill Hello world' data
from dual connect by level<=1000*10000;
CREATE UNIQUE INDEX tab1000w01_pk ON tab1000w01 (PK_ID) ;
ALTER TABLE tab1000w01 ADD CONSTRAINT tab1000w01_PK PRIMARY KEY (PK_ID) USING INDEX tab1000w01_pk ;
CREATE INDEX tab1000w02_pk ON tab500w02 (PK_ID) ;
ALTER TABLE tab1000w02 ADD CONSTRAINT tab1000w02_PK PRIMARY KEY (PK_ID) USING INDEX tab1000w02_pk ;
以下是按照PK查找数据的语句:
select * from tab1000w01 where id=34567;
----------------------------------------------------------------------------