有效地执行多个查询在相同的型号 [英] Performing multiple queries on the same model efficiently

查看:97
本文介绍了有效地执行多个查询在相同的型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直转圈圈了几天试图解决一个问题,我也挣扎了过去。本质的理解最好的(或有效的)的方式在模型上执行多个查询,因为我经常发现我的网页的一个问题是加载速度很慢。

I've been going round in circles for a few days trying to solve a problem which I've also struggled with in the past. Essentially its an issue of understanding the best (or an efficient) way to perform multiple queries on a model as I'm regularly finding my pages are very slow to load.

考虑,你有一个名为一切模式的情况。最初执行该发现的一切这符合某些标准

Consider the situation where you have a model called Everything. Initially you perform a query which finds those records in Everything which match certain criteria

@chosenrecords = Everything.where('name LIKE ?', 'What I want').order('price ASC')

我要记住的 @chosenrecords 的内容,我将present他们给用户列表,但是,我也想了解更多 @chosenrecords 的属性,比如

I want to remember the contents of @chosenrecords as I will present them to the user as a list, however, I would also like to understand more of the attributes of @chosenrecords,for instance

@minimumprice = @chosenrecords.first
@numberofrecords = @chosenrecords.count

当我使用上述code在我的控制器和检查本地服务器上的命令历史记录,我很惊讶地发现,这三个查询涉及对原一切SQL查询模式,而不是记忆中的 @chosenrecords 返回的记录,并执行上查询。这似乎是非常低效的我,确实是这三个查询花费的时间是相同的处理,使页面执行缓慢。

When I use the above code in my controller and inspect the command history on the local server, I am surprised to find that each of the three queries involves an SQL query on the original Everything model, rather than remembering the records returned in @chosenrecords and performing the query on that. This seems very inefficient to me and indeed each of the three queries takes the same amount of time to process, making the page perform slowly.

我比较有经验的书面codeS的软件,如MATLAB在这里,一旦你计算的变量是存储在本地,并且可以快速地询问,而不是重新计算该变量你想知道每一次的价值有关它的更多信息。请你能指导我一下,我是否只是在错误的轨道完全,我已经确定的问题仅仅是它是在Rails的怎么样,还是有什么我可以做些什么来改善它。我看着像使用范围,定义不同的变量类型,和缓存的概念,但我不能肯定我在做什么在每种情况下,保持了类似的洞结束了。

I am more experienced in writing codes in software like MATLAB where once you've calculated the value of a variable it is stored locally and can be quickly interrogated, rather than recalculating that variable on each occasion you want to know more information about it. Please could you guide me as to whether I am just on the wrong track completely and the issues I've identified are just "how it is in Rails" or whether there is something I can do to improve it. I've looked into concepts like using a scope, defining a different variable type, and caching, but I'm not quite sure what I'm doing in each case and keep ending up in a similar hole.

感谢您的时间

推荐答案

您是部分在错误的轨道。 Rails 3中带有阿雷尔,其中推迟查询到的数据是必需的。在你的情况,你已经产生阿雷尔查询,但。首先与放大器执行;然后用.Count之间。我在这里做的是运行第一个查询,得到的所有结果在一个数组和工作在接下来的两行的阵列上。

You are partially on the wrong track. Rails 3 comes with Arel, which defer the query until data is required. In your case, you have generated Arel query but executing it with .first & then with .count. What I have done here is run the first query, get all the results in an array and working on that array in next two lines.

执行的查询是这样的: -

Perform the queries like this:-

@chosenrecords = Everything.where('name LIKE ?', 'What I want').order('price ASC').all

@minimumprice = @chosenrecords.first
@numberofrecords = @chosenrecords.size

这将解决您的问题。

It will solve your issue.

这篇关于有效地执行多个查询在相同的型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆