首页 > 数据库 >openGauss\postgreSQL数据库性能查看

openGauss\postgreSQL数据库性能查看

时间:2024-05-15 09:42:43浏览次数:19  
标签:postgreSQL name text 0.00 when id query openGauss 数据库

1.查看系统CPU使用率

sar -u -f /var/log/sa/sa27 (sa27 根据时间变化,sa27是27号信息的记录)

结果输出:

15时52分01秒 CPU %user %nice %system %iowait %steal %idle
15时53分01秒 all 0.32 0.00 0.69 0.00 0.00 98.99
15时54分01秒 all 0.30 0.00 0.68 0.00 0.00 99.02
15时55分01秒 all 0.30 0.00 0.70 0.00 0.00 99.00
15时56分01秒 all 0.29 0.00 0.68 0.00 0.00 99.03
15时57分01秒 all 0.30 0.00 0.69 0.00 0.00 99.01
15时58分01秒 all 0.30 0.00 0.69 0.00 0.00 99.01
15时59分01秒 all 0.30 0.00 0.71 0.00 0.00 98.99
16时00分01秒 all 0.30 0.00 0.69 0.00 0.00 99.00
16时01分01秒 all 0.30 0.00 0.69 0.00 0.00 99.01
平均时间: all 0.33 0.00 0.61 0.03 0.00 99.02

 

2.查看数据库连接

select state,count(*) from pg_stat_activity group by state;

 


3.查看数据库慢SQL
1)整体慢SQL查询

select db_name,unique_query_id,count(*) from statement_history group by db_name,unique_query_id;

openGauss=# select db_name,unique_query_id,count(*) from statement_history group by db_name,unique_query_id;
db_name | unique_query_id | count
---------+-----------------+-------
gzdz | 2533841608 | 1
gzdz | 2066747755 | 3
gzdz | 1116594456 | 1
(3 rows)

2)具体慢SQL查询

openGauss=# select unique_query_id,query from statement_history;

 

unique_query_id | query
-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2066747755 | INSERT INTO sys_stu.log_stu (client, create_time, description, header, ip, method, params, result, sign, stu_id, time, url) +
| VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) +
| RETURNING id
2533841608 | INSERT INTO sys.log_change (change_content, change_type, create_time, person_id, person_type, sign, table_chinese_name, table_name, table_primary_key) +
| VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) +
| RETURNING id
2066747755 | INSERT INTO sys_stu.log_stu (client, create_time, description, header, ip, method, params, result, sign, stu_id, time, url) +
| VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) +
| RETURNING id
1116594456 | INSERT INTO sys_stu.log_operation_stu (certificate_no, certificate_type, create_time, description, ip, log_change_ids, log_stu_id, operation, sign, stu_id, stu_name)+
| VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) +
| RETURNING id
2066747755 | INSERT INTO sys_stu.log_stu (client, create_time, description, header, ip, method, params, result, sign, stu_id, time, url) +
| VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) +
| RETURNING id
(5 rows)

 

openGauss=# select db_name,unique_query_id,count(*) from statement_history group by db_name,unique_query_id order by 3;
db_name | unique_query_id | count
---------+-----------------+-------
gzdz | 1587254874 | 1
gzdz | 1961954918 | 1
gzdz | 2833510906 | 1
gzdz | 3913769193 | 1
gzdz | 2771099244 | 2
gzdz | 1116594456 | 5
gzdz | 2533841608 | 10
gzdz | 2533841608 | 11
gzdz | 4233328942 | 1441
(9 rows)

 

openGauss=# select query,finish_time-start_time exec_time from statement_history where unique_query_id ='4233328942' limit 1;
query | exec_time
-------------------------------------------------------------------------------------+-----------------
UPDATE exam.exam_place AS e +| 00:00:01.144499
SET applied_capacity = e.applied_capacity + ? +|
WHERE e.is_valid AND e.exam_place_code = $1 AND e.capacity - e.applied_capacity > ? |
(1 row)

 

openGauss=# select query_plan from statement_history where unique_query_id ='4233328942' limit 1;
query_plan
-----------------------------------------------------------------------------------------------------------
Datanode Name: dn_6001_6002_6003 +
Update on exam_place e (cost=0.00..2.48 rows=1 width=180) +
-> Index Scan using idx_exam_place_exam_place_code on exam_place e (cost=0.00..2.48 rows=1 width=180)+
Index Cond: ((exam_place_code)::text = '***'::text) +
Filter: (is_valid AND ((capacity - applied_capacity) > '***')) +
+

(1 row)

 

4. 锁等待查询:
1.创建视图v_locks_monitor

\c gzdz
create view v_locks_monitor as
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.transactionid,
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.transactionid,
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.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,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||
' , 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||
chr(10)||'--------'||chr(10), (case when granted then '0' else '1' end)
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 )
as lock_conflict
from t_unionall
group by
locktype,datname,relation,page,tuple,transactionid::text,classid,objid,objsubid;

 

2.查询锁等待

\c gzdz
select * from v_locks_monitor;

 

标签:postgreSQL,name,text,0.00,when,id,query,openGauss,数据库
From: https://www.cnblogs.com/shoulisun/p/18193162

相关文章

  • Python执行PostgreSQL数据库查询语句,并打印查询结果
    哈喽,大家好,我是木头左!准备工作:安装必要库和设置数据库连接在开始使用Python执行PostgreSQL数据库查询之前,需要确保已经安装了psycopg2这个库,它是Python语言中用来操作PostgreSQL数据库的一个适配器。可以通过以下命令进行安装:pipinstallpsycopg2-binary安装完成后,需要创......
  • ClickHouse vs StarRocks 全场景MPP数据库选型对比
    ClickHousevsStarRocks选型对比面向列存的DBMS新的选择Hadoop从诞生已经十三年了,Hadoop的供应商争先恐后的为Hadoop贡献各种开源插件,发明各种的解决方案技术栈,一方面确实帮助很多用户解决了问题,但另一方面因为繁杂的技术栈与高昂的维护成本,Hadoop也渐渐地失去了原本......
  • django启动时执行某个操作数据库的方法怎么实现
    为了让django启动时就执行某些方法做了如下尝试一、在Django中,可以通过AppConfig类的ready()方法来实现在Django启动时执行某个方法。首先,在你的应用的apps.py文件中,创建一个继承自AppConfig类的子类,并重写ready()方法。例如,假设你的应用名为myapp,则可以创建一个MyAppConfig类:......
  • openGauss 示例2-从MY向openGauss数据库进行数据迁移
    示例2:从MY迁移数据下面示例演示如何通过CopyManager从MY向openGauss进行数据迁移的过程。importjava.io.StringReader;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;i......
  • openGauss 事务隔离说明
    事务隔离说明openGauss基于MVCC(多版本并发控制)并结合两阶段锁的方式进行事务管理,其特点是读写之间不阻塞。SELECT是纯读操作,UPDATE和DELETE是读写操作。读写操作和纯读操作之间并不会发生冲突,读写操作之间也不会发生冲突。每个并发事务在事务开始时创建事务快照,并发事务之间不......
  • openGauss 数据安全维护建议
    数据安全维护建议为保证openGauss数据库中的数据安全,避免丢失数据,非法访问数据等事故发生,请仔细阅读以下内容。避免数据被丢失建议用户规划周期性的物理备份,且对备份文件进行可靠的保存。在系统发生严重错误的情况下,可以利用备份文件,将系统恢复到备份前的状态。避免数据被非法......
  • openGauss 使用gs_restore命令导入数据
    使用gs_restore命令导入数据操作场景gs_restore是openGauss数据库提供的与gs_dump配套的导入工具。通过该工具,可将gs_dump导出的文件导入至数据库。gs_restore支持导入的文件格式包含自定义归档格式、目录归档格式和tar归档格式。gs_restore具备如下两种功能。导入至数据库......
  • openGauss 使用gs_dump和gs_dumpall命令导出数据
    使用gs_dump和gs_dumpall命令导出数据概述导出单个数据库导出所有数据库无权限角色导出数据详情查看:https://opengauss.org详情查看:https://docs-opengauss.osinfra.cn......
  • openGauss 使用gsql元命令导入数据
    使用gsql元命令导入数据gsql工具提供了元命令\copy进行数据导入。\copy命令\copy命令格式以及说明参见表1\copy元命令说明。表1\copy元命令说明语法说明\copy{table[(column_list)]|(query)}{from|to}{filename|stdin|stdout|pstdin|......
  • openGauss 使用合并方式更新和插入数据
    使用合并方式更新和插入数据在用户需要将一个表中所有的数据或大量的数据添加至现有表的场景下,openGauss提供了MERGEINTO语句通过两个表合并的方式高效地将新数据添加到现有表。MERGEINTO语句将目标表和源表中数据针对关联条件进行匹配,若关联条件匹配时对目标表进行UPDATE,关联......