Advertisement
korenizla

прак№2незак

Aug 23rd, 2023 (edited)
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. drop table std2_27.table1;
  2.  
  3. create table std2_27.table1
  4. (
  5.     id integer,
  6.     not_id integer
  7. )
  8. distributed by (id);
  9.  
  10. insert into std2_27.table1
  11. select row_number() over() as id, not_id from (
  12.     select generate_series(1,(random() * 50)::integer) as not_id
  13.     from generate_series(1, 10000) as id
  14. ) a;
  15.  
  16. select gp_segment_id, count(*)
  17. from std2_27.table1
  18. group by 1
  19.  
  20. select (gp_toolkit.gp_skew_coefficients('std2_27.table1'::regclass)).skccoeff;
  21.  
  22. select not_id, count(*)
  23. from std2_27.table1
  24. group by 1
  25.  
  26. alter table std2_27.table1
  27. set with (reorganize=true)
  28. distributed by (not_id);
  29.  
  30. select gp_segment_id, count(*)
  31. from std2_27.table1
  32. group by 1
  33.  
  34. select(gp_toolkit.gp_skew_coefficients('std2_27.table1'::regclass)).skcoeff;
  35.  
  36.  
  37.  
  38. create table std2_27.table2
  39. (
  40.     id integer,
  41.     not_id integer
  42. )
  43. with(appendoptimized=true, orientation=column, compresstype=zstd, compresslevel=1)
  44. distributed by (id);
  45.  
  46. create table std2_27.table3
  47. (
  48.     field1 int,
  49.     field2 text
  50. )
  51. distributed replicated;
  52.  
  53. insert into std2_27.table3
  54. select a, md5(a::text)
  55. from generate_series(1, 2000) a;
  56.  
  57. select gp_segment_id, count(1) from std2_27.table3 group by 1;
  58.  
  59.  
  60.  
  61. create table std2_27.table4
  62. (
  63.     field1 int,
  64.     field2 text
  65. )
  66. distributed randomly;
  67.  
  68. insert into std2_27.table4
  69. select a, md5(a::text)
  70. from generate_series(1, 10000) a;
  71.  
  72. select gp_segment_id, count(1)
  73. from std2_27.table4
  74. group by 1;
  75.  
  76. truncate table std2_27.table4;
  77.  
  78. insert into std2_27.table4
  79. select 1, md5(1::text);
  80.  
  81. select gp_segment_id, count(1)
  82. from std2_27.table4
  83. group by 1;
  84.  
  85.  
  86.  
  87. create table std2_27.table5
  88. (
  89.     field1 int,
  90.     field2 text not null
  91. )
  92. distributed by(field1);
  93.  
  94. insert into std2_27.table5
  95. select a, null
  96. from generate_series(1, 2000) a;
  97.  
  98.  
  99.  
  100. create table std2_27.table6
  101. (
  102.     field1 int,
  103.     field2 text default 'hello'
  104. )
  105. distributed by(field1);
  106.  
  107. insert into std0.table6
  108. select a
  109. from generate_series(1, 2000) a;
  110.  
  111. select * from std2_27.table6;
  112.  
  113.  
  114.  
  115. create table std2_27.table7
  116. (
  117.     field1 int primary key,
  118.     field2 text
  119. )
  120. distributed by(field1);
  121.  
  122. insert into std2_27.table1 table7
  123. (field1, field2)
  124. values(1, 'PRIMARY KEY test')
  125.  
  126. insert into std2_27.table7
  127. field1, field2)
  128. values(1, 'PRIMARY KEY test')
  129.  
  130. insert into std2_27.table7
  131. field1, field2)
  132. values(1, 'PRIMARY KEY test')
  133.  
  134.  
  135.  
  136. create table std2_27.table8
  137. (
  138.     field1 int check (field1 > 10),
  139.     field2 text
  140. )
  141. distributed by(field1);
  142.  
  143. insert into std2_27.table8
  144. (field1, field2)
  145. values(1, 'check test');
  146.  
  147.  
  148. create table std2_27.table1 sales
  149. (
  150.     id int,
  151.     dt date,
  152.     amt decimal(10, 2)
  153. )
  154. distributed by (id)
  155. partition by range (dt)
  156. (
  157.     start (date '2016-01-01') inclusive
  158.     end (date '2017-01-01') exclusive
  159.     every (interval '1 month')
  160. );
  161.  
  162. insert into std2_27.sales
  163. values(1, '2016-01-02'::date, 145);
  164.  
  165. insert into std2_27.sales
  166. values(1, '2016-10-02'::date, 145);
  167.  
  168.  
  169. select * from std2_27.sales;
  170.  
  171. select * from std2_27.sales_1_prt_1;
  172.  
  173. select * from std2_27.sales_1_prt_10;
  174.  
  175. insert into std2_27.sales
  176. values(1, '2018-01-02'::date, 200);
  177.  
  178.  
  179. drop table std2_27.sales;
  180.  
  181. create table std2_27.sales
  182. (
  183.     id int,
  184.     dt date,
  185.     amt decimal(10,2)
  186. )
  187. distributed by (id)
  188. partition by range(dt)
  189. (
  190.     start (date '2016-01-01') inclusive
  191.     end (date '2017-01-01') exclusive
  192.     every (interval '1 month'),
  193.     default partition def
  194. );
  195.  
  196. insert into std2_27.sales
  197. values(1, '2018-01-02'::date, 200);
  198.  
  199. select * from std2_27.sales_1_prt_def;
  200.  
  201.  
  202.  
  203. alter table std2_27.sales
  204. split default partition start ('2018-01-01') end ('2018-02-01') exclusive;
  205.  
  206. select
  207.     partitiontablename,
  208.     partitionrangestart,
  209.     partitionrangeend
  210. from pg_partitions
  211. where
  212.     tablename='sales'
  213.     and schemaname = 'std2_27'
  214. order by partitionrangestart;
  215.  
  216. select * from std2_27.sales_1_prt_r790101298;
  217.  
  218.  
  219. drop table if exists std2_27.sales_2016
  220.  
  221. create table std2_27.sales_2016
  222. (
  223.     id int,
  224.     dt date,
  225.     amt decimal(10,2)
  226. )
  227. distributed by (id);
  228.  
  229. truncate table std2_27.sales;
  230.  
  231. insert into std2_27.sales_2016
  232. values(1, '2016-01-02'::date, 145);
  233.  
  234. alter table std2_27.sales
  235. exchange partition for (date '2016-10-01')
  236. with table std2_27.sales_2016
  237. with validation;
  238.  
  239. select * from std2_27.sales;
  240.  
  241. drop table std2_27.rank
  242.  
  243. create table std2_27.rank
  244. (
  245.     id int,
  246.     rank int,
  247.     year int,
  248.     gender char(1),
  249.     count int
  250. )
  251. distributed by (id)
  252. partition by range (year)
  253. (
  254.     start (2006) end (2016) every (1),
  255.     default partition extra
  256. );
  257.  
  258. select
  259.     partitiontablename,
  260.     partitionrangestart,
  261.     partitionrangeend
  262. from pg_partitions
  263. where
  264.     tablename='rank'
  265.     and schemaname = 'std2_27'
  266. order by partitionrangestart;
  267.  
  268.  
  269. -- партицирование по списку значений, если они заранее известны
  270. create table std2_27."list"
  271. (  
  272.     id int,
  273.     rank int,
  274.     year int,
  275.     gender char(1),
  276.     count int
  277. )
  278. distributed by (id)
  279. partition by list (gender)
  280. (
  281.     partition girls values ('f'),
  282.     partition boys values ('m'),
  283.     default partition other
  284. );
  285.  
  286. select
  287.     partitiontablename,
  288.     partitionname,
  289.     partitionlistvalues
  290. from pg_partitions
  291. where
  292.     tablename='list'
  293.     and schemaname = 'std2_27'
  294. order by partitionrangestart;
  295.  
  296.  
  297.  
  298. -- построчное хранение в append-optimized таблице
  299. -- эффективно когда при выборке выбираются все или почти все столбцы таблицы
  300. create table std2_27.lineitem_row (
  301.     "1_shipdate" date null,
  302.     "1_orderkey" int4 null,
  303.     "1_discount" numeric(19, 4) null,
  304.     "1_suppkey" int4 null,
  305.     "1_quantity" int4 null,
  306.     "1_returnflag" varchar(1) null,
  307.     "1_partkey" int4 null,
  308.     "1_linestatus" varchar(1) null,
  309.     "1_tax" numeric(19, 4) null,
  310.     "1_commitdate" date null,
  311.     "1_receipdate" date null,
  312.     "1_shipmode" varchar(10) null,
  313.     "1_shipinstruct" varchar(25) null,
  314.     "1_comment" varchar(44) null
  315. )
  316. with (
  317.     appendonly=true,
  318.     orientation=row,
  319.     compresstype=zstd,
  320.     compresslevel=1
  321. )
  322. distributed by ("1_orderkey");
  323.  
  324.  
  325. insert into std2_27.lineitem_row
  326. select * from std2_27.lineitem_ext;
  327.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement