浏览 3640 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-25
Post.find_by_id(953125641).comments.length产生了如下的SQL语句 [4;35;1mPost Columns (0.000000)[0m [0mSHOW FIELDS FROM `posts`[0m [4;36;1mPost Load (0.016000)[0m [0;1mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;35;1mComment Load (0.016000)[0m [0mSELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) [0m Post.find_by_id(953125641).comments.size产生了如下的SQL语句 [4;36;1mPost Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `posts`[0m [4;35;1mPost Load (0.016000)[0m [0mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;36;1mComment Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `comments`[0m [4;35;1mSQL (0.015000)[0m [0mSELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) [0m Post.find_by_id(953125641).comments.count产生了如下的SQL语句 [4;36;1mPost Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `posts`[0m [4;35;1mPost Load (0.015000)[0m [0mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;36;1mComment Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `comments`[0m [4;35;1mSQL (0.016000)[0m [0mSELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @length = @post.comments.length产生了如下的SQL语句 [4;35;1mPost Columns (0.000000)[0m [0mSHOW FIELDS FROM `posts`[0m [4;36;1mPost Load (0.000000)[0m [0;1mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;35;1mComment Load (0.000000)[0m [0mSELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @length = @post.comments.size产生了如下的SQL语句 [4;36;1mPost Columns (0.015000)[0m [0;1mSHOW FIELDS FROM `posts`[0m [4;35;1mPost Load (0.016000)[0m [0mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;36;1mComment Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `comments`[0m [4;35;1mSQL (0.000000)[0m [0mSELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @length = @post.comments.count产生了如下的SQL语句 [4;36;1mPost Columns (0.015000)[0m [0;1mSHOW FIELDS FROM `posts`[0m [4;35;1mPost Load (0.000000)[0m [0mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;36;1mComment Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `comments`[0m [4;35;1mSQL (0.000000)[0m [0mSELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @comments = @post.comments @length = @comments.count产生了如下的SQL语句 [4;36;1mPost Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `posts`[0m [4;35;1mPost Load (0.016000)[0m [0mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;36;1mComment Columns (0.000000)[0m [0;1mSHOW FIELDS FROM `comments`[0m [4;35;1mSQL (0.000000)[0m [0mSELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @comments = @post.comments @length = @comments.length产生了如下的SQL语句 [4;36;1mPost Columns (0.015000)[0m [0;1mSHOW FIELDS FROM `posts`[0m [4;35;1mPost Load (0.000000)[0m [0mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;36;1mComment Load (0.000000)[0m [0;1mSELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @comments = @post.comments @length = @comments.size产生了如下的SQL语句 [4;35;1mPost Columns (0.016000)[0m [0mSHOW FIELDS FROM `posts`[0m [4;36;1mPost Load (0.000000)[0m [0;1mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;35;1mComment Columns (0.015000)[0m [0mSHOW FIELDS FROM `comments`[0m [4;36;1mSQL (0.000000)[0m [0;1mSELECT count(*) AS count_all FROM `comments` WHERE (`comments`.post_id = 953125641) [0m @post = Post.find_by_id(953125641) @comments = @post.comments.find(:all) @length = @comments.size产生了如下的SQL语句 [4;35;1mPost Columns (0.015000)[0m [0mSHOW FIELDS FROM `posts`[0m [4;36;1mPost Load (0.032000)[0m [0;1mSELECT * FROM `posts` WHERE (`posts`.`id` = 953125641) LIMIT 1[0m [4;35;1mComment Load (0.000000)[0m [0mSELECT * FROM `comments` WHERE (`comments`.post_id = 953125641) [0m 由以上的测试数据表明: #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 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-06-29
不错。平时没有太注意。一般都在用size
|
|
返回顶楼 | |