{"id":9,"date":"2009-03-09T20:34:31","date_gmt":"2009-03-10T01:34:31","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/djcp\/?p=9"},"modified":"2009-03-09T20:34:31","modified_gmt":"2009-03-10T01:34:31","slug":"difference-between-count-and-length-in-rails","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/djcp\/2009\/03\/difference-between-count-and-length-in-rails\/","title":{"rendered":"Difference between &#8220;count&#8221; and &#8220;length&#8221; in Rails."},"content":{"rendered":"<p>I made an interesting discovery the other day between how &#8220;count&#8221; and &#8220;length&#8221; function with an ActiveRecord model, one that can be exploited effectively to speed up an app, as long as you understand the consequences.<\/p>\n<ul>\n<li>Invoking &#8220;count&#8221; on an array (or dependent relationship) will <strong>always<\/strong> hit the database, and do something similar to <strong>select count(id) as count_all from contacts<\/strong> every time you invoke it.<\/li>\n<li>Invoking &#8220;length&#8221; on an array (or dependent relationship) will only hit the database once. If the relationship has already been populated (say by :include-ing the dependent objects in your Model.find method), the database won&#8217;t get hit at all.<\/li>\n<\/ul>\n<p>So &#8211; if you&#8217;re OK not getting a count that&#8217;s 100% accurate at the time of method invocation, &#8220;length&#8221; will do the right thing and run the necessary SQL statement the first time it&#8217;s run. Otherwise, it&#8217;ll give you the array count, which may be out of sync with the database.<\/p>\n<p>This can have significant performance benefits if you&#8217;re iterating through many records and emitting counts of dependent objects. :include-ing the dependent objects and using &#8220;length&#8221; decreased the SQL expense of a page view 40 fold in one case.<\/p>\n<p>Once again, it&#8217;s good to know your tools.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I made an interesting discovery the other day between how &#8220;count&#8221; and &#8220;length&#8221; function with an ActiveRecord model, one that can be exploited effectively to speed up an app, as long as you understand the consequences. Invoking &#8220;count&#8221; on an &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/djcp\/2009\/03\/difference-between-count-and-length-in-rails\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1984,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,4166],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-code","tag-rails"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/users\/1984"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}