`

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

阅读更多
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
分享到:
评论
1 楼 holin 2008-06-29  
不错。平时没有太注意。一般都在用size

相关推荐

Global site tag (gtag.js) - Google Analytics