2008-06-25

在Rails中关于size,length,count三个方法的区别

关键字: activerocord
count,size,length到底有什么区别呢?通过以下的测试结果可以发现问题:

Post.find_by_id(953125641).comments.length产生了如下的SQL语句
[4;35;1mPost Columns (0.000000) SHOW FIELDS FROM `posts`
Post Load (0.016000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Load (0.016000) SELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) 


Post.find_by_id(953125641).comments.size产生了如下的SQL语句
Post Columns (0.000000) SHOW FIELDS FROM `posts`
Post Load (0.016000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Columns (0.000000) SHOW FIELDS FROM `comments`
SQL (0.015000) SELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) 


Post.find_by_id(953125641).comments.count产生了如下的SQL语句
Post Columns (0.000000) SHOW FIELDS FROM `posts`
Post Load (0.015000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Columns (0.000000) SHOW FIELDS FROM `comments`
SQL (0.016000) SELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) 


@post = Post.find_by_id(953125641)
@length = @post.comments.length产生了如下的SQL语句
Post Columns (0.000000) SHOW FIELDS FROM `posts`
Post Load (0.000000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Load (0.000000) SELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) 


@post = Post.find_by_id(953125641)
@length = @post.comments.size产生了如下的SQL语句
Post Columns (0.015000) SHOW FIELDS FROM `posts`
Post Load (0.016000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Columns (0.000000) SHOW FIELDS FROM `comments`
SQL (0.000000) SELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) 


@post = Post.find_by_id(953125641)
@length = @post.comments.count产生了如下的SQL语句
Post Columns (0.015000) SHOW FIELDS FROM `posts`
Post Load (0.000000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Columns (0.000000) SHOW FIELDS FROM `comments`
SQL (0.000000) SELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) 


@post = Post.find_by_id(953125641)
@comments = @post.comments
@length = @comments.count产生了如下的SQL语句
Post Columns (0.000000) SHOW FIELDS FROM `posts`
Post Load (0.016000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Columns (0.000000) SHOW FIELDS FROM `comments`
SQL (0.000000) SELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) 
@post = Post.find_by_id(953125641)


@comments = @post.comments
@length = @comments.length产生了如下的SQL语句
Post Columns (0.015000) SHOW FIELDS FROM `posts`
Post Load (0.000000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Load (0.000000) SELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) 


@post = Post.find_by_id(953125641)
@comments = @post.comments
@length = @comments.size产生了如下的SQL语句
Post Columns (0.016000) SHOW FIELDS FROM `posts`
Post Load (0.000000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Columns (0.015000) SHOW FIELDS FROM `comments`
SQL (0.000000) SELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) 


@post = Post.find_by_id(953125641)
@comments = @post.comments.find(:all)
@length = @comments.size产生了如下的SQL语句
Post Columns (0.015000) SHOW FIELDS FROM `posts`
Post Load (0.032000) SELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1
Comment Load (0.000000) SELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) 



由以上的测试数据表明: #count方法总是会产生count(*)的SQL语句去查询数据库,而#length方法总是取查询出的集合的个数,它总是不会产生count(*)的查询,#size方法就比较好了,如果目标集合还没有取出来,它会像#count方法一样,产生count(*)的查询,如果记录已经取出来了,它就像#length方法一样,直接读取集合的个数了。

基于以上的分析,结论:虽然以上三种方法都可以取得数据记录的个数,但是还是要根据根据实际所需要,调用相应的方法,以达到优化。


参考资料:
我发的帖子: http://www.railsforum.com/viewtopic.php?id=19593
文章:count vs length vs size http://blog.hasmanythrough.com/2008/2/27/count-length-size
评论
holin 2008-06-29
不错。平时没有太注意。一般都在用size
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

qichunren
  • 浏览: 9305 次
  • 性别: Icon_minigender_1
  • 来自: 与JE一起成长
  • 详细资料
搜索本博客
我的相册
85da3bd0-a62a-3ed8-b828-67195e4ce6d2-thumb
qq.png
共 4 张
存档
最新评论