简体   繁体   English

MYSQL 生成我在 Rails 应用程序中没有要求的查询

[英]MYSQL generating queries I didn't ask for in my Rails application

I've been trying to fix my app's performance and after using Datadog to try to trace the problem I found out that my application is generating a bunch of SQL queries that I have no idea where they are coming from.我一直在尝试修复我的应用程序的性能,在使用 Datadog 尝试跟踪问题后,我发现我的应用程序正在生成一堆我不知道它们来自哪里的 SQL 查询。

The top 3 queries being generated in the image below are queries that are not related to PreparationsController#index.下图中生成的前 3 个查询是与 PreparationsController#index 无关的查询。

在此处输入图像描述

When looking at my application log after calling for PreparationsController#index, the only query referring to the ingredients_receipts and ingredients_preparation models are the following (these ones I actually called for them, but as you can see, they are a simple "exists" query):在调用 PreparationsController#index 后查看我的应用程序日志时,唯一涉及到 ingredients_receipts 和 ingredients_preparation 模型的查询如下(这些我实际上是为它们调用的,但正如您所见,它们是一个简单的“存在”查询) : 在此处输入图像描述

Preparation.rb准备工作.rb

在此处输入图像描述

PreparationsController#index准备工作控制器#index

在此处输入图像描述

I've tried tracing it further down with DataDog but it won't go deeper than what the images are showing.我已经尝试使用 DataDog 进一步追踪它,但它不会比图像显示的更深。 I've been pulling my hairs out, any help is appreciated.我一直在拉我的头发,任何帮助表示赞赏。

This just looks like two different log capturing methods producing slightly different results, notice that the third query in the Datadog screenshot has select ingredients.* so the first two that have select ?这看起来就像两种不同的日志捕获方法产生的结果略有不同,请注意 Datadog 屏幕截图中的第三个查询有select ingredients.*所以前两个有select ? are not "normal" queries, so probably that placeholder is filled in with 1 as one just like your Rails log shows.不是“正常”查询,所以可能该占位符填充1 as one就像您的 Rails 日志显示的那样。

Seeing as they both seem to be id specific, that seems likely to be an n+1 problem, and probably simple to resolve with an includes .看到它们似乎都是特定于 id 的,这似乎是一个 n+1 问题,并且可能很容易用includes解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM