1

我的 Heroku Rails 加载速度总是非常缓慢,不仅是在第一次启动时。

它不会在生产模式下缓慢加载。

我安装了 New-Relic,我有两个 Dynos 正在运行,我有 PostgreSQL Crane Plan。但是当我启动我的应用程序时,它通常需要大约 30 秒才能加载,并且 60% 的时间它会直接进入Application Error,如果我检查我的日志,我会得到Memory Quota Exceeded. 即使我转到源代码中没有 Ruby 的静态页面,它仍然会加载缓慢。有时我设法连接到我的应用程序,并且在从一个页面导航到另一个页面时通常需要大约 10 秒才能加载。我一直在网上寻找年龄,我发现的所有最常见的答案就是添加一个额外的测功机,但这没有帮助。

我也在独角兽上跑步

这些是来自 movies#show 页面的日志,该页面首先加载然后崩溃

Started GET "/movies/61708" for 81.34.154.155 at 2013-08-29 23:08:23 +0000
2013-08-29T23:08:30.093034+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/?page=7 host=www.websitename.com fwd="81.34.154.155" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0
2013-08-29T23:08:31Z app[postgres.8916]: [BLUE] duration: 6945.946 ms  statement: SELECT "movies".* FROM "movies" 
2013-08-29T23:08:41+00:00 app[heroku-postgres]: source=HEROKU_POSTGRESQL_BLUE measure.current_transaction=2844 measure.db_size=60732536bytes measure.tables=23 measure.active-connections=10 measure.waiting-connections=0 measure.index-cache-hit-rate=1 measure.table-cache-hit-rate=1
2013-08-29T23:08:45.401673+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/movies/63724 host=www.websitename.com fwd="81.34.154.155" dyno=web.1 connect=2ms service=30001ms status=503 bytes=0
2013-08-29T23:08:45.445116+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/movies/63704 host=www.websitename.com fwd="81.34.154.155" dyno=web.2 connect=3ms service=30000ms status=503 bytes=0
2013-08-29T23:08:52.684401+00:00 heroku[web.2]: Process running mem=512M(100.0%)
2013-08-29T23:08:52.684613+00:00 heroku[web.2]: Error R14 (Memory quota exceeded)
2013-08-29T23:08:53.647059+00:00 app[web.1]: E, [2013-08-29T23:08:53.384868 #2] ERROR -- : worker=0 PID:5 timeout (61s > 60s), killing
2013-08-29T23:08:54.375154+00:00 app[web.1]: E, [2013-08-29T23:08:54.374971 #2] ERROR -- : reaped #<Process::Status: pid 5 SIGKILL (signal 9)> worker=0

有谁知道这个的解决方案?

4

1 回答 1

2

You mentioned in the comments you have 60,000 rows in your movies table. From your logs it appears you're doing something like the following:

Movie.all

This will generate an SQL query like what we see in your logs.

SELECT "movies".* FROM "movies"

The memory/storage capacity of Postgres doesn't matter with respect to your issue. Your database could be on a dedicated server with 96G RAM. You'll still end up with the same problem on Heroku's end.

When you do something like Movie.all, you're creating in-memory ruby class instances of Movie for all 60,000 records. This takes time and space.

You're probably also rendering too many (dare I say all?!) of these records to a single webpage at once. This is simply way too much data to render during a single request, whether you're on Heroku or not.

Heroku has a hard 30s time limit for it's request's running time. You're hitting this limit. Try a much smaller subset of data, even just as a test, and see if it solves your issues. I'm betting it will.

Movie.limit(10)
于 2013-08-30T01:02:36.890 回答