CREATE TABLE sales
( prod_id NUMBER
, cust_id NUMBER
, time_id DATE
)
PARTITION BY RANGE (time_id)
( PARTITION sales_2017_12 VALUES LESS THAN (TO_DATE('01-JAN-2018','dd-MON-yyyy'))
TABLESPACE users
, PARTITION sales_2018_01 VALUES LESS THAN (TO_DATE('01-FEB-2018','dd-MON-yyyy'))
TABLESPACE users
, PARTITION sales_2018_02 VALUES LESS THAN (TO_DATE('01-MAR-2018','dd-MON-yyyy'))
TABLESPACE users
, PARTITION sales_2018_03 VALUES LESS THAN (TO_DATE('01-APR-2018','dd-MON-yyyy'))
TABLESPACE users
, PARTITION sales_2018_04 VALUES LESS THAN (TO_DATE('01-MAY-2018','dd-MON-yyyy'))
TABLESPACE users
);
create sequence sales_seq;
insert into sales values(sales_seq.nextval,1121,to_date('20180101','yyyymmdd'));
insert into sales values(sales_seq.nextval,1122,to_date('20180101','yyyymmdd'));
insert into sales values(sales_seq.nextval,1120,to_date('20180101','yyyymmdd'));
insert into sales values(sales_seq.nextval,1111,to_date('20180101','yyyymmdd'));
insert into sales values(sales_seq.nextval,1123,to_date('20180201','yyyymmdd'));
insert into sales values(sales_seq.nextval,1123,to_date('20180201','yyyymmdd'));
insert into sales values(sales_seq.nextval,1234,to_date('20180201','yyyymmdd'));
insert into sales values(sales_seq.nextval,1643,to_date('20180201','yyyymmdd'));
insert into sales values(sales_seq.nextval,1111,to_date('20180301','yyyymmdd'));
insert into sales values(sales_seq.nextval,2222,to_date('20180301','yyyymmdd'));
insert into sales values(sales_seq.nextval,1321,to_date('20180301','yyyymmdd'));
insert into sales values(sales_seq.nextval,1955,to_date('20180301','yyyymmdd'));
insert into sales values(sales_seq.nextval,1754,to_date('20180401','yyyymmdd'));
insert into sales values(sales_seq.nextval,1027,to_date('20180401','yyyymmdd'));
insert into sales values(sales_seq.nextval,1013,to_date('20180401','yyyymmdd'));
insert into sales values(sales_seq.nextval,1100,to_date('20180401','yyyymmdd'));
commit;
create unique index sales_pk on sales(prod_id) local;
--유니크 인덱스는 파티션 키를 포함해야 local로 만들어진다.
*
--ERROR at line 1:
--ORA-14039: partitioning columns must form a subset of key columns of a UNIQUE index
create unique index sales_pk on sales(prod_id,time_id) local;
create index sales_custid on sales(cust_id) local;
exec dbms_stats.gather_table_stats(user,'sales');
alter session set statistics_level=all;
alter session set nls_date_format='yyyymmdd hh24:mi:ss';
select /*+gather_plan_statistics*/ * from sales;
PROD_ID CUST_ID TIME_ID
---------- ---------- -----------------
21 1121 20180101 00:00:00
22 1122 20180101 00:00:00
23 1120 20180101 00:00:00
24 1111 20180101 00:00:00
25 1123 20180201 00:00:00
26 1123 20180201 00:00:00
27 1234 20180201 00:00:00
28 1643 20180201 00:00:00
29 1111 20180301 00:00:00
30 2222 20180301 00:00:00
31 1321 20180301 00:00:00
32 1955 20180301 00:00:00
33 1754 20180401 00:00:00
34 1027 20180401 00:00:00
35 1013 20180401 00:00:00
36 1100 20180401 00:00:00