Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. -- Function: public.export_sas7bdat(text, text)
  2.  
  3. -- DROP FUNCTION public.export_sas7bdat(text, text);
  4.  
  5. CREATE OR REPLACE FUNCTION public.export_sas7bdat(
  6. tbl text DEFAULT 'p1_currency'::text,
  7. path text DEFAULT '/media/sf_PinkTransfert/'::text)
  8. RETURNS text AS
  9. $BODY$
  10.  
  11. require(RPostgreSQL)
  12. require(haven)
  13. require(data.table)
  14.  
  15.  
  16. return('makes server explode out of RAM on big tables. NOT SAFE altoough brilliant')
  17.  
  18.  
  19. # NOT USED BY PL/R
  20. con = dbConnect(dbDriver("PostgreSQL"), user="selector", password="select01",
  21. host="192.168.1.50", dbname="postgres")
  22.  
  23. time_start = Sys.time()
  24.  
  25. data <- dbGetQuery(con, paste0("TABLE ", tbl))
  26.  
  27. time_get = Sys.time()
  28. pg.thrownotice(paste0('done fetch data: ', (time_get - time_start)))
  29.  
  30. write_sas(data, paste0(path, tbl, '.sas7dbat'))
  31. fwrite(data, paste0(path, tbl, '.csv'))
  32.  
  33. time_write = Sys.time()
  34.  
  35. pg.thrownotice(paste0('done export data: ', (time_write - time_get) ))
  36.  
  37. return(paste0(tbl, ' exported to ', path))
  38.  
  39. $BODY$
  40. LANGUAGE plr VOLATILE
  41. COST 100;
  42. ALTER FUNCTION public.export_sas7bdat(text, text)
  43. OWNER TO datamart;
  44.  
  45. SELECT
  46. nspname AS schemaname, relname, reltuples::bigint
  47. FROM pg_class C
  48. LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
  49. WHERE
  50. nspname NOT IN ('pg_catalog', 'information_schema') AND
  51. relkind='r'
  52. ORDER BY reltuples DESC;/*
  53.  
  54. CREATE UNLOGGED TABLE temp_neolaneid_sample AS
  55. SELECT p.primary_key FROM p10_client_propre p TABLESAMPLE SYSTEM(1)
  56.  
  57. DROP TABLE IF EXISTS temp_sales_sample;
  58. CREATE UNLOGGED TABLE temp_sales_sample AS
  59. SELECT * FROM p10_vente_propre p
  60. RIGHT JOIN temp_neolaneid_sample t ON t.primary_key = p.idclient;
  61.  
  62. CREATE INDEX ON temp_sales_sample (idclient);
  63. CREATE INDEX ON temp_sales_sample (datetransaction);
  64. ANALYSE temp_sales_sample;
  65.  
  66. -- pq y a t il des null ???? On les supprime
  67. DELETE FROM temp_sales_sample WHERE idclient is null;
  68.  
  69.  
  70. SELECT t.idclient
  71. , sum(t.ca)
  72. FROM temp_sales_sample t
  73. GROUP BY 1
  74. ORDER BY 2 DESC;
  75.  
  76. -- Check example
  77. SELECT * FROM temp_sales_sample WHERE idclient = '51165675';
  78.  
  79. SELECT date_trunc('month', datetransaction) as month_transac, SUM(ca) FROM temp_sales_sample WHERE idclient = '51165675'
  80. GROUP BY month_transac
  81. ORDER BY month_transac ;
  82.  
  83. -- TODO keep local curency name or drop local amount (ca_local)
  84. CREATE UNLOGGED TABLE IF NOT EXISTS temp_sales_sample_month_aggr AS
  85. SELECT idclient
  86. , date_trunc('month', datetransaction) as month_transac
  87. , SUM(ca) as ca
  88. , SUM(ca_local) as ca_local
  89. , SUM(qte) as qte
  90. FROM temp_sales_sample
  91. GROUP BY 1, 2
  92. ORDER BY 2;
  93.  
  94. CREATE INDEX ON temp_sales_sample_month_aggr (idclient);
  95. CREATE INDEX ON temp_sales_sample_month_aggr (month_transac);
  96. ANALYZE temp_sales_sample_month_aggr;
  97.  
  98.  
  99. */
  100.  
  101. SELECT t1.idclient
  102. , t1.month_transac
  103. , t1.ca
  104. , SUM(t2.ca) as ca_12dm
  105. FROM temp_sales_sample_month_aggr t1
  106. LEFT JOIN temp_sales_sample_month_aggr t2 USING (idclient)
  107. WHERE t1.idclient = '51165675' AND
  108. t2.month_transac >= (t1.month_transac - interval '12 months') AND t2.month_transac < t1.month_transac
  109. GROUP by t1.idclient
  110. , t1.month_transac
  111. , t1.ca
  112. ORDER BY 2
  113. LIMIT 200;SELECT *, pg_size_pretty(total_bytes) AS total
  114. , pg_size_pretty(index_bytes) AS INDEX
  115. , pg_size_pretty(toast_bytes) AS toast
  116. , pg_size_pretty(table_bytes) AS TABLE
  117. FROM (
  118. SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
  119. SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
  120. , c.reltuples::bigint AS row_estimate
  121. , pg_total_relation_size(c.oid) AS total_bytes
  122. , pg_indexes_size(c.oid) AS index_bytes
  123. , pg_total_relation_size(reltoastrelid) AS toast_bytes
  124. FROM pg_class c
  125. LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
  126. WHERE relkind = 'r'
  127. ) a
  128. ) a
  129. ORDER BY total_bytes DESC
  130. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement