我如何找出所有这些额外的 sqlite3 选择在我的 Rails 应用程序中生成的位置?
我试图弄清楚我的 Rails 应用程序在哪里生成了一大堆额外的查询。我需要一些关于如何解决这个问题的想法。或者,如果有人能给我一些提示,我将不胜感激。
我得到这些:
SQL (1.0ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL (0.8ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL (0.8ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
对数据库的每个请求一遍又一遍地重复(单个请求多达 70 次)
我尝试安装一个跟踪查询源的插件,但它确实没有任何帮助。我正在使用 hobofields gem,不知道这是否是它的作用,但我现在有点喜欢它,
关于寻找这些额外查询的来源有什么建议吗?
I'm trying to figure out where a whole pile of extra queries are being generated by my rails app. I need some ideas on how to tackle it. Or, if someone can give me some hints, I'd be grateful.
I get these:
SQL (1.0ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL (0.8ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL (0.8ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
repeated over and over on every request to the DB (as much as 70 times for a single request)
I tried installing a plugin that traced the source of the queries, but it really didn't help at all. I'm using the hobofields gem, dunno if that is what's doing it but I'm somewhat wedded to it at the moment
Any tips on hunting down the source of these extra queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看第173行connection_adapters/sqlite_adapter.rb中的ActiveRecord gem(我使用的是ActiveRecord 3.0.7),您有一个名为tables的函数,它生成您发布的确切查询:
SELECT name
来自 sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
在开发模式下,针对每个请求,数据库中的每个表都会调用此函数。
在生产中,该函数仅在服务器启动时调用一次,并且结果被缓存。
在针对每个请求的开发过程中,rails 会查看您的数据库以查看每个表具有哪些列,以便它可以在您的模型上生成方法,例如“find_by_name”。由于在生产中数据库不太可能发生更改,rails 仅在服务器启动时进行此查找。
Have a look at ActiveRecord gem in connection_adapters/sqlite_adapter.rb on line 173 (I'm using ActiveRecord 3.0.7) you have a function called tables that generates the exact query you posted:
SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
In development mode this function gets called for every table from your database, on every request.
In production this function gets called only once, when the server starts, and the result is cached.
In development for each request rails looks into your database to see what columns each table has so it can generate methods on your models like "find_by_name". Since in production is unlikely that your database changes, rails does this lookup only on server start.
如果不查看代码,很难说出这一点。
但我确信您在某个循环中编写查询
所以我的建议是检查请求某些方法后调用的所有方法,并查看查询是否在循环中调用。
It's very difficult to tell this without look into the Code.
but i am sure you write your query in a certain loop
So my advice is that to check all the methods which gets called after requesting certain method and look whether the query called in a loop.
当我使用元搜索 gem 进行搜索时,我刚刚在日志中看到了这一点,但仅在开发模式下。
I've just seen this appearing in my logs when I do a search with the metasearch gem but ONLY in development mode.
我相信这是由插件
acts-as-taggable-on
引起的。它将检查表或缓存列是否存在。
I believe that it is caused by the plugin
acts-as-taggable-on
.It will check if the table or the cache column exists.