zoukankan      html  css  js  c++  java
  • postgres 查询表的大小 清空表

    查询表中数据大小:

    --数据库中单个表的大小(不包含索引)

    select pg_size_pretty(pg_relation_size('表名'));

    --查出所有表(包含索引)并排序
    SELECT table_schema || '.' || table_name AS table_full_name, pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size
    FROM information_schema.tables
    ORDER BY
    pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC limit 20

    --查出表大小按大小排序并分离data与index
    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

    删除表中数据:

    1.适用数据量较小的情况

    delete from tablename;

    2.适合删除大量数据,速度快

    TRUNCATE TABLE tablename;

    3.若该表有外键,要用级联方式删所有关联的数据

    TRUNCATE TABLE tablename CASCADE;

    直接删表:

    drop table if exists +表名

  • 相关阅读:
    将cvs迁移到svn
    wincvs,cvs,svn
    Open Source Camp 北京 2008技术交流盛会 感悟
    21,22,23,24日外出纪要
    10.31,11.1外出纪要
    虚拟经济区一行有感
    29,30号活动预告
    ror 2.1.2migration
    netbeans 6.5 release
    [转载]ruby on rails 每周精选二
  • 原文地址:https://www.cnblogs.com/pass-ion/p/15097344.html
Copyright © 2011-2022 走看看