zoukankan      html  css  js  c++  java
  • PostgreSQL查看表大小的命令

    SELECT
        table_name,
        pg_size_pretty(table_size) AS table_size,
        pg_size_pretty(indexes_size) AS indexes_size,
        pg_size_pretty(total_size) AS total_size
    FROM (
        SELECT
            table_name,
            pg_table_size(table_name) AS table_size,
            pg_indexes_size(table_name) AS indexes_size,
            pg_total_relation_size(table_name) AS total_size
        FROM (
            SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
            FROM information_schema.tables
        ) AS all_tables
        ORDER BY total_size DESC
    ) AS pretty_sizes;
    
                                      table_name                                   | table_size | indexes_size | total_size 
    -------------------------------------------------------------------------------+------------+--------------+------------
     "emg_search_cn_20170614"."place_address"                                      | 13 GB      | 10 GB        | 23 GB
     "emg_search_cn_20170614"."place_attributes"                                   | 6228 MB    | 17 GB        | 23 GB
     "emg_search_cn_20170614"."place_name"                                         | 5545 MB    | 2614 MB      | 8158 MB
     "emg_search_cn_20170614"."place_raw_category"                                 | 2132 MB    | 4582 MB      | 6715 MB
     "emg_search_cn_20170614"."place_main"                                         | 2668 MB    | 1226 MB      | 3895 MB
     "emg_search_jjj_20161128"."place_attributes"                                  | 422 MB     | 3081 MB      | 3503 MB
     "emg_search_jjj_20161128"."place_address"                                     | 869 MB     | 1785 MB      | 2654 MB
     "emg_search_cn_20170614"."place_category"                                     | 1010 MB    | 1226 MB      | 2237 MB
     "emg_search_cn_20170614"."place_chain"                                        | 656 MB     | 945 MB       | 1602 MB
     "emg_search_jjj_20161129"."place_address"                                     | 869 MB     | 283 MB       | 1152 MB
     "emg_search_jjj_20161125"."place_address"                                     | 787 MB     | 256 MB       | 1044 MB
     "emg_search_jjj_20161128"."place_raw_category"                                | 145 MB     | 810 MB       | 955 MB
     "emg_search_jjj_20161129"."place_attributes"                                  | 422 MB     | 521 MB       | 943 MB
     "emg_search_jjj_20161128"."place_name"                                        | 405 MB     | 473 MB       | 878 MB
     "emg_search_jjj_20161125"."place_attributes"                                  | 385 MB     | 475 MB       | 861 MB
     "emg_search_jjj_20161129"."place_name"                                        | 405 MB     | 76 MB        | 480 MB
     "emg_search_jjj_20161125"."place_name"                                        | 369 MB     | 69 MB        | 439 MB
     "emg_search_jjj_20161128"."place_main"                                        | 186 MB     | 223 MB       | 409 MB
     "emg_search_jjj_20161128"."place_category"                                    | 70 MB      | 223 MB       | 293 MB
     "emg_cn_170515"."emg_guangdongsheng_guangzhoushi_poi"                         | 267 MB     | 14 MB        | 281 MB
     "emg_cn_170515"."emg_shanghaishi_poi1_poi"                                    | 261 MB     | 14 MB        | 276 MB
    
    
    SELECT
        table_name,
        pg_size_pretty(table_size) AS table_size,
        pg_size_pretty(indexes_size) AS indexes_size,
        pg_size_pretty(total_size) AS total_size
    FROM (
        SELECT
            table_name,
            pg_table_size(table_name) AS table_size,
            pg_indexes_size(table_name) AS indexes_size,
            pg_total_relation_size(table_name) AS total_size
        FROM (
            -- tables from 'public'
            SELECT table_name
            FROM information_schema.tables
            where table_schema = 'public' and table_type = 'BASE TABLE'
            union
            -- materialized views
            SELECT oid::regclass::text as table_name
            FROM pg_class
            WHERE relkind = 'm'
            order by table_name
        ) AS all_tables
        -- ORDER BY total_size DESC
        order by table_name
    ) AS pretty_sizes
    
                           table_name                      | table_size | indexes_size | total_size 
    ------------------------------------------------------+------------+--------------+------------
     emg_search_cn_20170602.all_place_attributes_mv       | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_address_mv              | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_attributes_mv           | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_category_mv             | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_chain_mv                | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_embeded_ids             | 0 bytes    | 8192 bytes   | 8192 bytes
     emg_search_cn_20170602.place_full_view_static        | 8192 bytes | 24 kB        | 32 kB
     emg_search_cn_20170602.place_name_mv                 | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_phoneme_mv              | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_raw_category_mv         | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_relationship_child_mv   | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.place_relationship_parent_mv  | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170602.rich_attributes_mv            | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170614.all_place_attributes_mv       | 3805 MB    | 509 MB       | 4314 MB
     emg_search_cn_20170614.place_address_mv              | 7960 MB    | 509 MB       | 8469 MB
     emg_search_cn_20170614.place_attributes_mv           | 1854 MB    | 509 MB       | 2363 MB
     emg_search_cn_20170614.place_category_mv             | 1005 MB    | 509 MB       | 1514 MB
     emg_search_cn_20170614.place_chain_mv                | 1253 MB    | 276 MB       | 1530 MB
     emg_search_cn_20170614.place_embeded_ids             | 827 MB     | 0 bytes      | 827 MB
     emg_search_cn_20170614.place_full_view_static        | 19 GB      | 1537 MB      | 21 GB
     emg_search_cn_20170614.place_name_mv                 | 4179 MB    | 509 MB       | 4688 MB
     emg_search_cn_20170614.place_phoneme_mv              | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_cn_20170614.place_raw_category_mv         | 1494 MB    | 509 MB       | 2003 MB
     emg_search_cn_20170614.place_relationship_child_mv   | 30 MB      | 6944 kB      | 37 MB
     emg_search_cn_20170614.place_relationship_parent_mv  | 73 MB      | 31 MB        | 104 MB
     emg_search_cn_20170614.rich_attributes_mv            | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_jjj_20161125.all_place_attributes_mv      | 240 MB     | 32 MB        | 272 MB
     emg_search_jjj_20161125.place_address_mv             | 468 MB     | 32 MB        | 500 MB
     emg_search_jjj_20161125.place_attributes_mv          | 115 MB     | 32 MB        | 147 MB
     emg_search_jjj_20161125.place_category_mv            | 64 MB      | 32 MB        | 97 MB
     emg_search_jjj_20161125.place_chain_mv               | 8192 bytes | 8192 bytes   | 16 kB
     emg_search_jjj_20161125.place_embeded_ids            | 52 MB      | 32 MB        | 85 MB
     emg_search_jjj_20161125.place_full_view_static       | 1145 MB    | 97 MB        | 1243 MB
    
     
    SELECT *, pg_size_pretty(total_bytes) AS total
        , pg_size_pretty(index_bytes) AS INDEX
        , pg_size_pretty(toast_bytes) AS toast
        , pg_size_pretty(table_bytes) AS TABLE
      FROM (
      SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
          SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
                  , c.reltuples AS row_estimate
                  , pg_total_relation_size(c.oid) AS total_bytes
                  , pg_indexes_size(c.oid) AS index_bytes
                  , pg_total_relation_size(reltoastrelid) AS toast_bytes
              FROM pg_class c
              LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
              WHERE relkind = 'r'
      ) a
    ) a;
    
      oid  |      table_schema       |                         table_name                          | row_estimate | total_bytes | index_bytes | toast_bytes | table_bytes |   total    |   index    |   toast    |   table    
    -------+-------------------------+-------------------------------------------------------------+--------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
      2619 | pg_catalog              | pg_statistic                                                |         1444 |    25845760 |      532480 |     8650752 |    16662528 | 25 MB      | 520 kB     | 8448 kB    | 16 MB
      1247 | pg_catalog              | pg_type                                                     |         2363 |      802816 |      286720 |             |      516096 | 784 kB     | 280 kB     |            | 504 kB
      1260 | pg_catalog              | pg_authid                                                   |            1 |       73728 |       32768 |             |       40960 | 72 kB      | 32 kB      |            | 40 kB
      1249 | pg_catalog              | pg_attribute                                                |        37008 |     8650752 |     2490368 |             |     6160384 | 8448 kB    | 2432 kB    |            | 6016 kB
      1255 | pg_catalog              | pg_proc                                                     |         3327 |     1286144 |      393216 |       24576 |      868352 | 1256 kB    | 384 kB     | 24 kB      | 848 kB
      1418 | pg_catalog              | pg_user_mapping                                             |            0 |       16384 |       16384 |             |           0 | 16 kB      | 16 kB      |            | 0 bytes
      2604 | pg_catalog              | pg_attrdef                                                  |          478 |      425984 |       65536 |        8192 |      352256 | 416 kB     | 64 kB      | 8192 bytes | 344 kB
      2606 | pg_catalog              | pg_constraint                                               |          352 |      270336 |      163840 |        8192 |       98304 | 264 kB     | 160 kB     | 8192 bytes | 96 kB
      2610 | pg_catalog              | pg_index                                                    |         1096 |      393216 |      114688 |             |      278528 | 384 kB     | 112 kB     |            | 272 kB
      2617 | pg_catalog              | pg_operator                                                 |          765 |      245760 |       81920 |             |      163840 | 240 kB     | 80 kB      |            | 160 kB
      2753 | pg_catalog              | pg_opfamily                                                 |           86 |       81920 |       32768 |             |       49152 | 80 kB      | 32 kB      |            | 48 kB
      2616 | pg_catalog              | pg_opclass                                                  |          128 |       90112 |       32768 |             |       57344 | 88 kB      | 32 kB      |            | 56 kB
      2601 | pg_catalog              | pg_am                                                       |            5 |       73728 |       32768 |             |       40960 | 72 kB      | 32 kB      |            | 40 kB
      2602 | pg_catalog              | pg_amop                                                     |          436 |      163840 |       98304 |             |       65536 | 160 kB     | 96 kB      |            | 64 kB
      2603 | pg_catalog              | pg_amproc                                                   |          344 |      106496 |       49152 |             |       57344 | 104 kB     | 48 kB      |            | 56 kB
      2612 | pg_catalog              | pg_language                                                 |            4 |       73728 |       32768 |             |       40960 | 72 kB      | 32 kB      |            | 40 kB
      1262 | pg_catalog              | pg_database                                                 |            2 |       40960 |       32768 |             |        8192 | 40 kB      | 32 kB      |            | 8192 bytes
      2600 | pg_catalog              | pg_aggregate                                                |          133 |       65536 |       16384 |             |       49152 | 64 kB      | 16 kB      |            | 48 kB
      2618 | pg_catalog              | pg_rewrite                                                  |          142 |      925696 |       32768 |      671744 |      221184 | 904 kB     | 32 kB      | 656 kB     | 216 kB
      2620 | pg_catalog              | pg_trigger                                                  |            0 |       32768 |       24576 |        8192 |           0 | 32 kB      | 24 kB      | 8192 bytes | 0 bytes
      3466 | pg_catalog              | pg_event_trigger                                            |            0 |       16384 |       16384 |             |           0 | 16 kB      | 16 kB      |            | 0 bytes
      2609 | pg_catalog              | pg_description                                              |         3686 |      442368 |      139264 |        8192 |      294912 | 432 kB     | 136 kB     | 8192 bytes | 288 kB
      2605 | pg_catalog              | pg_cast                                                     |          198 |       81920 |       32768 |             |       49152 | 80 kB      | 32 kB      |            | 48 kB
      3501 | pg_catalog              | pg_enum                                                     |            0 |       24576 |       24576 |             |           0 | 24 kB      | 24 kB      |            | 0 bytes
      2615 | pg_catalog              | pg_namespace                                                |            6 |       73728 |       32768 |             |       40960 | 72 kB      | 32 kB      |            | 40 kB
      2607 | pg_catalog              | pg_conversion                                               |          132 |      106496 |       49152 |             |       57344 | 104 kB     | 48 kB      |            | 56 kB
      2608 | pg_catalog              | pg_depend                                                   |        15060 |     2998272 |     1933312 |             |     1064960 | 2928 kB    | 1888 kB    |            | 1040 kB
      2964 | pg_catalog              | pg_db_role_setting                                          |            0 |       32768 |       16384 |        8192 |        8192 | 32 kB      | 16 kB      | 8192 bytes | 8192 bytes
      1213 | pg_catalog              | pg_tablespace                                               |            2 |       73728 |       32768 |             |       40960 | 72 kB      | 32 kB      |            | 40 kB
      1136 | pg_catalog              | pg_pltemplate                                               |            8 |       57344 |       16384 |             |       40960 | 56 kB      | 16 kB      |            | 40 kB
      1261 | pg_catalog              | pg_auth_members                                             |            0 |       16384 |       16384 |             |           0 | 16 kB      | 16 kB      |            | 0 bytes
      1214 | pg_catalog              | pg_shdepend                                                 |          784 |      204800 |       98304 |             |      106496 | 200 kB     | 96 kB      |            | 104 kB
      2396 | pg_catalog              | pg_shdescription                                            |            1 |       65536 |       16384 |        8192 |       40960 | 64 kB      | 16 kB      | 8192 bytes | 40 kB
      3602 | pg_catalog              | pg_ts_config                                                |           16 |       73728 |       32768 |             |       40960 | 72 kB      | 32 kB      |            | 40 kB
      3603 | pg_catalog              | pg_ts_config_map                                            |          304 |       81920 |       32768 |             |       49152 | 80 kB      | 32 kB      |            | 48 kB
    
      
    SELECT nspname || '.' || relname AS "relation",
      pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
    FROM pg_class C
    LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
    WHERE nspname NOT IN ('pg_catalog', 'information_schema')
      AND C.relkind <> 'i'
      AND nspname !~ '^pg_toast'
    ORDER BY pg_total_relation_size(C.oid) DESC
    LIMIT 20;
    
                        relation                    | total_size 
    ------------------------------------------------+------------
     emg_search_cn_20170614.place_address           | 23 GB
     emg_search_cn_20170614.place_attributes        | 23 GB
     emg_search_cn_20170614.place_full_view_static  | 21 GB
     emg_search_cn_20170614.place_address_mv        | 8469 MB
     emg_search_cn_20170614.place_name              | 8158 MB
     emg_search_cn_20170614.place_raw_category      | 6715 MB
     emg_search_cn_20170614.place_name_mv           | 4688 MB
     emg_search_cn_20170614.all_place_attributes_mv | 4314 MB
     emg_search_cn_20170614.place_main              | 3895 MB
     emg_search_jjj_20161128.place_attributes       | 3503 MB
     emg_search_jjj_20161128.place_address          | 2654 MB
     emg_search_cn_20170614.place_attributes_mv     | 2363 MB
     emg_search_cn_20170614.place_category          | 2237 MB
     emg_search_cn_20170614.place_raw_category_mv   | 2003 MB
     emg_search_cn_20170614.place_chain             | 1602 MB
     emg_search_jjj_20161128.place_full_view_static | 1600 MB
     emg_search_cn_20170614.place_chain_mv          | 1530 MB
     emg_search_cn_20170614.place_category_mv       | 1514 MB
     emg_search_jjj_20161129.place_full_view_static | 1369 MB
     emg_search_jjj_20161125.place_full_view_static | 1243 MB
    (20 rows)
    
    
    SELECT
        table_schema || '.' || table_name AS TableName,
        pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS TableSize
    FROM information_schema.tables
    ORDER BY
        pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC;
        
                                     tablename                                 | tablesize  
    ---------------------------------------------------------------------------+------------
     emg_search_cn_20170614.place_address                                      | 23 GB
     emg_search_cn_20170614.place_attributes                                   | 23 GB
     emg_search_cn_20170614.place_name                                         | 8158 MB
     emg_search_cn_20170614.place_raw_category                                 | 6715 MB
     emg_search_cn_20170614.place_main                                         | 3895 MB
     emg_search_jjj_20161128.place_attributes                                  | 3503 MB
     emg_search_jjj_20161128.place_address                                     | 2654 MB
     emg_search_cn_20170614.place_category                                     | 2237 MB
     emg_search_cn_20170614.place_chain                                        | 1602 MB
     emg_search_jjj_20161129.place_address                                     | 1152 MB
     emg_search_jjj_20161125.place_address                                     | 1044 MB
     emg_search_jjj_20161128.place_raw_category                                | 955 MB
     emg_search_jjj_20161129.place_attributes                                  | 943 MB
     emg_search_jjj_20161128.place_name                                        | 878 MB
     emg_search_jjj_20161125.place_attributes                                  | 861 MB
     emg_search_jjj_20161129.place_name                                        | 480 MB
     emg_search_jjj_20161125.place_name                                        | 439 MB
     emg_search_jjj_20161128.place_main                                        | 409 MB
     emg_search_jjj_20161128.place_category                                    | 293 MB
     emg_cn_170515.emg_guangdongsheng_guangzhoushi_poi                         | 281 MB
     emg_cn_170515.emg_shanghaishi_poi1_poi                                    | 276 MB
     emg_search_jjj_20161129.place_raw_category                                | 275 MB
     emg_cn_170515.emg_guangdongsheng_shenzhenshi_poi                          | 249 MB

     查看当前库表和物化视图总大小

    SELECT
        inet_server_addr() AS instance_address,
        table_catalog AS database_name,
        table_schema AS schema_name,
        table_name,
        pg_table_size(table_full_name) AS table_size,
        pg_indexes_size(table_full_name) AS index_size,
        pg_total_relation_size(table_full_name) AS total_size,
        table_type
    FROM (
    
    SELECT
              ('"' || table_schema || '"."' || table_name || '"') AS table_full_name,
              table_catalog,
              table_schema,
              table_name,
              table_type
         FROM information_schema.tables
         WHERE table_schema NOT IN ('pg_catalog','information_schema','topology','tiger','tiger_data') and table_type='BASE TABLE'
         UNION ALL
             SELECT
                    ('"' || schemaname || '"."' || matviewname || '"') AS table_full_name,
                    current_database() AS table_catalog,
                    schemaname AS table_schema,
                    matviewname AS table_name,
                   'MATERIALIZED VIEW' as table_type
             FROM pg_matviews
         ) AS all_tables
    ORDER BY total_size DESC;     
  • 相关阅读:
    数据结构--链表基础练习题
    LeetCode 10.28每日一题1207. 独一无二的出现次数【简单】
    数据结构--链表
    LeetCode 10.25每日一题845. 数组中的最长山脉【中等】
    LeetCode 10.22每日一题763. 划分字母区间【中等】
    解决map热点与uni-app中map标签冲突的问题。(Vue Render函数应用)
    【Codeforces 1329A】Dreamoon Likes Coloring
    【Codeforces Alpha Round #20 C】Dijkstra?
    【 Educational Codeforces Round 93 (Rated for Div. 2) D】Colored Rectangles
    【Codeforces Round #643 (Div. 2) C】Count Triangles
  • 原文地址:https://www.cnblogs.com/ilifeilong/p/9244370.html
Copyright © 2011-2022 走看看