域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過
這篇文章主要介紹了Postgresql 動(dòng)態(tài)統(tǒng)計(jì)某一列的某一值出現(xiàn)的次數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧。
實(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:用于查詢時(shí)間格式化,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這個(gè)列的值為1的時(shí)候有多少行,后面的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 "順序掃描獲取活動(dòng)行的數(shù)量",
idx_scan as "索引掃描次數(shù)",
idx_tup_fetch as "索引掃描獲取活動(dòng)行的數(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)前活動(dòng)行估計(jì)數(shù)量",
n_dead_tup as "當(dāng)前死亡行的估計(jì)數(shù)量",
n_mod_since_analyze as "最后一次分析后被修改的行估計(jì)數(shù)量",
last_vacuum as "上次被手動(dòng)清理的時(shí)間(不統(tǒng)計(jì)VACUUM FULL)",
last_autovacuum as "上次自動(dòng)清理的時(shí)間",
last_analyze as "上次手動(dòng)分析的時(shí)間",
last_autoanalyze as "上次自動(dòng)清理分析的時(shí)間",
vacuum_count as "手動(dòng)清理的次數(shù)",
autovacuum_count as "自動(dòng)清理的次數(shù)",
analyze_count as "手動(dòng)分析的次數(shù)",
autoanalyze_count as "自動(dòng)分析的次數(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 "簡(jiǎn)單索引掃描獲取的活動(dòng)行數(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
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!