【问题标题】:Postgres connection is slow from PHP来自 PHP 的 Postgres 连接很慢
【发布时间】:2011-03-22 13:42:26
【问题描述】:

我在通过 php 连接 Postgres 服务器时遇到了一点问题。我刚开始使用 Postgres + PHP 组合,我意识到连接建立真的很慢。
建立一个简单的连接通常需要 1 秒,有时甚至超过 2 秒。而且它只是一个开发服务器,所以没有真正的流量。好的,服务器不是最好的,但是 MySQL 连接要快得多。

连接后,一切顺利,每个查询都按我的预期运行。应用程序运行时间约为 10%,连接约为 90%。真的很奇怪,因为有了mysql数据库层,速度真的很快。

可能是什么问题?

我尝试过 PDO、pg_pconnect、pg_connect,但每次结果都是一样的。

可能是 Postgres 配置错误?但是查询运行速度很快,只有连接建立速度很慢。我不知道。

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

【问题讨论】:

  • 您能告诉我们您的集群的连接设置吗? postgresql.org/docs/current/static/… SSL 连接要慢得多,这可能是(部分)问题。
  • 嘿,我编辑了我的帖子。如我所见,ssl 是真的,所以也许我们应该尝试不使用 ssl = true?
  • 除非您正在开发 SSL,否则请务必在开发服务器上禁用它。
  • SSL 禁用 = 与 mysql 的速度相同。谢谢!弗兰克海肯斯请发帖,我可以接受它作为一个很好的答案。

标签: php postgresql


【解决方案1】:

如果您不使用 SSL,请务必关闭它。如果您想使用 SSL,请确保 pg_hba.conf 具有适当的设置以要求安全连接。

【讨论】:

  • 恕我直言,大多数人不需要 SSL,应该将其关闭。不过,Debian 默认是开启的。
  • 是的,这个开发服务器有debian lenny,默认开启了pg ssl。
【解决方案2】:

如果 PostGres 在同一台服务器上,请尝试将您的连接配置为使用 Unix 套接字而不是 TCP 堆栈。您的延迟很可能是由 Postgres 服务器中的反向 DNS 解析或身份验证延迟引起的,所以如果 Unix 套接字不是一个选项,我将在 postgres 上启用调试日志记录并检查那里发生了什么。

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2013-11-25
    • 2014-09-07
    相关资源
    最近更新 更多