gpt4 book ai didi

ruby-on-rails - 仅在生产中发生错误时如何调试 Rails 应用程序?

转载 作者:行者123 更新时间:2023-11-29 14:14:57 25 4
gpt4 key购买 nike

所以我的 Rails Web 应用程序托管在 Heroku 上,我从 Heroku 收到 H12 错误,这意味着请求需要超过 30 秒才能执行。我开发时,请求也需要很长时间才能完全运行,但最终可以正常工作。所以我在开发模式下没有收到任何错误。

当我查看日志时,我得到了问题所在的提示:

SubscriptionOrder Load (19.4ms)  SELECT  "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3  [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
SubscriptionOrder Load (0.7ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 AND "subscription_orders"."running" = $3 ORDER BY "subscription_orders"."id" DESC LIMIT $4 [["user_id", 107], ["state", "paid"], ["running", "t"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
(4.4ms) SELECT COUNT(*) FROM "orders" WHERE (user_id = 107 AND (status = '') IS NOT FALSE AND created_at >= '2018-09-17 10:00:38.559259')
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 AND "subscription_orders"."running" = $3 ORDER BY "subscription_orders"."id" DESC LIMIT $4 [["user_id", 107], ["state", "paid"], ["running", "t"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
(0.6ms) SELECT COUNT(*) FROM "orders" WHERE (user_id = 107 AND (status = '') IS NOT FALSE AND created_at >= '2018-09-17 10:00:38')
(1.4ms) SELECT COUNT(*) FROM "free_meals" WHERE (used = 'f' AND user_id = 107)
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
Subscription Load (0.4ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT $2 [["id", 6], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 AND "subscription_orders"."running" = $3 ORDER BY "subscription_orders"."id" DESC LIMIT $4 [["user_id", 107], ["state", "paid"], ["running", "t"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE (0.0ms) SELECT COUNT(*) FROM "orders" WHERE (user_id = 107 AND (status = '') IS NOT FALSE AND created_at >= '2018-09-17 10:00:38.559259')
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 AND "subscription_orders"."running" = $3 ORDER BY "subscription_orders"."id" DESC LIMIT $4 [["user_id", 107], ["state", "paid"], ["running", "t"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE (0.0ms) SELECT COUNT(*) FROM "orders" WHERE (user_id = 107 AND (status = '') IS NOT FALSE AND created_at >= '2018-09-17 10:00:38')
CACHE (0.0ms) SELECT COUNT(*) FROM "free_meals" WHERE (used = 'f' AND user_id = 107)
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE Subscription Load (0.0ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT $2 [["id", 6], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 AND "subscription_orders"."running" = $3 ORDER BY "subscription_orders"."id" DESC LIMIT $4 [["user_id", 107], ["state", "paid"], ["running", "t"], ["LIMIT", 1]]
CACHE SubscriptionOrder Load (0.0ms) SELECT "subscription_orders".* FROM "subscription_orders" WHERE "subscription_orders"."user_id" = $1 AND "subscription_orders"."state" = $2 ORDER BY "subscription_orders"."id" DESC LIMIT $3 [["user_id", 107], ["state", "paid"], ["LIMIT", 1]]
CACHE (0.0ms) SELECT COUNT(*) FROM "orders" WHERE (user_id = 107 AND (status = '') IS NOT FALSE AND created_at >= '2018-09-17 10:00:38.559259')

...等等等等。 SubscriptionOrder 数据库请求运行了 100 次。

问题是,我不知道如何识别生成此文件的代码行。你知道我该怎么做吗?从这些日志中,找到我的代码在哪里。

最佳答案

这看起来像一个 N+1问题。您正在遍历每个用户,并显示他们的订阅订单,但每次都进行 SQL 查询。

这意味着大量的 SQL 查询,这会导致加载时间过长。通常,这意味着您必须告诉 Rails 在单个 SQL 查询中加载连接的模型,而不是在您第一次调用它时。 includes方法会让你做到这一点。

添加 bullet gem到您的应用程序将帮助您发现那些 N+1 问题并解决它们。

关于ruby-on-rails - 仅在生产中发生错误时如何调试 Rails 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52363456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com