• SQL语句


    SQL语句

    1.1.  查询表和索引的大小

    SELECT

           pretty_sizes. 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,

           pretty_sizes.reltuples AS COUNT,

      pretty_sizes.relpages

    FROM

           (

                  SELECT

                         all_tables. TABLE_NAME,

                         pg_table_size (all_tables. TABLE_NAME) AS table_size,

                         pg_indexes_size (all_tables. TABLE_NAME) AS indexes_size,

                         pg_total_relation_size (all_tables. TABLE_NAME) AS total_size,

                         table_size.reltuples,

          table_size.relname,

          table_size.relpages

                  FROM

                         (

                                SELECT

                                       (

                                              '"' || table_schema || '"."' || TABLE_NAME || '"'

                                       ) AS TABLE_NAME, TABLE_NAME as tmp_name

                                FROM

                                       information_schema.tables

                         ) AS all_tables

                  LEFT JOIN (

                         SELECT

                                relname AS TABLE_NAME,relname,relpages,reltuples

                         FROM

                                pg_class

                         WHERE

                                relkind = 'r'

                  ) AS table_size ON (

                         table_size."table_name" = all_tables."tmp_name"

                  )

                  ORDER BY

                         total_size DESC

           ) AS pretty_sizes

    1.2.  系统表

    系统表介绍

           pg_locks 保存锁的信息

           pg_stat_activity  保存当前连接的信息

           pg_stat_replication 保存复制状态信息

           pg_stat_all_tables   包含所有表信息(user表和系统表)

           pg_stat_sys_tables    包含系统表信息

           pg_stat_user_tables  用户表,包含vacuum相关信息和数据库update/delete相关信息

           pg_stat_all/sys/user_tables    全部索引,系统表的索引,用户表的索引

           pg_stat_database  每个数据库中增删改查、提交,回滚,冲突的数量统计,以数据库为单位。

           pg_stat_bgwriter  checkpoint相关信息

           pg_stat_database_conflicts  数据库冲突信息的统计(死锁,冲突),以数据库为单位

    常用sql语句

    l  查询前10大的表

    select relname,relpages from pg_class order by relpages desc limit 10;

    l  查询对应数据库的活跃连接

           select * from pg_stat_activity where datname='testdb';

    l  查询活跃连接在执行的任务

    select client_addr, query_start,query from pg_stat_activity;

    l  关闭该数据库的活跃连接,但是自己不能关闭自己

           select pg_terminate_backend(pid) from pg_stat_activity where datname='testdb' and pid <> pg_backend_pid(); 

    l  查询当前事物ID

    select txid_current();                                                                                              

    l  查看oid和磁盘页的数量

           select oid,relpages from pg_class where relname='test';

    l  查询某个oid对应的信息   

    select * from pg_database where oid='16385';

    l  查看某个oid对应的磁盘文件路径

           select pg_relation_filepath(16392);   

           select pg_relation_filepath(oid),relpages from pg_class where relname = 'test1';       

    l  查询某个表的oid(oid可以理解为唯一标示,其作为这个表/库的文件夹名,在data目录可以找到的)

           select oid,* from pg_class where relname = 'gctest';

    l  查询某个表的磁盘文件,及页的数量

           select pg_relation_filepath(oid),relpages from pg_class where relname = 'test';         

    l  查询占用磁盘页数最大的表

           select relname,relpages from pg_class order by relpages desc limit 10;                     

    l  查询某个表的大小

           select pg_size_pretty(pg_relation_size('xxxx'));

    l  查询某个库的oid 

    SELECT oid from pg_database where datname='testdb';       

    l  查看库大小   

           select pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size from pg_database;

    1.3.  psql查询语句

    指令

    作用

    di+

    显示所有的索引及索引的大小

    l+

    列出所有数据库、大小、权限信息

    dn+

    列出schema对应用户的权限

    dp+

    列出表、视图对应用户的权限

    x

    设置查询结果为列式展示,再设置一次复原行级展示

    c dbname

    切换数据库

          

    1.4.  用户相关sql

    l  用户操作

           create user test with password 'test';         创建用户

           drop user test;                                          删除用户

           alter user test superuser;            修改用户为超级用户

           alter user test password '123456';          修改用户的密码

    l  用户权限

            grant select on all tables in schema public to admin;    当前数据库的所有查询权限

            revoke select on all tables in schema public from admin;

            grant connect on database testdb to admin;

            grant all on table test to admin;              赋予test表的所有权限给admin用户

            revoke all on table test from admin; 回收权限

            grant all privileges on database testdb to admin;         

    将数据库testdb的所有权限赋予用户admin

            revoke all privileges on database testdb from admin;   回收权限

    select * from pg_roles;      查询角色

    1.5.  表操作

    l  创建数据库

           CREATE DATABASE exampledb OWNER dbuser;

    l  创建表

    l  CREATE TABLE gctest AS SELECT * FROM generate_series(1,100000);  创建含有10W行数据的表

           create table test (id int);

           create table collectinfo(id VARCHAR, username VARCHAR, update_time timestamp default current_timestamp, click_num int, finished boolean, primary key(id));

           insert into test select generate_series(1,49);

    l  修改表

            alter table test add column name varchar;             增加列,列名为那么,表名为test

            alter table test drop column name;                       删除列

            alter table test alter column name set default 'gc'; 列设置默认值

    l  查询表中记录(行)的隐藏字段

           select xmin,xmax,cmin,cmax,ctid,* from test;

    l  分析查询语句

    explain (analyze,verbose,timing,costs,buffers) select count(*) from test1;

  • 相关阅读:
    Windows 7 32位上硬盘安装linux[ubuntu13.04] 双系统
    【笨嘴拙舌WINDOWS】BMP图片浏览器
    【笨嘴拙舌WINDOWS】设备无关图(*.bmp)
    【笨嘴拙舌WINDOWS】GDI对象之位图
    Android-Java-对象在内存中的简单关系图
    Android-Java-类与对象的关系
    Android-Java-面向对象与面向过程举例
    Android-Java-面向对象与面向过程的简单理解
    Android-ANR异常
    Android-Genymotion Unable to load VirtualBox engine
  • 原文地址:https://www.cnblogs.com/gc65/p/11011937.html
Copyright © 2020-2023  润新知