From 582e7b6d9f133487077d90302538ab293bb5f179 Mon Sep 17 00:00:00 2001 From: zcp100_zcp100 Date: Thu, 11 Aug 2022 16:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=80=E4=B9=88=E6=98=AF=E6=85=A2SQL?= =?UTF-8?q?=E4=B8=94=E5=A6=82=E4=BD=95=E6=9F=A5=E7=9C=8B=E6=85=A2SQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\237\245\347\234\213\346\205\242SQL.md" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "content/zh/post/zhangcuiping/\344\273\200\344\271\210\346\230\257\346\205\242SQL\344\270\224\345\246\202\344\275\225\346\237\245\347\234\213\346\205\242SQL.md" diff --git "a/content/zh/post/zhangcuiping/\344\273\200\344\271\210\346\230\257\346\205\242SQL\344\270\224\345\246\202\344\275\225\346\237\245\347\234\213\346\205\242SQL.md" "b/content/zh/post/zhangcuiping/\344\273\200\344\271\210\346\230\257\346\205\242SQL\344\270\224\345\246\202\344\275\225\346\237\245\347\234\213\346\205\242SQL.md" new file mode 100644 index 00000000..e803800f --- /dev/null +++ "b/content/zh/post/zhangcuiping/\344\273\200\344\271\210\346\230\257\346\205\242SQL\344\270\224\345\246\202\344\275\225\346\237\245\347\234\213\346\205\242SQL.md" @@ -0,0 +1,59 @@ +title = "什么是慢SQL且如何查看慢SQL" + +date = "2022-08-11" + +tags = ["什么是慢SQL且如何查看慢SQL"] + +archives = "2022-04" + +author = "张翠娉" + +summary = "什么是慢SQL且如何查看慢SQL" + +img = "/zh/post/zhangcuiping/title/img.png" + +times = "10:20" + ++++ + +# 什么是慢SQL且如何查看慢SQL? + + + +## **介绍** + +某个SQL执行时间超过指定时间时称为慢SQL。我们可以查看慢SQL,包括历史慢SQL以及当前慢SQL。 + +## 查看历史慢SQL + +首先要设置log_min_duration_statement,记录慢SQL。 + +然后在参数log_directory 指定的目录中查看日志。 + +进入数据库的数据目录,找到postgresql.conf配置文件,设置log_min_duration_statement参数。 + +```bash +cd /opt/mogdb/data +``` + +## 查看当前慢SQL + +例如查询执行时间超过1秒的SQL + +``` sql +MogDB=# select * from pg_stat_activity where state<>'idle' and now()-query_start > interval '1 s' order by query_start; + datid | datname | pid | sessionid | usesysid | usename | application_name | client_addr | client_hostname | client_port | back +end_start | xact_start | query_start | state_change | waiting | enqueue | state | resourc +e_pool | query_id | query | connection_info | unique_sql_id | trace_id +-------+----------+----------------+----------------+----------+---------+------------------+-------------+-----------------+-------------+------------- +------------------+-------------------------------+-------------------------------+-------------------------------+---------+---------+--------+-------- +-------+----------+----------------------------------------+-----------------+---------------+---------- + 16019 | postgres | 70396694860272 | 70396694860272 | 10 | ommdoc | workload | | | | 2022-07-25 1 +5:15:34.954987+08 | 2022-07-25 15:15:34.957823+08 | 2022-07-25 15:15:34.957823+08 | 2022-07-25 15:15:34.959476+08 | f | | active | default +_pool | 0 | WLM fetch collect info from data nodes | | 0 | +(1 row) + +MogDB=# +``` + + -- Gitee