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慢查詢排查思路2021-07-27SQL
- 快速學會慢查詢SQL排查2022-12-08SQL
- mysql查詢效率慢的SQL語句2018-12-12MySql
- SQL Server查詢慢的解決方案2022-09-02SQLServer
- 在mysql查詢效率慢的SQL語句2019-04-18MySql
- SQL查詢的:子查詢和多表查詢2020-11-18SQL
- Sql語句本身的優化-定位慢查詢2018-10-11SQL優化
- 原生SQL查詢2018-11-14SQL
- SQL--查詢2024-10-05SQL
- SQL 聚合查詢2022-03-08SQL
- SQL查詢總結2018-07-12SQL
- SQL連線查詢2020-04-06SQL
- SQL高階查詢2019-05-30SQL
- sql常用查詢命令2021-09-11SQL
- SQL 複雜查詢2022-03-14SQL
- Sql介紹 與 Sql基礎查詢2024-09-26SQL
- 《MySQL慢查詢優化》之SQL語句及索引優化2020-12-06MySql優化索引
- SQL-小白最佳入門sql查詢一2020-01-11SQL
- sql查詢語句流程2024-05-05SQL
- SQL mother查詢語句2024-03-09SQL
- SQL 唯一查詢2024-05-31SQL
- 優化sql查詢速度2020-10-25優化SQL
- SQL查詢語句 (Oracle)2020-10-31SQLOracle
- sql 模糊查詢問題2021-05-31SQL
- sql查詢更新update select2020-12-27SQL
- SQL Server 查詢表註釋和欄位2020-12-07SQLServer
- pid,sid相互查詢,根據PID查詢sql2018-04-12SQL
- 為什麼 SQL 語句使用了索引,但卻還是慢查詢?2022-06-09SQL索引
- mybatis之sql查詢配置檔案resultType和resultMap2024-04-25MyBatisSQL
- 查詢oracle正在執行的SQL和事務2018-03-05OracleSQL
- 使用SSMS連線和查詢 SQL Server 例項2024-08-18SSMSQLServer
- ATM-簡單SQL查詢2018-09-25SQL
- SQL 三表聯合查詢2019-09-26SQL
- MongoDB 如何支援類 SQL 查詢2020-07-05MongoDBSQL
- 01-sql-聯合查詢2020-10-18SQL
- SQL的基礎查詢案例2020-10-13SQL
- SQL 查詢中的 NULL 值2020-04-05SQLNull
- ASP.Net LINQ to SQl查詢2018-04-06ASP.NETSQL