PG常用的幾個查詢(1)

2021-08-27 12:08:29 字數 1793 閱讀 2448

select version(); ---檢視pg版本

select current_database(); --檢視當前資料庫

select current_user; --檢視當前使用者

psql -c "select version()" ----執行單條sql命令

psql -f ***x.sql -----可以把多條命令寫入檔案,用-f執行

select pg_database_size(current_database()); 檢視當前庫大小

select sum(pg_database_size(datname)) from pg_database; 查詢所有庫大小之和

select pg_relation_size('accounts'); ---查詢表大小

select pg_total_relation_size('accounts'); ---查詢包含表和表索引其他總大小

select pg_tablespace_size('tbs_index')/1024/1024 as "size m"; 檢視表空間大小

檢視top10大表:

select table_name

,pg_relation_size(table_name) as size

from information_schema.tables

where table_schema not in ('information_schema',

'pg_catalog')

order by size desc

limit 10;

快速評估表的行數:(一般都是大表,如果用count比較耗時間)

select (case when reltuples > 0 then

pg_relation_size('mytable')/(8192*relpages/reltuples)

else 0

end)::bigint as estimated_row_count

from pg_class

where oid = 'mytable'::regclass;

查詢等待會話

select

w.current_query as waiting_query,

w.procpid as w_pid,

w.usename as w_user,

l.current_query as locking_query,

l.procpid as l_pid,

l.usename as l_user,

t.schemaname || '.' || t.relname as tablename

from pg_stat_activity w

join pg_locks l1 on w.procpid = l1.pid and not l1.granted

join pg_locks l2 on l1.relation = l2.relation and l2.granted

join pg_stat_activity l on l2.pid = l.procpid

join pg_stat_user_tables t on l1.relation = t.relid

where w.waiting;

select date_trunc('second',

current_timestamp - pg_postmaster_start_time()) as uptime; ----資料庫開啟多久

select pg_postmaster_start_time(); --------什麼時候開啟的

幾個常用的查詢演算法

有序陣列二分查詢演算法函式sq dichotomy search0 用陣列實現 引數描述 int array 被查詢陣列 int n 被查詢陣列元素個數 int key 被查詢的關鍵值 返回值 如果沒有找到 sq dichotomy search0 1 否則 sq dichotomy search0...

pg幾個重要的引數調優

pg資料庫在不同的業務場景下,引數最好也需要進行針對的調整。預設值是在確保資源消耗最小的情況下,pg都能夠執行起來,不會導致任何致命性的威脅。而實際中,預設的引數需要進行優化來達到效能的最大化,本文介紹一些常用的引數配置。pg資料庫預設安裝完後在data目錄會有乙個postgresql.conf檔案...

常用的幾個SQL 查詢語句

記錄新增 insert into 表名 欄位1,欄位2 values 值1,值2 insert into tbclass classname values 測試1 insert into tbclass classname values 測試2 insert into tbclass classna...