Postgres连接从PHP变慢 [英] Postgres connection is slow from PHP
问题描述
我用php连接Postgres服务器有点麻烦。我刚开始使用Postgres + PHP组合,我意识到连接的建立确实很慢。
进行简单连接通常需要1秒钟,有时甚至超过2秒。而且它只是一个开发服务器,因此没有真正的流量。好的,服务器不是最好的,但是MySQL连接要快得多。
I have a little trouble with the Postgres server connection from php. I just started working with Postgres + PHP combo, and I realized that the connection establishment is really slow.
It usually takes 1s or sometimes more than 2 seconds to make a simple connection. And it's only a development server, so there is no real traffic. Ok, the server isn't the best, but MySQL connection is much faster.
连接后,一切运行良好,每个查询都按我的期望运行。应用程序的运行时间约为10%,连接约为90%。真的很奇怪,因为使用mysql数据库层确实非常快。
After connecting, everything goes well, every query runs how I expect. The application running time is about 10% and the connection is about 90%. Really strange, because with the mysql database layer it is really fast.
可能是什么问题?
我尝试过PDO,pg_pconnect,pg_connect,但是每次结果都是一样的。
I've tried with PDO, pg_pconnect, pg_connect, but everytime the result is the same.
它可能是Postgres配置错误?但是查询运行很快,只有连接建立很慢。我不知道。
It can be a Postgres configuration error? But the queries run fast, only the connection establishment is slow. I have no idea.
PG:PostgreSQL 8.3.9
PHP:5.2.6
PG: PostgreSQL 8.3.9
PHP: 5.2.6
预先感谢您!
配置:
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost', '*' = all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
# Note: Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction). You might
# also need to raise shared_buffers to support more connections.
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directory = '/var/run/postgresql' # (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# (change requires restart)
# - Security and Authentication -
#authentication_timeout = 1min # 1s-600s
ssl = true # (change requires restart)
#ssl_ciphers = 'ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH' # allowed SSL ciphers
# (change requires restart)
#password_encryption = on
#db_user_namespace = off
# Kerberos and GSSAPI
#krb_server_keyfile = '' # (change requires restart)
#krb_srvname = 'postgres' # (change requires restart, Kerberos only)
#krb_server_hostname = '' # empty string matches any keytab entry
# (change requires restart, Kerberos only)
#krb_caseins_users = off # (change requires restart)
#krb_realm = '' # (change requires restart)
# - TCP Keepalives -
# see "man 7 tcp" for details
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
# 0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
# 0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;
# 0 selects the system default
#------------------------------------------------------------------------------
# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------
# - Memory -
shared_buffers = 24MB # min 128kB or max_connections*16kB
# (change requires restart)
#temp_buffers = 8MB # min 800kB
#max_prepared_transactions = 5 # can be 0 or more
# (change requires restart)
# Note: Increasing max_prepared_transactions costs ~600 bytes of shared memory
# per transaction slot, plus lock space (see max_locks_per_transaction).
#work_mem = 1MB # min 64kB
#maintenance_work_mem = 16MB # min 1MB
#max_stack_depth = 2MB # min 100kB
# - Free Space Map -
max_fsm_pages = 153600 # min max_fsm_relations*16, 6 bytes each
# (change requires restart)
#max_fsm_relations = 1000 # min 100, ~70 bytes each
# (change requires restart)
# - Kernel Resource Usage -
#max_files_per_process = 1000 # min 25
# (change requires restart)
#shared_preload_libraries = '' # (change requires restart)
# - Cost-Based Vacuum Delay -
#vacuum_cost_delay = 0 # 0-1000 milliseconds
#vacuum_cost_page_hit = 1 # 0-10000 credits
#vacuum_cost_page_miss = 10 # 0-10000 credits
#vacuum_cost_page_dirty = 20 # 0-10000 credits
#vacuum_cost_limit = 200 # 1-10000 credits
# - Background Writer -
#bgwriter_delay = 200ms # 10-10000ms between rounds
#bgwriter_lru_maxpages = 100 # 0-1000 max buffers written/round
#bgwriter_lru_multiplier = 2.0 # 0-10.0 multipler on buffers scanned/round
推荐答案
在以下情况下始终关闭SSL:你不使用它。而且,如果要使用SSL,请确保 pg_hba.conf
也具有正确的设置以要求安全连接。
Always turn SSL off if you don't use it. And if you want to use SSL, make sure pg_hba.conf
has the proper settings to demand a secure connection as well.
这篇关于Postgres连接从PHP变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!