我最近遇到了一个问题,Rails 正在断开与我的 PostgreSQL 的连接,导致每个请求多次重新连接,从而显着降低了请求速度。我目前正在使用以下环境在 Mac OS X 上本地运行所有内容:
- Mac OS X 10.8(山狮)
- 战俘
- NginX (ssl unwrapping - 相同的应用程序重新连接行为即使没有启用此功能)
- PostgreSQL 9.2.2 通过 Homebrew
- 导轨 3.2.11
这是我的database.yml
:(数据库和用户名已编辑)
development:
adapter: postgresql
encoding: unicode
database: <database>
pool: 5
username: <user>
password:
development.log
这是典型请求期间的输出:(特定模型和渲染已编辑)
Connecting to database specified by database.yml
Started GET "/" for 127.0.0.1 at 2013-02-05 12:25:38 -0800
Processing by HomeController#index as HTML
... <app performs model loads and render calls>
Completed 200 OK in 314ms (Views: 196.0ms | ActiveRecord: 60.9ms)
Connecting to database specified by database.yml
Connecting to database specified by database.yml
我还在 Postgres 中启用了语句日志记录,以便更好地了解在给定请求期间数据库端到底发生了什么。这是来自的输出postgresql-<date>.log
:(特定于我的应用程序的查询已编辑)
LOG: connection received: host=[local]
LOG: connection authorized: user=<user> database=<database>
LOG: statement: set client_encoding to 'UTF8'
LOG: statement: set client_encoding to 'unicode'
LOG: statement: SHOW client_min_messages
LOG: statement: SET client_min_messages TO 'panic'
LOG: statement: SET standard_conforming_strings = on
LOG: statement: SET client_min_messages TO 'notice'
LOG: statement: SET time zone 'UTC'
LOG: statement: SHOW TIME ZONE
LOG: statement: SELECT 1
LOG: statement: SELECT 1
... <app makes queries for request>
LOG: disconnection: session time: 0:00:01.346 user=<user> database=<database> host=[local]
LOG: connection received: host=[local]
LOG: connection authorized: user=<user> database=<database>
LOG: statement: set client_encoding to 'UTF8'
LOG: statement: set client_encoding to 'unicode'
LOG: statement: SHOW client_min_messages
LOG: statement: SET client_min_messages TO 'panic'
LOG: statement: SET standard_conforming_strings = on
LOG: statement: SET client_min_messages TO 'notice'
LOG: statement: SET time zone 'UTC'
LOG: statement: SHOW TIME ZONE
LOG: statement: SELECT 1
LOG: connection received: host=[local]
LOG: connection authorized: user=<user> database=<database>
LOG: statement: set client_encoding to 'UTF8'
LOG: statement: set client_encoding to 'unicode'
LOG: statement: SHOW client_min_messages
LOG: statement: SET client_min_messages TO 'panic'
LOG: statement: SET standard_conforming_strings = on
LOG: statement: SET client_min_messages TO 'notice'
LOG: statement: SET time zone 'UTC'
LOG: statement: SHOW TIME ZONE
LOG: statement: SELECT 1
LOG: statement: SELECT 1
LOG: statement: SELECT 1
LOG: disconnection: session time: 0:00:00.750 user=<user> database=<database> host=[local]
LOG: disconnection: session time: 0:00:00.733 user=<user> database=<database> host=[local]
我很高兴更新相关配置或命令调用的日志输出。另外,为什么所有的SELECT 1
电话?他们的目的是什么?
谢谢!