云数据库 GAUSSDB-CREATE TABLE SUBPARTITION:示例
时间:2025-03-14 09:57:02
示例
- 示例1:创建各种组合类型的二级分区表
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
CREATE TABLE list_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY LIST (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 VALUES ( '201902' ) ( SUBPARTITION p_201901_a VALUES ( '1' ), SUBPARTITION p_201901_b VALUES ( '2' ) ), PARTITION p_201902 VALUES ( '201903' ) ( SUBPARTITION p_201902_a VALUES ( '1' ), SUBPARTITION p_201902_b VALUES ( '2' ) ) ); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201902', '2', '1', 1); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); insert into list_list values('201903', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); select * from list_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (6 rows) drop table list_list; CREATE TABLE list_hash ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY LIST (month_code) SUBPARTITION BY HASH (dept_code) ( PARTITION p_201901 VALUES ( '201902' ) ( SUBPARTITION p_201901_a, SUBPARTITION p_201901_b ), PARTITION p_201902 VALUES ( '201903' ) ( SUBPARTITION p_201902_a, SUBPARTITION p_201902_b ) ); insert into list_hash values('201902', '1', '1', 1); insert into list_hash values('201902', '2', '1', 1); insert into list_hash values('201902', '3', '1', 1); insert into list_hash values('201903', '4', '1', 1); insert into list_hash values('201903', '5', '1', 1); insert into list_hash values('201903', '6', '1', 1); select * from list_hash; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 4 | 1 | 1 201903 | 5 | 1 | 1 201903 | 6 | 1 | 1 201902 | 2 | 1 | 1 201902 | 3 | 1 | 1 201902 | 1 | 1 | 1 (6 rows) drop table list_hash; CREATE TABLE list_range ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY LIST (month_code) SUBPARTITION BY RANGE (dept_code) ( PARTITION p_201901 VALUES ( '201902' ) ( SUBPARTITION p_201901_a values less than ('4'), SUBPARTITION p_201901_b values less than ('6') ), PARTITION p_201902 VALUES ( '201903' ) ( SUBPARTITION p_201902_a values less than ('3'), SUBPARTITION p_201902_b values less than ('6') ) ); insert into list_range values('201902', '1', '1', 1); insert into list_range values('201902', '2', '1', 1); insert into list_range values('201902', '3', '1', 1); insert into list_range values('201903', '4', '1', 1); insert into list_range values('201903', '5', '1', 1); insert into list_range values('201903', '6', '1', 1); ERROR: inserted partition key does not map to any table partition select * from list_range; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 4 | 1 | 1 201903 | 5 | 1 | 1 201902 | 1 | 1 | 1 201902 | 2 | 1 | 1 201902 | 3 | 1 | 1 (5 rows) drop table list_range; CREATE TABLE range_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY RANGE (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 VALUES LESS THAN( '201903' ) ( SUBPARTITION p_201901_a values ('1'), SUBPARTITION p_201901_b values ('2') ), PARTITION p_201902 VALUES LESS THAN( '201904' ) ( SUBPARTITION p_201902_a values ('1'), SUBPARTITION p_201902_b values ('2') ) ); insert into range_list values('201902', '1', '1', 1); insert into range_list values('201902', '2', '1', 1); insert into range_list values('201902', '1', '1', 1); insert into range_list values('201903', '2', '1', 1); insert into range_list values('201903', '1', '1', 1); insert into range_list values('201903', '2', '1', 1); select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 (6 rows) drop table range_list; CREATE TABLE range_hash ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY RANGE (month_code) SUBPARTITION BY HASH (dept_code) ( PARTITION p_201901 VALUES LESS THAN( '201903' ) ( SUBPARTITION p_201901_a, SUBPARTITION p_201901_b ), PARTITION p_201902 VALUES LESS THAN( '201904' ) ( SUBPARTITION p_201902_a, SUBPARTITION p_201902_b ) ); insert into range_hash values('201902', '1', '1', 1); insert into range_hash values('201902', '2', '1', 1); insert into range_hash values('201902', '1', '1', 1); insert into range_hash values('201903', '2', '1', 1); insert into range_hash values('201903', '1', '1', 1); insert into range_hash values('201903', '2', '1', 1); select * from range_hash; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 (6 rows) drop table range_hash; CREATE TABLE range_range ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY RANGE (month_code) SUBPARTITION BY RANGE (dept_code) ( PARTITION p_201901 VALUES LESS THAN( '201903' ) ( SUBPARTITION p_201901_a VALUES LESS THAN( '2' ), SUBPARTITION p_201901_b VALUES LESS THAN( '3' ) ), PARTITION p_201902 VALUES LESS THAN( '201904' ) ( SUBPARTITION p_201902_a VALUES LESS THAN( '2' ), SUBPARTITION p_201902_b VALUES LESS THAN( '3' ) ) ); insert into range_range values('201902', '1', '1', 1); insert into range_range values('201902', '2', '1', 1); insert into range_range values('201902', '1', '1', 1); insert into range_range values('201903', '2', '1', 1); insert into range_range values('201903', '1', '1', 1); insert into range_range values('201903', '2', '1', 1); select * from range_range; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 2 | 1 | 1 201903 | 1 | 1 | 1 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 (6 rows) drop table range_range; CREATE TABLE hash_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY hash (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 ( SUBPARTITION p_201901_a VALUES ( '1' ), SUBPARTITION p_201901_b VALUES ( '2' ) ), PARTITION p_201902 ( SUBPARTITION p_201902_a VALUES ( '1' ), SUBPARTITION p_201902_b VALUES ( '2' ) ) ); insert into hash_list values('201901', '1', '1', 1); insert into hash_list values('201901', '2', '1', 1); insert into hash_list values('201901', '1', '1', 1); insert into hash_list values('201903', '2', '1', 1); insert into hash_list values('201903', '1', '1', 1); insert into hash_list values('201903', '2', '1', 1); select * from hash_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 201901 | 2 | 1 | 1 201901 | 1 | 1 | 1 201901 | 1 | 1 | 1 (6 rows) drop table hash_list; CREATE TABLE hash_hash ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY hash (month_code) SUBPARTITION BY hash (dept_code) ( PARTITION p_201901 ( SUBPARTITION p_201901_a, SUBPARTITION p_201901_b ), PARTITION p_201902 ( SUBPARTITION p_201902_a, SUBPARTITION p_201902_b ) ); insert into hash_hash values('201901', '1', '1', 1); insert into hash_hash values('201901', '2', '1', 1); insert into hash_hash values('201901', '1', '1', 1); insert into hash_hash values('201903', '2', '1', 1); insert into hash_hash values('201903', '1', '1', 1); insert into hash_hash values('201903', '2', '1', 1); select * from hash_hash; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 201901 | 2 | 1 | 1 201901 | 1 | 1 | 1 201901 | 1 | 1 | 1 (6 rows) drop table hash_hash; CREATE TABLE hash_range ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY hash (month_code) SUBPARTITION BY range (dept_code) ( PARTITION p_201901 ( SUBPARTITION p_201901_a VALUES LESS THAN ( '2' ), SUBPARTITION p_201901_b VALUES LESS THAN ( '3' ) ), PARTITION p_201902 ( SUBPARTITION p_201902_a VALUES LESS THAN ( '2' ), SUBPARTITION p_201902_b VALUES LESS THAN ( '3' ) ) ); insert into hash_range values('201901', '1', '1', 1); insert into hash_range values('201901', '2', '1', 1); insert into hash_range values('201901', '1', '1', 1); insert into hash_range values('201903', '2', '1', 1); insert into hash_range values('201903', '1', '1', 1); insert into hash_range values('201903', '2', '1', 1); select * from hash_range; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 1 | 1 | 1 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201901 | 1 | 1 | 1 201901 | 1 | 1 | 1 201901 | 2 | 1 | 1 (6 rows)
- 示例2:对二级分区表进行DML指定分区操作
CREATE TABLE range_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY RANGE (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 VALUES LESS THAN( '201903' ) ( SUBPARTITION p_201901_a values ('1'), SUBPARTITION p_201901_b values ('2') ), PARTITION p_201902 VALUES LESS THAN( '201910' ) ( SUBPARTITION p_201902_a values ('1'), SUBPARTITION p_201902_b values ('2') ) ); --指定一级分区插入数据 insert into range_list partition (p_201901) values('201902', '1', '1', 1); --实际分区和指定分区不一致,报错 insert into range_list partition (p_201902) values('201902', '1', '1', 1); ERROR: inserted partition key does not map to the table partition DETAIL: N/A. --指定二级分区插入数据 insert into range_list subpartition (p_201901_a) values('201902', '1', '1', 1); --实际分区和指定分区不一致,报错 insert into range_list subpartition (p_201901_b) values('201902', '1', '1', 1); ERROR: inserted subpartition key does not map to the table subpartition DETAIL: N/A. insert into range_list partition for ('201902') values('201902', '1', '1', 1); insert into range_list subpartition for ('201902','1') values('201902', '1', '1', 1); --指定分区查询数据 select * from range_list partition (p_201901); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) select * from range_list subpartition (p_201901_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) select * from range_list partition for ('201902'); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) select * from range_list subpartition for ('201902','1'); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) --指定分区更新数据 update range_list partition (p_201901) set user_no = '2'; select * from range_list; select *from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 2 | 1 201902 | 1 | 2 | 1 201902 | 1 | 2 | 1 201902 | 1 | 2 | 1 (4 rows) update range_list subpartition (p_201901_a) set user_no = '3'; select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 3 | 1 201902 | 1 | 3 | 1 201902 | 1 | 3 | 1 201902 | 1 | 3 | 1 (4 rows) update range_list partition for ('201902') set user_no = '4'; select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 4 | 1 201902 | 1 | 4 | 1 201902 | 1 | 4 | 1 201902 | 1 | 4 | 1 (4 rows) update range_list subpartition for ('201902','2') set user_no = '5'; gaussdb=# select *from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 4 | 1 201902 | 1 | 4 | 1 201902 | 1 | 4 | 1 201902 | 1 | 4 | 1 (4 rows) select * from range_list; --指定分区删除数据 delete from range_list partition (p_201901); DELETE 4 delete from range_list partition for ('201903'); DELETE 0 delete from range_list subpartition (p_201901_a); DELETE 0 delete from range_list subpartition for ('201903','2'); DELETE 0 --参数sql_compatibility='B'时,可指定多分区删除数据 delete from range_list as t partition (p_201901_a, p_201901); DELETE 0 --指定分区insert数据 insert into range_list partition (p_201901) values('201902', '1', '1', 1) ON DUPLICATE KEY UPDATE sales_amt = 5; insert into range_list subpartition (p_201901_a) values('201902', '1', '1', 1) ON DUPLICATE KEY UPDATE sales_amt = 10; insert into range_list partition for ('201902') values('201902', '1', '1', 1) ON DUPLICATE KEY UPDATE sales_amt = 30; insert into range_list subpartition for ('201902','1') values('201902', '1', '1', 1) ON DUPLICATE KEY UPDATE sales_amt = 40; select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) --指定分区merge into数据 CREATE TABLE newrange_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY RANGE (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 VALUES LESS THAN( '201903' ) ( SUBPARTITION p_201901_a values ('1'), SUBPARTITION p_201901_b values ('2') ), PARTITION p_201902 VALUES LESS THAN( '201910' ) ( SUBPARTITION p_201902_a values ('1'), SUBPARTITION p_201902_b values ('2') ) ); insert into newrange_list values('201902', '1', '1', 1); insert into newrange_list values('201903', '1', '1', 2); MERGE INTO range_list partition (p_201901) p USING newrange_list partition (p_201901) np ON p.month_code= np.month_code WHEN MATCHED THEN UPDATE SET dept_code = np.dept_code, user_no = np.user_no, sales_amt = np.sales_amt WHEN NOT MATCHED THEN INSERT VALUES (np.month_code, np.dept_code, np.user_no, np.sales_amt); select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) MERGE INTO range_list partition for ('201901') p USING newrange_list partition for ('201901') np ON p.month_code= np.month_code WHEN MATCHED THEN UPDATE SET dept_code = np.dept_code, user_no = np.user_no, sales_amt = np.sales_amt WHEN NOT MATCHED THEN INSERT VALUES (np.month_code, np.dept_code, np.user_no, np.sales_amt); select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) MERGE INTO range_list subpartition (p_201901_a) p USING newrange_list subpartition (p_201901_a) np ON p.month_code= np.month_code WHEN MATCHED THEN UPDATE SET dept_code = np.dept_code, user_no = np.user_no, sales_amt = np.sales_amt WHEN NOT MATCHED THEN INSERT VALUES (np.month_code, np.dept_code, np.user_no, np.sales_amt); select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows) MERGE INTO range_list subpartition for ('201901', '1') p USING newrange_list subpartition for ('201901', '1') np ON p.month_code= np.month_code WHEN MATCHED THEN UPDATE SET dept_code = np.dept_code, user_no = np.user_no, sales_amt = np.sales_amt WHEN NOT MATCHED THEN INSERT VALUES (np.month_code, np.dept_code, np.user_no, np.sales_amt); select * from range_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (4 rows)
- 示例3对二级分区表进行truncate操作
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
CREATE TABLE list_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY LIST (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 VALUES ( '201902' ) ( SUBPARTITION p_201901_a VALUES ( '1' ), SUBPARTITION p_201901_b VALUES ( default ) ), PARTITION p_201902 VALUES ( '201903' ) ( SUBPARTITION p_201902_a VALUES ( '1' ), SUBPARTITION p_201902_b VALUES ( '2' ) ) ); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201902', '2', '1', 1); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); insert into list_list values('201903', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); select * from list_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (6 rows) select * from list_list partition (p_201901); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (3 rows) alter table list_list truncate partition p_201901; select * from list_list partition (p_201901); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list partition (p_201902); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 (3 rows) alter table list_list truncate partition p_201902; select * from list_list partition (p_201902); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) insert into list_list values('201902', '1', '1', 1); insert into list_list values('201902', '2', '1', 1); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); insert into list_list values('201903', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); select * from list_list subpartition (p_201901_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (2 rows) alter table list_list truncate subpartition p_201901_a; select * from list_list subpartition (p_201901_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list subpartition (p_201901_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 (1 row) alter table list_list truncate subpartition p_201901_b; select * from list_list subpartition (p_201901_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list subpartition (p_201902_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 1 | 1 | 1 (1 row) alter table list_list truncate subpartition p_201902_a; select * from list_list subpartition (p_201902_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list subpartition (p_201902_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 (2 rows) alter table list_list truncate subpartition p_201902_b; select * from list_list subpartition (p_201902_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) drop table list_list;
- 示例4:对二级分区表进行split操作
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
CREATE TABLE list_list ( month_code VARCHAR2 ( 30 ) NOT NULL , dept_code VARCHAR2 ( 30 ) NOT NULL , user_no VARCHAR2 ( 30 ) NOT NULL , sales_amt int ) PARTITION BY LIST (month_code) SUBPARTITION BY LIST (dept_code) ( PARTITION p_201901 VALUES ( '201902' ) ( SUBPARTITION p_201901_a VALUES ( '1' ), SUBPARTITION p_201901_b VALUES ( default ) ), PARTITION p_201902 VALUES ( '201903' ) ( SUBPARTITION p_201902_a VALUES ( '1' ), SUBPARTITION p_201902_b VALUES ( default ) ) ); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201902', '2', '1', 1); insert into list_list values('201902', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); insert into list_list values('201903', '1', '1', 1); insert into list_list values('201903', '2', '1', 1); select * from list_list; month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 201903 | 1 | 1 | 1 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (6 rows) select * from list_list subpartition (p_201901_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (2 rows) select * from list_list subpartition (p_201901_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 (1 row) alter table list_list split subpartition p_201901_b values (2) into ( subpartition p_201901_b, subpartition p_201901_c ); select * from list_list subpartition (p_201901_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (2 rows) select * from list_list subpartition (p_201901_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 (1 row) select * from list_list subpartition (p_201901_c); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list partition (p_201901); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201902 | 2 | 1 | 1 201902 | 1 | 1 | 1 201902 | 1 | 1 | 1 (3 rows) select * from list_list subpartition (p_201902_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 1 | 1 | 1 (1 row) select * from list_list subpartition (p_201902_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 (2 rows) alter table list_list split subpartition p_201902_b values (3) into ( subpartition p_201902_b, subpartition p_201902_c ); select * from list_list subpartition (p_201902_a); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 1 | 1 | 1 (1 row) select * from list_list subpartition (p_201902_b); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- (0 rows) select * from list_list subpartition (p_201902_c); month_code | dept_code | user_no | sales_amt ------------+-----------+---------+----------- 201903 | 2 | 1 | 1 201903 | 2 | 1 | 1 (2 rows) drop table list_list;
support.huaweicloud.com/centralized-devg-v3-gaussdb/gaussdb-42-0561.html