Come posso elencare tutte le tabelle di un database PostgreSQL e ordinarle per dimensione ?
Come posso elencare tutte le tabelle di un database PostgreSQL e ordinarle per dimensione ?
Risposte:
select table_name, pg_relation_size(quote_ident(table_name))
from information_schema.tables
where table_schema = 'public'
order by 2
Questo mostra la dimensione di tutte le tabelle nello schema publicse hai più schemi, potresti voler usare:
select table_schema, table_name, pg_relation_size('"'||table_schema||'"."'||table_name||'"')
from information_schema.tables
order by 3
Esempio SQLFiddle: http://sqlfiddle.com/#!15/13157/3
Elenco di tutte le funzioni di dimensione degli oggetti nel manuale .
select table_schema, table_name, pg_relation_size(table_schema||'.'||table_name) from information_schema.tables order by 3; grazie per l'aiuto!
select * from information_schema.tables where table_schema = 'public';restituisce zero righe anche se \dnmostra lo schema public. Forse un cambiamento in 9.5 ha causato questo?
Questo ti mostrerà il nome dello schema, il nome della tabella, le dimensioni e le dimensioni (necessarie per l'ordinamento).
SELECT
schema_name,
relname,
pg_size_pretty(table_size) AS size,
table_size
FROM (
SELECT
pg_catalog.pg_namespace.nspname AS schema_name,
relname,
pg_relation_size(pg_catalog.pg_class.oid) AS table_size
FROM pg_catalog.pg_class
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
) t
WHERE schema_name NOT LIKE 'pg_%'
ORDER BY table_size DESC;
Lo costruisco in base alle soluzioni da qui elenco di schemi con dimensioni (relative e assolute) in un database PostgreSQL
Questo sarà più chiaro.
pg_size_pretty(<numeric_value>) - converte il numero di byte in un formato leggibile dall'uomo.
pg_database_size(<db_name>)- ottiene la dimensione del database in byte .
pg_total_relation_size(<relation_name>)- ottiene la dimensione totale della tabella e il suo indice in byte .
pg_relation_size(<relation_name>)- ottiene la dimensione della relazione (tabella / indice) in byte .
pg_index_size(<relation_name>)- ottiene la dimensione dell'indice della relazione in byte .
current_database() - ottiene il database attualmente utilizzato su cui viene eseguita questa query.
Query:
select current_database() as database,
pg_size_pretty(total_database_size) as total_database_size,
schema_name,
table_name,
pg_size_pretty(total_table_size) as total_table_size,
pg_size_pretty(table_size) as table_size,
pg_size_pretty(index_size) as index_size
from ( select table_name,
table_schema as schema_name,
pg_database_size(current_database()) as total_database_size,
pg_total_relation_size(table_name) as total_table_size,
pg_relation_size(table_name) as table_size,
pg_indexes_size(table_name) as index_size
from information_schema.tables
where table_schema=current_schema() and table_name like 'table_%'
order by total_table_size
) as sizes;
Risultato:
database | total_database_size | schema_name | table_name | total_table_size | table_size | index_size
-----------+---------------------+-------------+------------+------------------+------------+------------
vigneshdb | 1586 MB | corpdata | table_aaa | 16 kB | 0 bytes | 8192 bytes
vigneshdb | 1586 MB | corpdata | table_bbb | 24 kB | 0 bytes | 16 kB
vigneshdb | 1586 MB | corpdata | table_ccc | 640 kB | 112 kB | 488 kB
vigneshdb | 1586 MB | corpdata | table_ddd | 9760 kB | 3152 kB | 6568 kB
vigneshdb | 1586 MB | corpdata | table_eee | 1120 MB | 311 MB | 808 MB
Il formato è umanizzato rappresentare in bytes, kB, MB, GB, e TB.
bytesa kB- inizia da10240 bytes
bytesa MB- inizia da 10485248 bytes= 10239.5 kB~10 MB
bytesa GB- inizia da 10736893952 bytes= 10239.5 MB~10 BG
bytesa TB- inizia da 10994579406848 bytes= 10239.5 GB~10 TB
Tutte le conversioni di unità iniziano da 10 + <unit>.
Per riferimento - Documentazione ufficiale Postgres
SELECT
relname as "Table",
pg_size_pretty(pg_total_relation_size(relid)) As "Size",
pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "External Size"
FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;
tratto da qui https://wiki-bsse.ethz.ch/display/ITDOC/Check+size+of+tables+and+objects+in+PostgreSQL+database
select table_name,n_live_tup, pg_size_pretty(pg_relation_size(table_name))
from information_schema.tables
inner join pg_stat_user_tables on table_name=relname
where table_schema = 'public'
order by 2 desc
Un'altra alternativa
Avevo bisogno di trovare quali tabelle utilizzano più spazio.
Sulla base di altre risposte, ho usato quella query:
select table_name, pg_size_pretty( pg_relation_size(quote_ident(table_name)) )
from information_schema.tables
where table_schema = 'public'
order by pg_relation_size(quote_ident(table_name)) desc
Ottengo il seguente risultato:
table_name pg_size_pretty
--------------------------------------
trade_binance 96 GB
closs_v2_binance_stash 46 GB
closs_bitfinex_stash 5725 MB
trade_bitfinex 5112 MB
...
api_requests 0 bytes
trade_huobi 0 bytes
Avrei dovuto comprare un SSD più grande.
select uv.a tablename, pg_size_pretty(uv.b) sizepretty
from (select tb.tablename a, pg_table_size('schemaname.'||tb.tablename::text) b
from pg_tables tb
where tb.schemaname ilike 'schemaname'
order by 2 desc
) uv
\d+ti mostrerà queste informazioni, sebbene non ordinate.