首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Rails + Mongo解决URL性能低下问题

如何使用Rails + Mongo解决URL性能低下问题
EN

Stack Overflow用户
提问于 2013-05-09 05:00:59
回答 1查看 632关注 0票数 2

我们可以在伸缩/运维问题上使用建议。

我们有一个简单的移动应用程序,它运行在Rails 3.2.12上,使用MongoMapper而不是ActiveRecord。有一个数据库调用偶尔表现不佳,导致用户写入并抱怨。目前还不清楚原因。由于MongoMapper的原因,我们无法安装Mongo,而且Mongo返回的数据也不多(< 200KB)。在控制器中也没有执行太多的逻辑。

随着更多的用户,这个问题似乎变得更加严重。服务器在VPS上运行,VPS与30个节点共享。托管公司表示,平均I/O利用率为12%,远低于临界阈值。

既然我们不能使用NewRelic,那么解决这个问题的最佳方法是什么呢?

下面是explain的输出

代码语言:javascript
复制
User.collection.find({:username_downcase => 'banana2006'}).explain
=> {"cursor"=>"BtreeCursor username_downcase", 
    "isMultiKey"=>false, 
    "n"=>1, 
    "nscannedObjects"=>1, 
    "nscanned"=>1, 
    "nscannedObjectsAllPlans"=>1, 
    "nscannedAllPlans"=>1, 
    "scanAndOrder"=>false, 
    "indexOnly"=>false, 
    "nYields"=>0, 
    "nChunkSkips"=>0, 
    "millis"=>3, 
    "indexBounds"=>{"username_downcase"=>[["banana2006", "banana2006"]]}, 
    "allPlans"=>[{"cursor"=>"BtreeCursor username_downcase", "n"=>1, "nscannedObjects"=>1, "nscanned"=>1, "indexBounds"=>{"username_downcase"=>[["banana2006", "banana2006"]]}}], "oldPlan"=>{"cursor"=>"BtreeCursor username_downcase", "indexBounds"=>{"username_downcase"=>[["banana2006", "banana2006"]]}}, "server"=>"x.com"}

来自db.serverStatus的输出

代码语言:javascript
复制
> db.serverStatus()
{
        "host" : "mongo.x.com",
        "version" : "2.2.0",
        "process" : "mongod",
        "pid" : 15957,
        "uptime" : 5232267,
        "uptimeMillis" : NumberLong("5232267460"),
        "uptimeEstimate" : 5178261,
        "localTime" : ISODate("2013-05-19T19:32:14.561Z"),
        "locks" : {
                "." : {
                        "timeLockedMicros" : {
                                "R" : NumberLong(131563265),
                                "W" : NumberLong("2824934127")
                        },
                        "timeAcquiringMicros" : {
                                "R" : NumberLong(536751143),
                                "W" : NumberLong(644540368)
                        }
                },
                "admin" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong(11906),
                                "w" : NumberLong(0)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong(424),
                                "w" : NumberLong(0)
                        }
                },
                "local" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong(13829064),
                                "w" : NumberLong(0)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong(96863334),
                                "w" : NumberLong(0)
                        }
                },
                "x-development" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong(22074626),
                                "w" : NumberLong(645528)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong(2876041),
                                "w" : NumberLong(3693)
                        }
                },
                "x-production" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong("39251850394"),
                                "w" : NumberLong(1466862624)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong("17410130690"),
                                "w" : NumberLong(858232658)
                        }
                },
                "z-development" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong(1897461),
                                "w" : NumberLong(0)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong(134836),
                                "w" : NumberLong(0)
                        }
                }
        },
        "globalLock" : {
                "totalTime" : NumberLong("5232267461000"),
                "lockTime" : NumberLong("2824934127"),
                "currentQueue" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                },
                "activeClients" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                }
        },
        "mem" : {
                "bits" : 64,
                "resident" : 87,
                "virtual" : 9071,
                "supported" : true,
                "mapped" : 4207,
                "mappedWithJournal" : 8414
        },
        "connections" : {
                "current" : 3,
                "available" : 9597
        },
        "extra_info" : {
                "note" : "fields vary by platform",
                "heap_usage_bytes" : 198457056,
                "page_faults" : 3176777
        },
        "indexCounters" : {
                "btree" : {
                        "accesses" : 18208995,
                        "hits" : 18208994,
                        "misses" : 0,
                        "resets" : 0,
                        "missRatio" : 0
                }
        },
        "backgroundFlushing" : {
                "flushes" : 87204,
                "total_ms" : 563603,
                "average_ms" : 6.463040686207055,
                "last_ms" : 1,
                "last_finished" : ISODate("2013-05-19T19:31:55.201Z")
        },
        "cursors" : {
                "totalOpen" : 0,
                "clientCursors_size" : 0,
                "timedOut" : 0
        },
        "network" : {
                "bytesIn" : 9286320357,
                "bytesOut" : 148669944094,
                "numRequests" : 5102457
        },
        "opcounters" : {
                "insert" : 0,
                "query" : 3213569,
                "update" : 1989197,
                "delete" : 0,
                "getmore" : 30944,
                "command" : 216139
        },
        "asserts" : {
                "regular" : 0,
                "warning" : 0,
                "msg" : 0,
                "user" : 0,
                "rollovers" : 0
        },
        "writeBacksQueued" : false,
        "dur" : {
                "commits" : 30,
                "journaledMB" : 0.04096,
                "writeToDataFilesMB" : 0.043131,
                "compression" : 0.9447148096039855,
                "commitsInWriteLock" : 0,
                "earlyCommits" : 0,
                "timeMs" : {
                        "dt" : 3069,
                        "prepLogBuffer" : 0,
                        "writeToJournal" : 0,
                        "writeToDataFiles" : 0,
                        "remapPrivateView" : 0
                }
        },
        "recordStats" : {
                "accessesNotInMemory" : 1102532,
                "pageFaultExceptionsThrown" : 657056,
                "admin" : {
                        "accessesNotInMemory" : 0,
                        "pageFaultExceptionsThrown" : 0
                },
                "local" : {
                        "accessesNotInMemory" : 0,
                        "pageFaultExceptionsThrown" : 0
                },
                "x-development" : {
                        "accessesNotInMemory" : 1555,
                        "pageFaultExceptionsThrown" : 1304
                },
                "x-production" : {
                        "accessesNotInMemory" : 1074115,
                        "pageFaultExceptionsThrown" : 639842
                },
                "z-development" : {
                        "accessesNotInMemory" : 0,
                        "pageFaultExceptionsThrown" : 0
                }
        },
        "ok" : 1
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-15 16:06:55

您可以设置MongoDB profiling,尽管默认的100ms应该足够好。

但是,如果我们不知道Mongo查询是什么,我们就不能告诉你如何优化它们。什么是“偶尔表现不佳的一个数据库调用”?

在注释中,您引用了一个使用snapshot选项的长查询。这可能会导致锁争用,所以如果您可以安全地删除快照选项,您可能会看到改进。

我不知道你为什么说你不能使用新遗物。它可能不会为您提供数据库查询的自动计时,但是因为您知道问题出在哪一个,所以可以使用method tracers来隔离问题,并确保它是数据库而不是代码的其他部分。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16450111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档