當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

Postgresql 動態(tài)統(tǒng)計(jì)某一列的某一值出現(xiàn)的次數(shù)實(shí)例

 2021-05-28 16:35  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

這篇文章主要介紹了Postgresql 動態(tài)統(tǒng)計(jì)某一列的某一值出現(xiàn)的次數(shù)實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。

實(shí)例解析:

select to_char(log.date, 'yyyy-MM-dd HH24') as hour,
  log.exten, sum(case log.grade when '1' then 1 else 0 end) as "1",
  sum(case log.grade when '2' then 1 else 0 end) as "2",
  sum(case log.grade when '3' then 1 else 0 end) as "3",
  sum(case log.grade when '4' then 1 else 0 end) as "4",
  sum(case log.grade when '5' then 1 else 0 end) as "5",
  log.direction from iface_satisfaction_investigation as log
where log.date >= '2017-08-03 00:00:00' and log.date < '2017-08-04 00:00:00'
group by hour,log.exten,log.direction order by hour,log.exten,log.direction asc

 

to_char:用于查詢時間格式化,to_char(log.date, 'yyyy-MM-dd HH24'),大致的結(jié)果是:2017-08-03 13

sum():毫無疑問是用來計(jì)算總和的。

sum(case log.grade when '1' then 1 else 0 end) 是計(jì)算什么呢?

他的意思就是:

計(jì)算grade這個列的值為1的時候有多少行,后面的sum(……)就類推了。

其他的也沒有什么好講的了

補(bǔ)充:PostgreSQL常用的統(tǒng)計(jì)信息

我就廢話不多說了,大家還是直接看代碼吧~

/*計(jì)算表的空間大小*/
select oid,table_schema as "模式",
 table_name as "表名",
 row_estimate::bigint as "表中的行數(shù)(估計(jì)值)",
 pg_size_pretty(total_bytes) as "總大小",
  pg_size_pretty(table_bytes) as "表大小",
 pg_size_pretty(index_bytes) as "索引大小",
  pg_size_pretty(toast_bytes) as "toast表總大小" 
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'
 ) t1
) t2 order by 2,3;
/*統(tǒng)計(jì)用戶表信息*/
select
 schemaname as "模式",
 relname as "表名",
 seq_scan as "順序掃描的次數(shù)",
 seq_tup_read as "順序掃描獲取活動行的數(shù)量",
 idx_scan as "索引掃描次數(shù)",
 idx_tup_fetch as "索引掃描獲取活動行的數(shù)量",
 n_tup_ins as "累計(jì)插入的行數(shù)",
 n_tup_upd as "累計(jì)更新的行數(shù)(包含HOT 更新的行)", 
 n_tup_del as "累計(jì)刪除的行數(shù)",
 n_live_tup as "當(dāng)前活動行估計(jì)數(shù)量",
 n_dead_tup as "當(dāng)前死亡行的估計(jì)數(shù)量",
 n_mod_since_analyze as "最后一次分析后被修改的行估計(jì)數(shù)量",
 last_vacuum as "上次被手動清理的時間(不統(tǒng)計(jì)VACUUM FULL)",
 last_autovacuum as "上次自動清理的時間",
 last_analyze as "上次手動分析的時間",
 last_autoanalyze as "上次自動清理分析的時間",
 vacuum_count as "手動清理的次數(shù)",
 autovacuum_count as "自動清理的次數(shù)",
 analyze_count as "手動分析的次數(shù)",
 autoanalyze_count as "自動分析的次數(shù)",
 pg_size_pretty(pg_table_size(relid)) as "表大小(不包含索引)"
from pg_stat_user_tables
order by 1;
/*統(tǒng)計(jì)用戶表IO信息*/
select
 schemaname as "模式",
 relname as "表名",
 heap_blks_read as "讀取的磁盤塊數(shù)量",
 heap_blks_hit as "緩沖區(qū)命中數(shù)量",
 idx_blks_read as "表上所有索引讀取的磁盤塊數(shù)",
 idx_blks_hit as "表上的所有索引緩沖區(qū)命中數(shù)量",
 toast_blks_read as "TOAST表(如果有)讀取的磁盤塊數(shù)",
 toast_blks_hit as "TOAST表(如果有)緩沖區(qū)命中數(shù)量",
 tidx_blks_read as "TOAST表索引(如果有)讀取的磁盤塊數(shù)",
 tidx_blks_hit as "TOAST表索引(如果有)緩沖區(qū)命中數(shù)量"
from pg_statio_user_tables
order by 1;
/*統(tǒng)計(jì)用戶索引信息*/
select
 indexrelid,
 schemaname as "模式",
 relname as "索引所在的表名稱",
 indexrelname as "索引名稱",
 idx_scan as "索引掃描次數(shù)",
 idx_tup_read as "索引掃描返回的索引項(xiàng)數(shù)量",
 idx_tup_fetch as "簡單索引掃描獲取的活動行數(shù)量",
 pg_size_pretty(pg_relation_size(indexrelid)) as "索引大小"
from pg_stat_user_indexes
order by 1,2;
/*追蹤函數(shù),需要打開track_functions參數(shù)(默認(rèn)關(guān)閉)*/
select * from pg_stat_user_functions;

文章來源:腳本之家

來源地址:https://www.jb51.net/article/204791.htm

申請創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)文章

熱門排行

信息推薦