|
| 1 | +-- vacuum (analyze, verbose); |
| 2 | + |
| 3 | +-- do $$declare r record; |
| 4 | +-- begin |
| 5 | +-- for r in ( |
| 6 | +-- select table_name |
| 7 | +-- from information_schema.tables |
| 8 | +-- where table_name ~ '^(django_|auth_|easyaudit_|clinicalcode_).*$' |
| 9 | +-- ) |
| 10 | +-- loop |
| 11 | +-- execute format('reindex table %s.%s;', quote_ident('public'), quote_ident(r.table_name)); |
| 12 | +-- end loop; |
| 13 | +-- end$$; |
| 14 | + |
| 15 | +-- vacuum (full, analyze, verbose); |
| 16 | + |
| 17 | +select |
| 18 | + table_name as name, |
| 19 | + pg_size_pretty(pg_total_relation_size(quote_ident(table_name))) as total_size, |
| 20 | + pg_size_pretty(pg_relation_size(quote_ident(table_name))) as table_size, |
| 21 | + pg_size_pretty(pg_indexes_size(quote_ident(table_name))) as index_size |
| 22 | + from information_schema.tables |
| 23 | + where table_schema = 'public' |
| 24 | + order by pg_total_relation_size(quote_ident(table_name)) desc; |
| 25 | + |
| 26 | +-- truncate table public.django_session; |
| 27 | + |
| 28 | +-- truncate |
| 29 | +-- table public.django_admin_log |
| 30 | +-- restart identity; |
| 31 | + |
| 32 | +-- truncate |
| 33 | +-- table public.easyaudit_crudevent |
| 34 | +-- restart identity; |
| 35 | + |
| 36 | +-- truncate |
| 37 | +-- table public.easyaudit_loginevent |
| 38 | +-- restart identity; |
| 39 | + |
| 40 | +-- truncate |
| 41 | +-- table public.easyaudit_requestevent |
| 42 | +-- restart identity; |
| 43 | + |
| 44 | +-- truncate |
| 45 | +-- table public.django_celery_beat_periodictask |
| 46 | +-- restart identity; |
| 47 | + |
| 48 | +-- truncate |
| 49 | +-- table public.django_celery_results_taskresult |
| 50 | +-- restart identity; |
| 51 | + |
| 52 | +-- alter sequence public.django_admin_log_id_seq restart with 1; |
| 53 | + |
| 54 | +-- alter sequence public.easyaudit_crudevent_id_seq restart with 1; |
| 55 | + |
| 56 | +-- alter sequence public.easyaudit_loginevent_id_seq restart with 1; |
| 57 | + |
| 58 | +-- alter sequence public.easyaudit_requestevent_id_seq restart with 1; |
| 59 | + |
| 60 | +-- alter sequence public.django_celery_beat_periodictask_id_seq restart with 1; |
| 61 | + |
| 62 | +-- alter sequence public.django_celery_results_taskresult_id_seq restart with 1; |
| 63 | + |
| 64 | +-- begin; |
| 65 | +-- create temporary table tmp_code( |
| 66 | +-- code text |
| 67 | +-- ); |
| 68 | + |
| 69 | +-- with |
| 70 | +-- ont as ( |
| 71 | +-- select cast(t0.properties::json->'code' as varchar) as code |
| 72 | +-- from public.clinicalcode_ontologytag as t0 |
| 73 | +-- where json_typeof(t0.properties::json->'code') = 'string' |
| 74 | +-- union all |
| 75 | +-- select t0.code::varchar |
| 76 | +-- from public.clinicalcode_code as t0 |
| 77 | +-- ), |
| 78 | +-- unq as ( |
| 79 | +-- select code::varchar |
| 80 | +-- from ont |
| 81 | +-- group by code |
| 82 | +-- ) |
| 83 | +-- insert into tmp_code(code) ( |
| 84 | +-- select t0.code |
| 85 | +-- from public.clinicalcode_snomed_codes as t0 |
| 86 | +-- left join unq as t1 |
| 87 | +-- on t0.code = t1.code |
| 88 | +-- where t1.code is null |
| 89 | +-- ); |
| 90 | +-- delete |
| 91 | +-- from public.clinicalcode_snomed_codes as t0 |
| 92 | +-- using tmp_code as t1 |
| 93 | +-- where t0.code = t1.code; |
| 94 | +-- commit; |
| 95 | + |
| 96 | +-- begin; |
| 97 | +-- create temporary table tmp_code( |
| 98 | +-- code text |
| 99 | +-- ); |
| 100 | + |
| 101 | +-- with |
| 102 | +-- unq as ( |
| 103 | +-- select code |
| 104 | +-- from public.clinicalcode_code |
| 105 | +-- group by code |
| 106 | +-- ) |
| 107 | +-- insert into tmp_code(code) ( |
| 108 | +-- select t0.term_id as code |
| 109 | +-- from public.clinicalcode_read_cd_cv3_terms_scd as t0 |
| 110 | +-- left join unq as t1 |
| 111 | +-- on t0.term_id = t1.code |
| 112 | +-- where t1.code is null |
| 113 | +-- ); |
| 114 | +-- delete |
| 115 | +-- from public.clinicalcode_read_cd_cv3_terms_scd as t0 |
| 116 | +-- using tmp_code as t1 |
| 117 | +-- where t0.term_id = t1.code; |
| 118 | +-- commit; |
| 119 | + |
| 120 | +-- with |
| 121 | +-- used as ( |
| 122 | +-- select t1.value::text::bigint |
| 123 | +-- from public.clinicalcode_genericentity as t0, |
| 124 | +-- json_array_elements(t0.template_data::json->'ontology') as t1 |
| 125 | +-- where json_typeof(t0.template_data::json->'ontology') = 'array' |
| 126 | +-- and json_array_length(t0.template_data::json->'ontology') > 0 |
| 127 | +-- union all |
| 128 | +-- select t1.value::text::bigint |
| 129 | +-- from public.clinicalcode_historicalgenericentity as t0, |
| 130 | +-- json_array_elements(t0.template_data::json->'ontology') as t1 |
| 131 | +-- where json_typeof(t0.template_data::json->'ontology') = 'array' |
| 132 | +-- and json_array_length(t0.template_data::json->'ontology') > 0 |
| 133 | +-- ), |
| 134 | +-- unq_used as ( |
| 135 | +-- select value |
| 136 | +-- from used |
| 137 | +-- group by value |
| 138 | +-- ) |
| 139 | +-- delete |
| 140 | +-- from public.clinicalcode_ontologytag as t0 |
| 141 | +-- where t0.id not in (table unq_used) |
| 142 | +-- and t0.id > 100; |
| 143 | + |
| 144 | + |
| 145 | +-- select t0.* |
| 146 | +-- from public.clinicalcode_ontologytag as t0 |
| 147 | +-- where t0.id in (table unq_used) |
| 148 | + -- join public.clinicalcode_ontologytag as t1 |
| 149 | + -- on t0.template_data::json->'ontology' @> t1.id, |
0 commit comments