- Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathexample.sql
153 lines (128 loc) · 4.59 KB
/
example.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-- ===================================================================================
-- Complete example of a partition exchange load using incremental stats
-- and a subpartitioned main table.
-- ===================================================================================
set linesize 2000
set trims on
set pagesize 50
set echo on
drop table range_list_main_tab;
drop table list_part_load_tab;
--
-- interval range-list table
--
create table range_list_main_tab
(num number,
ten number)
partition by range (num)
interval (1000)
subpartition by list (ten)
subpartition template
(subpartition t_spart1 values (0,2,4,6,8),
subpartition t_spart2 values (1,3,5,7,9))
(partition range_list_main_part1 values less than (1000),
partition range_list_main_part2 values less than (2000));
create index range_list_main_tab_n on range_list_main_tab(num) local;
--
-- list partitioned table
--
create table list_part_load_tab
(num number,
ten number)
partition by list (ten)
(partition list_part_load_part1 values (0,2,4,6,8),
partition list_part_load_part2 values (1,3,5,7,9));
exec dbms_stats.set_table_prefs(null, 'range_list_main_tab', 'incremental', 'true');
exec dbms_stats.set_table_prefs(null, 'list_part_load_tab', 'incremental', 'true');
exec dbms_stats.set_table_prefs(null, 'range_list_main_tab', 'incremental_level', 'partition');
exec dbms_stats.set_table_prefs(null, 'list_part_load_tab', 'incremental_level', 'table');
--
-- The main table will have 998 rows
--
insert into range_list_main_tab
select rownum,mod(rownum,10)
from dual
connect by level<500
union all
select rownum+1000,mod(rownum,10)
from dual
connect by level<500;
--
-- The load table will have 999 rows
--
insert into list_part_load_tab
select rownum,mod(rownum,10)
from dual
connect by level<1000;
exec dbms_stats.gather_table_stats(null, 'range_list_main_tab');
--
-- Let's sleep here to give the main table and load table
-- different last_analyzed times
--
host sleep 5
exec dbms_stats.gather_table_stats(null, 'list_part_load_tab');
--
-- Should be 1000 rows
--
select count(*) from range_list_main_tab;
select to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') table_ana
from user_tables
where table_name = upper('range_list_main_tab');
select partition_name, to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') part_ana
from user_tab_partitions
where table_name = upper('range_list_main_tab')
order by partition_position;
select subpartition_name, to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') subpart_ana
from user_tab_subpartitions
where table_name = upper('range_list_main_tab')
order by subpartition_name;
select to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') load_table_ana
from user_tables
where table_name = upper('list_part_load_tab');
select partition_name, to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') load_part_ana
from user_tab_partitions
where table_name = upper('list_part_load_tab')
order by partition_position;
--
-- Perform the exchange after a delay
--
host sleep 5
alter table range_list_main_tab
exchange partition range_list_main_part1
with table list_part_load_tab;
--
-- Exchange complete at:
--
select to_char(sysdate,'dd-mon-yyyy hh24:mi:ss') exchange_complete
from dual;
exec dbms_stats.gather_table_stats(null, 'range_list_main_tab');
--
-- Should now be 1498 rows
--
select count(*) from range_list_main_tab;
--
-- The time shown here will be the most recent because the global
-- statistics must be updated after the partition has been exchanged.
-- So, expect the time to be similar to the completion exchange time.
--
select to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') table_ana
from user_tables
where table_name = upper('range_list_main_tab');
--
-- Part 1 statistics were gathered earlier, because they came from the load
-- table. They did not have to be regathered after the partition was echanged.
-- Part 2 statistics have not been regathered - there is no need.
--
select partition_name, to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') part_ana
from user_tab_partitions
where table_name = upper('range_list_main_tab')
order by partition_position;
--
-- The Part 1 subpartition stats came from the load table so they have not
-- been regathered after the exchange.
-- Part 2 subpartition stats have not been regathered - there is no need.
--
select subpartition_name, to_char(last_analyzed,'dd-mon-yyyy hh24:mi:ss') subpart_ana
from user_tab_subpartitions
where table_name = upper('range_list_main_tab')
order by subpartition_name;