postgresql分区表2

前端之家收集整理的这篇文章主要介绍了postgresql分区表2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 1). 创建"主表",所有分区都从它继承。
  2. create table ft
  3. (
  4. sid int not null,sdate date not null,saddress varchar(100)
  5. )tablespace tb01;
  6.  
  7. 2). 创建几个"子"表,每个都从主表上继承
  8.  
  9. create table ft01
  10. (check ( sdate >= date '2015-01-01' and sdate < date '2015-02-01') )
  11. inherits (ft)
  12. tablespace tb01;
  13.  
  14.  
  15. create table ft02
  16. ( check ( sdate >= date '2015-02-01' and sdate < date '2015-03-01'))
  17. inherits (ft)
  18. tablespace tb01;
  19.  
  20.  
  21. create table ft03
  22. ( check ( sdate >= date '2015-03-01' and sdate < date '2015-04-01'))
  23. inherits (ft)
  24. tablespace tb01;
  25.  
  26. create table ft04
  27. ( check ( sdate >= date '2015-04-01' and sdate < date '2015-05-01'))
  28. inherits (ft)
  29. tablespace tb01;
  30.  
  31.  
  32. create table ft05
  33. ( check ( sdate >= date '2015-05-01' and sdate < date '2015-06-01'))
  34. inherits (ft)
  35. tablespace tb01;
  36.  
  37. create table ft06
  38. ( check ( sdate >= date '2015-06-01' and sdate < date '2015-07-01'))
  39. inherits (ft)
  40. tablespace tb01;
  41.  
  42.  
  43. create table ft07
  44. ( check ( sdate >= date '2015-07-01' and sdate < date '2015-08-01'))
  45. inherits (ft)
  46. tablespace tb01;
  47.  
  48. create table ft08
  49. ( check ( sdate >= date '2015-08-01' and sdate < date '2015-09-01'))
  50. inherits (ft)
  51. tablespace tb01;
  52.  
  53. create table ft09
  54. ( check ( sdate >= date '2015-09-01' and sdate < date '2015-10-01'))
  55. inherits (ft)
  56. tablespace tb01;
  57.  
  58.  
  59. create table ft10
  60. ( check ( sdate >= date '2015-10-01' and sdate < date '2015-11-01'))
  61. inherits (ft)
  62. tablespace tb01;
  63.  
  64.  
  65. create table ft11
  66. ( check ( sdate >= date '2015-11-01' and sdate < date '2015-12-01'))
  67. inherits (ft)
  68. tablespace tb01;
  69.  
  70.  
  71. create table ft12
  72. ( check ( sdate >= date '2015-12-01' and sdate < date '2016-1-01'))
  73. inherits (ft)
  74. tablespace tb01;
  75.  
  76.  
  77. create index index_ft01_sdate on ft01 (sdate) tablespace tb01_index;
  78. create index index_ft02_sdate on ft02 (sdate) tablespace tb01_index;
  79. create index index_ft03_sdate on ft03 (sdate) tablespace tb01_index ;
  80. create index index_ft04_sdate on ft04 (sdate) tablespace tb01_index;
  81. create index index_ft05_sdate on ft05 (sdate) tablespace tb01_index;
  82. create index index_ft06_sdate on ft06 (sdate) tablespace tb01_index;
  83. create index index_ft07_sdate on ft07 (sdate) tablespace tb01_index;
  84. create index index_ft08_sdate on ft08 (sdate) tablespace tb01_index;
  85. create index index_ft09_sdate on ft09 (sdate) tablespace tb01_index;
  86. create index index_ft10_sdate on ft10 (sdate) tablespace tb01_index;
  87. create index index_ft11_sdate on ft11 (sdate) tablespace tb01_index;
  88. create index index_ft12_sdate on ft12 (sdate) tablespace tb01_index;
  89.  
  90. create or replace function ft_insert_trigger()
  91. returns trigger as $$
  92. begin
  93. if( new.sdate between '2015-1-01' and '2015-02-01' ) then
  94. insert into ft01 values (new.*);
  95. elsif( new.sdate between '2015-2-01' and '2015-03-01' ) then
  96. insert into ft02 values (new.*);
  97. elsif( new.sdate between '2015-3-01' and '2015-04-01' ) then
  98. insert into ft03 values (new.*);
  99. elsif( new.sdate between '2015-4-01' and '2015-05-01' ) then
  100. insert into ft04 values (new.*);
  101. elsif( new.sdate between '2015-05-01' and '2015-06-01' ) then
  102. insert into ft05 values (new.*);
  103. elsif( new.sdate between '2015-06-01' and '2015-07-01' ) then
  104. insert into ft06 values (new.*);
  105. elsif( new.sdate between '2015-07-01' and '2015-08-01' ) then
  106. insert into ft07 values (new.*);
  107. elsif( new.sdate between '2015-08-01' and '2015-09-01' ) then
  108. insert into ft08 values (new.*);
  109. elsif( new.sdate between '2015-09-01' and '2015-10-01' ) then
  110. insert into ft09 values (new.*);
  111. elsif( new.sdate between '2015-10-01' and '2015-11-01' ) then
  112. insert into ft10 values (new.*);
  113. elsif( new.sdate between '2015-11-01' and '2015-12-01' ) then
  114. insert into ft11 values (new.*);
  115. elsif( new.sdate between '2015-12-01' and '2016-01-01' ) then
  116. insert into ft12 values (new.*);
  117. else
  118. raise exception '!out of range!';
  119. end if;
  120. return null;
  121. end;
  122. $$
  123. language plpgsql;
  124.  
  125.  
  126. create trigger insert_ft_trigger
  127. before insert on ft
  128. for each row execute procedure ft_insert_trigger() ;
  129.  
  130.  
  131. ----插入测试数据
  132.  
  133. insert into ft(sid,sdate,saddress) select n,'2015-01-03',n||':杨楂文'
  134. from generate_series(1,100) n;
  135.  
  136. insert into ft(sid,'2015-02-04','2015-03-05','2015-04-04','2015-05-05','2015-06-06','2015-07-07','2015-08-08','2015-09-09','2015-10-10','2015-11-11','2015-12-12',100) n;
  137.  
  138.  
  139. ----查询
  140.  
  141. SET constraint_exclusion = off;
  142. #EXPLAIN SELECT count(*) FROM ft WHERE sdate >= DATE '2015-01-01';
  143. QUERY PLAN
  144. ------------------------------------------------------------------
  145. Aggregate (cost=30.02..30.03 rows=1 width=0)
  146. -> Append (cost=0.00..27.01 rows=1202 width=0)
  147. -> Seq Scan on ft (cost=0.00..0.00 rows=1 width=0)
  148. Filter: (sdate >= '2015-01-01'::date)
  149. -> Seq Scan on ft01 (cost=0.00..2.26 rows=101 width=0)
  150. Filter: (sdate >= '2015-01-01'::date)
  151. -> Seq Scan on ft02 (cost=0.00..2.25 rows=100 width=0)
  152. Filter: (sdate >= '2015-01-01'::date)
  153. -> Seq Scan on ft03 (cost=0.00..2.25 rows=100 width=0)
  154. Filter: (sdate >= '2015-01-01'::date)
  155. -> Seq Scan on ft04 (cost=0.00..2.25 rows=100 width=0)
  156. Filter: (sdate >= '2015-01-01'::date)
  157. -> Seq Scan on ft05 (cost=0.00..2.25 rows=100 width=0)
  158. Filter: (sdate >= '2015-01-01'::date)
  159. -> Seq Scan on ft06 (cost=0.00..2.25 rows=100 width=0)
  160. Filter: (sdate >= '2015-01-01'::date)
  161. -> Seq Scan on ft07 (cost=0.00..2.25 rows=100 width=0)
  162. Filter: (sdate >= '2015-01-01'::date)
  163. -> Seq Scan on ft08 (cost=0.00..2.25 rows=100 width=0)
  164. Filter: (sdate >= '2015-01-01'::date)
  165. -> Seq Scan on ft09 (cost=0.00..2.25 rows=100 width=0)
  166. Filter: (sdate >= '2015-01-01'::date)
  167. -> Seq Scan on ft10 (cost=0.00..2.25 rows=100 width=0)
  168. Filter: (sdate >= '2015-01-01'::date)
  169. -> Seq Scan on ft11 (cost=0.00..2.25 rows=100 width=0)
  170. Filter: (sdate >= '2015-01-01'::date)
  171. -> Seq Scan on ft12 (cost=0.00..2.25 rows=100 width=0)
  172. Filter: (sdate >= '2015-01-01'::date)
  173. (28 rows)
  174.  
  175.  
  176.  
  177. SET constraint_exclusion = off;
  178. #EXPLAIN SELECT count(*) FROM ft WHERE sdate >= DATE '2015-01-01';
  179. ------------------------------------------------------------------
  180. Aggregate (cost=30.02..30.03 rows=1 width=0)
  181. -> Append (cost=0.00..27.01 rows=1202 width=0)
  182. -> Seq Scan on ft (cost=0.00..0.00 rows=1 width=0)
  183. Filter: (sdate >= '2015-01-01'::date)
  184. -> Seq Scan on ft01 (cost=0.00..2.26 rows=101 width=0)
  185. Filter: (sdate >= '2015-01-01'::date)
  186. -> Seq Scan on ft02 (cost=0.00..2.25 rows=100 width=0)
  187. Filter: (sdate >= '2015-01-01'::date)
  188. -> Seq Scan on ft03 (cost=0.00..2.25 rows=100 width=0)
  189. Filter: (sdate >= '2015-01-01'::date)
  190. -> Seq Scan on ft04 (cost=0.00..2.25 rows=100 width=0)
  191. Filter: (sdate >= '2015-01-01'::date)
  192. -> Seq Scan on ft05 (cost=0.00..2.25 rows=100 width=0)
  193. Filter: (sdate >= '2015-01-01'::date)
  194. -> Seq Scan on ft06 (cost=0.00..2.25 rows=100 width=0)
  195. Filter: (sdate >= '2015-01-01'::date)
  196. -> Seq Scan on ft07 (cost=0.00..2.25 rows=100 width=0)
  197. Filter: (sdate >= '2015-01-01'::date)
  198. -> Seq Scan on ft08 (cost=0.00..2.25 rows=100 width=0)
  199. Filter: (sdate >= '2015-01-01'::date)
  200. -> Seq Scan on ft09 (cost=0.00..2.25 rows=100 width=0)
  201. Filter: (sdate >= '2015-01-01'::date)
  202. -> Seq Scan on ft10 (cost=0.00..2.25 rows=100 width=0)
  203. Filter: (sdate >= '2015-01-01'::date)
  204. -> Seq Scan on ft11 (cost=0.00..2.25 rows=100 width=0)
  205. Filter: (sdate >= '2015-01-01'::date)
  206. -> Seq Scan on ft12 (cost=0.00..2.25 rows=100 width=0)
  207. Filter: (sdate >= '2015-01-01'::date)
  208. (28 rows)
  209.  
  210.  
  211.  
  212. # EXPLAIN SELECT count(*) FROM ft WHERE sdate between '2015-03-01' and '2015-04-01';
  213. QUERY PLAN
  214. -----------------------------------------------------------------------------------------
  215. Aggregate (cost=5.25..5.26 rows=1 width=0)
  216. -> Append (cost=0.00..5.00 rows=102 width=0)
  217. -> Seq Scan on ft (cost=0.00..0.00 rows=1 width=0)
  218. Filter: ((sdate >= '2015-03-01'::date) AND (sdate <= '2015-04-01'::date))
  219. -> Seq Scan on ft03 (cost=0.00..2.50 rows=100 width=0)
  220. Filter: ((sdate >= '2015-03-01'::date) AND (sdate <= '2015-04-01'::date))
  221. -> Seq Scan on ft04 (cost=0.00..2.50 rows=1 width=0)
  222. Filter: ((sdate >= '2015-03-01'::date) AND (sdate <= '2015-04-01'::date))
  223. (8 rows)
  224.  

猜你在找的Postgre SQL相关文章