当请求 ruby on rails 网页时,有时数据库查询需要 2-4 倍的时间。我已经为 Moped 启用了调试日志记录,并看到了这样的行:
D, [2013-11-21T23:11:10.889418 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY database=db collection=col selector={"emailCount"=>{"$gt"=>9}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (42.4671ms)
D, [2013-11-21T23:11:13.403011 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY database=db collection=col selector={"emailCount"=>{"$gt"=>9}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (127.4977ms)
编辑查询和标志,这是时间上的差异:
D, [2013-11-21T22:28:16.268263 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (163.0192ms)
D, [2013-11-21T22:28:17.872661 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (89.7279ms)
D, [2013-11-21T22:30:26.645921 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY [...redacted...] (38.0206ms)
D, [2013-11-21T22:37:47.464478 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY [...redacted...] (42.1586ms)
D, [2013-11-21T22:39:53.393541 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (154.6400ms)
D, [2013-11-21T22:41:16.217997 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY [...redacted...] (38.3251ms)
D, [2013-11-21T22:41:29.540417 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (124.6662ms)
D, [2013-11-21T22:41:36.281601 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY [...redacted...] (37.7121ms)
D, [2013-11-21T23:11:07.696951 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY [...redacted...] (41.8334ms)
D, [2013-11-21T23:11:10.889418 #7690] DEBUG -- : MOPED: 127.0.0.1:17203 QUERY [...redacted...] (42.4671ms)
D, [2013-11-21T23:11:13.403011 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (127.4977ms)
D, [2013-11-21T23:11:16.796217 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (166.5795ms)
D, [2013-11-21T23:11:22.210991 #7690] DEBUG -- : MOPED: 54.213.xxx.xx:17203 QUERY [...redacted...] (195.2744ms)
我想知道为什么 Moped 有时使用 IP 127.0.0.1 而有时使用 IP 54.213.xxx.xx?为什么查询 54.213.xxx.xx 需要 2-4 倍的时间?为了生成这些日志条目,我只是刷新了一个网页。
理想情况下,我希望所有查询花费 ~40ms 而不是偶尔花费 ~160ms。
mongoid.yml
如果相关,这是我的:
development:
sessions:
default:
database: db
hosts:
- localhost:17203
- xxx.no-ip.biz:27017
options:
consistency: :eventual
username: user
password: pass
副本集中有两个 mongo。localhost:17203(或 54.213.xxx.xx)上的那个是次要成员。xxx.no-ip-.biz 解析为 174.21.xxx.xxx 所以我知道我的主要成员没有被用于读取(如我所料)。