这两个陈述有什么区别,为什么要选择它们?

时间:2022-11-10 15:34:47

I'm a beginner at rails. And I've come to understand two different ways to return the same result.

我是铁轨的初学者。我已经了解了两种不同的方法来返回相同的结果。

What is the difference between these two? And what situation would require you to choose one from the other?

这两者有什么区别?什么情况需要你从另一个中选择一个?

Example 1:

例1:

Object.find(:all).select {|c| c.name == "Foobar" }.size

Example 2:

例2:

Object.count(:conditions => ['name = ?', 'Foobar'])

FURTHER NOTE:

进一步说明:

I seriously wish I could vote everyone correct answers for this one. Thank you so much. I just had a serious rails affirmation.

我真的希望我可以投票给每个人这个问题的正确答案。非常感谢。我刚刚有一个严肃的铁路肯定。

4 个解决方案

#1


4  

Object.count always hits the DB, and the find()....size() call can optimize. Good discussion here

Object.count总是命中数据库,find().... size()调用可以优化。这里好好讨论

http://rhnh.net/2007/09/26/counting-activerecord-associations-count-size-or-length

http://rhnh.net/2007/09/26/counting-activerecord-associations-count-size-or-length

#2


4  

Example 1:

例1:

This constructs a query:

这构造了一个查询:

SELECT * FROM objects

then turns all the records into a collection of objects in your memory, then iterates through every object to see if it meets the condition, and then counts the number of elements that meet condition.

然后将所有记录转换为内存中的对象集合,然后遍历每个对象以查看它是否满足条件,然后计算满足条件的元素数量。

Example 2:

例2:

This constructs a query:

这构造了一个查询:

SELECT count(id) FROM objects WHERE name = 'Foobar'

lets sql do all the hard work, and returns just an integer - a number of objects meeting condition.

让sql做所有艰苦的工作,并返回一个整数 - 一些符合条件的对象。

Usually you want no 2 - faster and less memory

通常你不需要2 - 更快,更少的内存

#3


2  

Example 1 will load all of your records from the DB (assuming Object is an ActiveRecord model), then uses Ruby to reduce the set, and then return the size of that array. So this is potentially memory and CPU heavy - not good.

示例1将从DB加载所有记录(假设Object是ActiveRecord模型),然后使用Ruby减少集合,然后返回该数组的大小。所以这可能是内存和CPU重 - 不好。

Example 2 performs the count in SQL, so all the heavy lifting is performed in the database, not in Ruby. Much better :)

示例2在SQL中执行计数,因此所有繁重的工作都在数据库中执行,而不是在Ruby中执行。好多了 :)

#4


2  

In example 1, you are getting all objects from the datastore, and then iterating over all of them, selecting the objects that has the name Foobar. And then getting the size of that array. Example 1 is the clear loser here.

在示例1中,您将从数据存储区获取所有对象,然后迭代所有对象,选择名称为Foobar的对象。然后获得该数组的大小。示例1在这里是明显的输家。

Example 1 sql:

示例1 sql:

select * from whatever
# then iterate over entire array

Example two executes a where clause in SQL to the datastore.

示例二在SQL中执行where子句到数据存储区。

select count(id) from whatever where name = 'foobar'
# The SQL above is sql-server accurate, but not necessarily mysql or sqlite3

#1


4  

Object.count always hits the DB, and the find()....size() call can optimize. Good discussion here

Object.count总是命中数据库,find().... size()调用可以优化。这里好好讨论

http://rhnh.net/2007/09/26/counting-activerecord-associations-count-size-or-length

http://rhnh.net/2007/09/26/counting-activerecord-associations-count-size-or-length

#2


4  

Example 1:

例1:

This constructs a query:

这构造了一个查询:

SELECT * FROM objects

then turns all the records into a collection of objects in your memory, then iterates through every object to see if it meets the condition, and then counts the number of elements that meet condition.

然后将所有记录转换为内存中的对象集合,然后遍历每个对象以查看它是否满足条件,然后计算满足条件的元素数量。

Example 2:

例2:

This constructs a query:

这构造了一个查询:

SELECT count(id) FROM objects WHERE name = 'Foobar'

lets sql do all the hard work, and returns just an integer - a number of objects meeting condition.

让sql做所有艰苦的工作,并返回一个整数 - 一些符合条件的对象。

Usually you want no 2 - faster and less memory

通常你不需要2 - 更快,更少的内存

#3


2  

Example 1 will load all of your records from the DB (assuming Object is an ActiveRecord model), then uses Ruby to reduce the set, and then return the size of that array. So this is potentially memory and CPU heavy - not good.

示例1将从DB加载所有记录(假设Object是ActiveRecord模型),然后使用Ruby减少集合,然后返回该数组的大小。所以这可能是内存和CPU重 - 不好。

Example 2 performs the count in SQL, so all the heavy lifting is performed in the database, not in Ruby. Much better :)

示例2在SQL中执行计数,因此所有繁重的工作都在数据库中执行,而不是在Ruby中执行。好多了 :)

#4


2  

In example 1, you are getting all objects from the datastore, and then iterating over all of them, selecting the objects that has the name Foobar. And then getting the size of that array. Example 1 is the clear loser here.

在示例1中,您将从数据存储区获取所有对象,然后迭代所有对象,选择名称为Foobar的对象。然后获得该数组的大小。示例1在这里是明显的输家。

Example 1 sql:

示例1 sql:

select * from whatever
# then iterate over entire array

Example two executes a where clause in SQL to the datastore.

示例二在SQL中执行where子句到数据存储区。

select count(id) from whatever where name = 'foobar'
# The SQL above is sql-server accurate, but not necessarily mysql or sqlite3