欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年01月26日 10:32:01   作者:AnAnDawn  
這篇文章主要介紹了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;

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • PostgreSQL?流復(fù)制配置環(huán)境搭建過程

    PostgreSQL?流復(fù)制配置環(huán)境搭建過程

    PostgreSQL 流復(fù)制是 9.0 提供的一種新的 WAL 傳遞方法,使用流復(fù)制時(shí),每當(dāng) Primary 節(jié)點(diǎn) WAL 產(chǎn)生,就會(huì)馬上傳遞到 Standby 節(jié)點(diǎn),流復(fù)制提供異步和同步兩種模式,同步模式可以保障數(shù)據(jù) 0 丟失,這篇文章主要介紹了PostgreSQL?流復(fù)制搭建,需要的朋友可以參考下
    2023-09-09
  • Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫的方法

    Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫的方法

    這篇文章主要介紹了Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Postgresql - 查看鎖表信息的實(shí)現(xiàn)

    Postgresql - 查看鎖表信息的實(shí)現(xiàn)

    這篇文章主要介紹了Postgresql 查看鎖表信息的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • PostgreSQL function返回多行的操作

    PostgreSQL function返回多行的操作

    這篇文章主要介紹了PostgreSQL function返回多行的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • PostgreSQL 安裝和簡(jiǎn)單使用

    PostgreSQL 安裝和簡(jiǎn)單使用

    ostgreSQL是現(xiàn)在比較流行的數(shù)據(jù)庫之一,這個(gè)起源于伯克利(BSD)的數(shù)據(jù)庫研究計(jì)劃目前已經(jīng)衍生成一項(xiàng)國際開發(fā)項(xiàng)目,并且有非常廣泛的用戶。
    2009-08-08
  • 基于postgresql行級(jí)鎖for update測(cè)試

    基于postgresql行級(jí)鎖for update測(cè)試

    這篇文章主要介紹了基于postgresql行級(jí)鎖for update測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • PostgreSQL實(shí)時(shí)查看數(shù)據(jù)庫實(shí)例正在執(zhí)行的SQL語句實(shí)例詳解

    PostgreSQL實(shí)時(shí)查看數(shù)據(jù)庫實(shí)例正在執(zhí)行的SQL語句實(shí)例詳解

    在任何數(shù)據(jù)庫中,分析和優(yōu)化SQL的執(zhí)行,最重要的工作就是執(zhí)行計(jì)劃的解讀,而說到執(zhí)行計(jì)劃得先了解postgresql的查詢執(zhí)行過程,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL實(shí)時(shí)查看數(shù)據(jù)庫實(shí)例正在執(zhí)行的SQL語句的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • PostgreSQL 對(duì)數(shù)組的遍歷操作

    PostgreSQL 對(duì)數(shù)組的遍歷操作

    這篇文章主要介紹了PostgreSQL 對(duì)數(shù)組的遍歷操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • postgresql varchar字段regexp_replace正則替換操作

    postgresql varchar字段regexp_replace正則替換操作

    這篇文章主要介紹了postgresql varchar字段regexp_replace正則替換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • postgres array_to_string和array的用法講解

    postgres array_to_string和array的用法講解

    這篇文章主要介紹了postgres array_to_string和array的用法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評(píng)論