如何查看PostgreSQL执行效率低的SQL
答案:2 悬赏:30 手机版
解决时间 2021-03-21 01:58
- 提问者网友:精神病院里
- 2021-03-20 17:42
如何查看PostgreSQL执行效率低的SQL
最佳答案
- 五星知识达人网友:过活
- 2021-03-20 18:11
postgresql(8.2)的配置文件中有一个参数log_min_duration_statement,意思是只log执行时间大于设定值的语句,如果设为0,表示log所有语句;如果设为-1,表示不log任何语句。
看起来,这个配置选项对性能的调整是很有用的,比如可以设置:
log_min_duration_statement = 1000
则只log执行时间大于1s的语句,重点优化这些sql语句就好了。
然而,奇怪的,这个选项不太容易生效!经过反复试验,原来需要如下配置:
#debug_print_parse = off
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = off
log_connections = off
#log_disconnections = off
log_duration = off
log_line_prefix = '%t [%p]: [%l-1] ' # Special values:
# %u = user name
# %d = database name
# %r = remote host and port
# %h = remote host
# %p = PID
# %t = timestamp (no milliseconds)
# %m = timestamp with milliseconds
# %i = command tag
# %c = session id
# %l = session line number
# %s = session start timestamp
# %x = transaction id
# %q = stop here in non-session
# processes
# %% = '%'
# e.g. '<%u%%%d> '
log_statement = 'none' # none, mod, ddl, all
#log_statement = 'all' # none, mod, ddl, all
#log_hostname = off
注意看上面的其中两个选项的设置:
log_duration = off
log_statement = 'none'
这两个选项的意思是不log任何sql语句和执行时间,但是恰恰是关闭了这两个,log_min_duration_statement才会生效!可能postgresql内部 对这两个选项做了“互斥”处理吧。
看起来,这个配置选项对性能的调整是很有用的,比如可以设置:
log_min_duration_statement = 1000
则只log执行时间大于1s的语句,重点优化这些sql语句就好了。
然而,奇怪的,这个选项不太容易生效!经过反复试验,原来需要如下配置:
#debug_print_parse = off
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = off
log_connections = off
#log_disconnections = off
log_duration = off
log_line_prefix = '%t [%p]: [%l-1] ' # Special values:
# %u = user name
# %d = database name
# %r = remote host and port
# %h = remote host
# %p = PID
# %t = timestamp (no milliseconds)
# %m = timestamp with milliseconds
# %i = command tag
# %c = session id
# %l = session line number
# %s = session start timestamp
# %x = transaction id
# %q = stop here in non-session
# processes
# %% = '%'
# e.g. '<%u%%%d> '
log_statement = 'none' # none, mod, ddl, all
#log_statement = 'all' # none, mod, ddl, all
#log_hostname = off
注意看上面的其中两个选项的设置:
log_duration = off
log_statement = 'none'
这两个选项的意思是不log任何sql语句和执行时间,但是恰恰是关闭了这两个,log_min_duration_statement才会生效!可能postgresql内部 对这两个选项做了“互斥”处理吧。
全部回答
- 1楼网友:胯下狙击手
- 2021-03-20 18:30
select
procpid,
start,
now() - start as lap,
current_query
from
(select
backendid,
pg_stat_get_backend_pid(s.backendid) as procpid,
pg_stat_get_backend_activity_start(s.backendid) as start,
pg_stat_get_backend_activity(s.backendid) as current_query
from
(select pg_stat_get_backend_idset() as backendid) as s
) as s
where
current_query <> ''
order by
lap desc;
procpid:进程id
start:进程开始时间
lap:经过时间
current_query:执行中的sql
怎样停止正在执行的sql
select pg_cancel_backend(进程id);
或者用系统函数
kill -9 进程id;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯