列出PostgreSQL数据库中所有表的表大小

时间:2020-09-22 11:48:01

标签: sql postgresql

我可以通过以下方式获取Postgresql数据库中所有表的列数

SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(*) 
FROM INFORMATION_SCHEMA.COLUMNS 
GROUP BY TABLE_SCHEMA, TABLE_NAME;

我可以通过以下方式获取数据库中特定表中的行数:

SELECT  COUNT(*) FROM mytable

如何通过一次查询获取数据库中所有表的列和行

2 个答案:

答案 0 :(得分:1)

使用CTE组合查询,然后将其与一个posted on this answer结合使用,例如建议使用@a_horse_with_no_name

public class Matrix {

    private final int[][] grid;
    private final int width;
    private final int height;

    public Matrix() {
        this(3);
    }

    public Matrix(int size) {
        this(size, size);
    }

    public Matrix(int rows, int cols) {
        grid = createGrid(rows, cols);
        width = cols;
        height = rows;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append('[');

        for (int row = 0; row < grid.length; row++) {
            if (buf.length() > 1)
                buf.append(',');

            buf.append('[');

            for (int col = 0; col < grid[row].length; col++) {
                if (col > 0)
                    buf.append(',');
                buf.append(grid[row][col]);
            }

            buf.append(']');
        }

        return buf.append(']').toString();
    }

    public boolean isSquare() {
        return width == height;
    }

    public boolean isSameSize(Matrix m) {
        return m != null && width == m.width && height == m.height;
    }

    public void add(Matrix m) {
        if (!isSameSize(m))
            return;

        for (int row = 0; row < grid.length; row++)
            for (int col = 0; col < grid[row].length; col++)
                grid[row][col] += m.grid[row][col];
    }

    private static int[][] createGrid(int rows, int cols) {
        Random random = new Random();
        int[][] grid = new int[rows][cols];

        for (int row = 0; row < grid.length; row++)
            for (int col = 0; col < grid[row].length; col++)
                grid[row][col] = random.nextInt(10);

        return grid;
    }

    public static void main(String... args) {
        Random random = new Random();
        Matrix m = new Matrix();
        Matrix n = new Matrix(random.nextInt(7));
        Matrix o = new Matrix(random.nextInt(7), random.nextInt(7));
        System.out.println("First matrix:");
        System.out.print(m);
        System.out.println("Second matrix:");
        System.out.println(n);
        System.out.println("Third matrix:");
        System.out.println(o);

        if (m.isSameSize(n))
            System.out.println("First two are the same size");
        else
            System.out.println("First two are not the same size");

        if (o.isSquare())
            System.out.println("All three are square matrices");
        else
            System.out.println("Only first two are square matrices");

        n.add(o);
        System.out.println(n);
    }
}

答案 1 :(得分:0)

您可以在命令行中使用psql在所选数据库中输入\ d +,也可以从此处使用SQL语句:

https://wiki.postgresql.org/wiki/Disk_Usage