HighgoDB查詢慢SQL和阻塞SQL
1.修改配置,記錄SQL資訊
修改配置檔案postgresql.conf,設定以下引數
#開啟日誌記錄 logging_collector = on #設定日誌輸出格式,格式有stderr(預設), csvlog , syslog log_destination = 'csvlog’ #設定日誌存放位置,下面設定表示日誌存放在$PGDATA下hgdb_log日誌中 log_directory = 'hgdb_log’ #設定日誌截斷 log_truncate_on_rotation = on #設定日誌的名稱 log_filename = 'highgodb_%d.log’ #設定跟蹤的SQL語句級別,級別包含none(預設,只記錄出錯資訊), ddl, mod, all log_statement = all #記錄執行超過以下時間的SQL語句,單位毫秒 log_min_duration_statement = 5000
資料庫日誌保留數量通過設定引數log_truncate_on_rotation和log_filename進行控制,引數log_truncate_on_rotation設定為on後,通過log_filename控制日誌保留時間,常用設定如下 log_filename = 'highgodb-%I.log' #最多儲存12小時的日誌,每小時一個檔案 log_filename = 'highgodb-%H.log' #最多儲存24小時的日誌,每小時一個檔案 log_filename = 'highgodb-%w.log' #最多儲存一週的日誌,每天一個檔案 log_filename = 'highgodb-%d.log' #最多儲存一個月的日誌,每天一個檔案 log_filename = 'highgodb-%j.log' #最多儲存一年的日誌,每天一個檔案 |
資料庫日誌檔案一般較大,直接開啟不方便分析,可以通過將日誌檔案匯入到資料庫中,使用SQL進行分析,建表語句如下
CREATE TABLE highgodb_log ( log_time timestamp(3) with time zone, user_name text, database_name text, process_id integer, connection_from text, session_id text, session_line_num bigint, command_tag text, session_start_time timestamp with time zone, virtual_transaction_id text, transaction_id bigint, error_severity text, sql_state_code text, message text, detail text, hint text, internal_query text, internal_query_pos integer, context text, query text, query_pos integer, location text, application_name text, PRIMARY KEY (session_id, session_line_num) ); |
使用如下語句將日誌匯入到資料庫
COPY highgodb_log FROM '/path/to/highgodb-1.csv' WITH csv; |
使用SQL語句進行查詢,此處給出的示例是按執行時間排序,由於執行時間資訊存放在message列中,需要對該列進行擷取才能進行排序,根據日誌語言,需要將“執行時間”和擷取字元量進行修改。
select log_time,database_name,user_name,application_name,substr(message, 7,8),message from hgdblog where message like '%執行時間%' order by substr(message, 7,8) desc; |
2.查詢阻塞會話的SQL
語句如下
with t_wait as ( select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted, a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath, b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted ), t_run as ( select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted, a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath, b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted ), t_overlap as ( select r.* from t_wait w join t_run r on ( r.locktype is not distinct from w.locktype and r.database is not distinct from w.database and r.relation is not distinct from w.relation and r.page is not distinct from w.page and r.tuple is not distinct from w.tuple and r.virtualxid is not distinct from w.virtualxid and r.transactionid is not distinct from w.transactionid and r.classid is not distinct from w.classid and r.objid is not distinct from w.objid and r.objsubid is not distinct from w.objsubid and r.pid <> w.pid ) ), t_unionall as ( select r.* from t_overlap r union all select w.* from t_wait w ) select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid, string_agg( 'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)|| 'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' , FastPath: '||case when fastpath is null then 'NULL' else fastpath::text end||' , VirtualTransaction: '||case when virtualtransaction is null then 'NULL' else virtualtransaction::text end||' , Session_State: '||case when state is null then 'NULL' else state::text end||chr(10)|| 'Username: '||case when usename is null then 'NULL' else usename::text end||' , Database: '||case when datname is null then 'NULL' else datname::text end||' , Client_Addr: '||case when client_addr is null then 'NULL' else client_addr::text end||' , Client_Port: '||case when client_port is null then 'NULL' else client_port::text end||' , Application_Name: '||case when application_name is null then 'NULL' else application_name::text end||chr(10)|| 'Xact_Start: '||case when xact_start is null then 'NULL' else xact_start::text end||' , Query_Start: '||case when query_start is null then 'NULL' else query_start::text end||' , Xact_Elapse: '||case when (now()-xact_start) is null then 'NULL' else (now()-xact_start)::text end||' , Query_Elapse: '||case when (now()-query_start) is null then 'NULL' else (now()-query_start)::text end||chr(10)|| 'SQL (Current SQL in Transaction): '||chr(10)|| case when query is null then 'NULL' else query::text end, chr(10)||'--------'||chr(10) order by ( case mode when 'INVALID' then 0 when 'AccessShareLock' then 1 when 'RowShareLock' then 2 when 'RowExclusiveLock' then 3 when 'ShareUpdateExclusiveLock' then 4 when 'ShareLock' then 5 when 'ShareRowExclusiveLock' then 6 when 'ExclusiveLock' then 7 when 'AccessExclusiveLock' then 8 else 0 end ) desc, (case when granted then 0 else 1 end) ) as lock_conflict from t_unionall group by locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ; |
執行結果如下,如有大量鎖時,可以使用\x進行行列轉換,檢視起來更方便。
highgo-# locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ; locktype | datname | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | lock_conflict ---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------- transactionid | highgo | | | | | 760 | | | | Pid: 24355 + | | | | | | | | | | Lock_Granted: true , Mode: ExclusiveLock , FastPath: false , VirtualTransaction: 5/527 , Session_State: idle in transaction + | | | | | | | | | | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql + | | | | | | | | | | Xact_Start: 2021-01-28 15:15:46.810264+08 , Query_Start: 2021-01-28 15:16:06.108277+08 , Xact_Elapse: 00:00:55.57579 , Query_Elapse: 00:00:36.277777 + | | | | | | | | | | SQL (Current SQL in Transaction): + | | | | | | | | | | delete from host ; + | | | | | | | | | | -------- + | | | | | | | | | | Pid: 24383 + | | | | | | | | | | Lock_Granted: false , Mode: ShareLock , FastPath: false , VirtualTransaction: 4/14615 , Session_State: active + | | | | | | | | | | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql + | | | | | | | | | | Xact_Start: 2021-01-28 15:16:13.619987+08 , Query_Start: 2021-01-28 15:16:31.022072+08 , Xact_Elapse: 00:00:28.766067 , Query_Elapse: 00:00:11.363982+ | | | | | | | | | | SQL (Current SQL in Transaction): + | | | | | | | | | | delete from host ; (1 row) |
上面結果中,Lock_Granted: true 表示獲取鎖的會話,可以通過pid找到對應程式。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69994931/viewspace-2848521/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 查詢阻塞與被阻塞SQL語句2015-05-17SQL
- SQL慢查詢排查思路2021-07-27SQL
- SQL Server阻塞查詢語句2016-11-11SQLServer
- 查詢oracle比較慢的session和sql2014-02-18OracleSessionSQL
- 查詢oracle比較慢的session和SQL[轉]2012-08-09OracleSessionSQL
- 快速學會慢查詢SQL排查2022-12-08SQL
- mysql查詢效率慢的SQL語句2018-12-12MySql
- SQL Server查詢慢的解決方案2022-09-02SQLServer
- 查詢執行慢的SQL語句2017-04-12SQL
- SQL查詢的:子查詢和多表查詢2020-11-18SQL
- 在mysql查詢效率慢的SQL語句2019-04-18MySql
- 談談SQL慢查詢的解決思路2017-08-03SQL
- Sql語句本身的優化-定位慢查詢2018-10-11SQL優化
- 找出Mysql查詢速度慢的SQL語句2010-10-27MySql
- Linux下mysql配置慢日誌查詢,把查詢慢的sql記錄下來2017-05-27LinuxMySql
- SQL Server查詢速度慢原因及優化方法2010-06-07SQLServer優化
- 透過shell和sql結合查詢效能sql2015-12-26SQL
- 通過shell和sql結合查詢效能sql2014-08-18SQL
- 原生SQL查詢2018-11-14SQL
- SQL 聚合查詢2022-03-08SQL
- sql 查詢效率2010-08-27SQL
- sql子查詢2007-04-02SQL
- SQL--查詢2024-10-05SQL
- 通過spid,查詢執行慢的sql指令碼2014-01-06SQL指令碼
- SQL Server查詢速度慢的原因及優化方法2009-02-12SQLServer優化
- 一條查詢Oracle中的阻塞鎖(以及阻塞在哪個資料上)的SQL2014-04-17OracleSQL
- 【SQL查詢】集合查詢之INTERSECT2016-11-13SQL
- 《MySQL慢查詢優化》之SQL語句及索引優化2020-12-06MySql優化索引
- Mysql索引型別建立錯誤導致SQL查詢緩慢2017-03-02MySql索引型別
- SQL連線查詢2020-04-06SQL
- SQL高階查詢2019-05-30SQL
- sql常用查詢命令2021-09-11SQL
- SQL查詢總結2018-07-12SQL
- SQL 複雜查詢2022-03-14SQL
- sql: 查詢約束2016-10-13SQL
- SQL複雜查詢2017-04-07SQL
- SQL--子查詢2017-09-05SQL
- (1)SQL 基本查詢2017-01-17SQL