{"id":1766,"date":"2008-09-04T21:29:28","date_gmt":"2008-09-04T13:29:28","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/?p=1766"},"modified":"2011-04-09T02:05:06","modified_gmt":"2011-04-08T18:05:06","slug":"optimize-activerecord-sql-query","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1766-optimize-activerecord-sql-query","title":{"rendered":"\u6700\u4f73\u5316 ActiveRecord SQL \u67e5\u8a62"},"content":{"rendered":"<p>Update(2008\/9\/5) \u88dc\u5145 named_scope \u4e5f\u53ef\u4ee5\u7528\u5728 :select\uff0c\u611f\u8b1d tsechingho++<\/p>\n<p>\u8981\u770b AciveRecord \u7522\u751f\u7684 SQL \u662f\u4ec0\u9ebc\uff0c\u9664\u4e86\u53ef\u4ee5\u76f4\u63a5 tail -f log\/development.log \u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u5728 script\/console \u7684\u60c5\u6cc1\u4e0b\u900f\u904e\u4ee5\u4e0b\u6307\u4ee4\u76f4\u63a5\u628a log \u76f4\u63a5\u986f\u793a\u51fa\u4f86\uff1a<br \/>\n<code><br \/>\nActiveRecord::Base.logger = Logger.new(STDOUT)<br \/>\nActiveRecord::Base.clear_active_connections!<br \/>\n<\/code><br \/>\n\u5982\u6b64\u4fbf\u53ef\u4ee5\u597d\u597d\u5728 console \u7684\u74b0\u5883\u4e0b\u5be6\u9a57 ActiveRecord \u56c9\u3002<\/p>\n<p>\u6211\u770b\u5230\u95dc\u65bc ActiveRecord SQL \u67e5\u8a62\u6700\u4f73\u5316\u65b9\u5f0f\u6709\u9019\u5e7e\u7a2e\uff1a<\/p>\n<h3>1. \u4f7f\u7528 :select <\/h3>\n<p>\u53ea\u6488\u9700\u8981\u7528\u5230\u7684\u6b04\u4f4d\uff0c\u7279\u5225\u662f\u5982\u679c\u4e0d\u9700\u8981\u7528\u5230\u7684 text \u6216  binary \u6b04\u4f4d\u8acb\u6392\u9664\u3002<br \/>\n<code><br \/>\nEvent.find(:all, :select => \"id, title, description\")<br \/>\n<\/code><\/p>\n<p>\u642d\u914d named_scope \u6211\u5011\u53ef\u4ee5\u628a\u5e38\u7528\u7684 :select \u9810\u5148\u8a2d\u5b9a\u597d\uff0c\u4f8b\u5982\uff1a<br \/>\n<code><br \/>\nclass User < ActiveRecord::Base\n  named_scope :short, :select => \"id, name, email\"<br \/>\nend<\/p>\n<p>User.short.find(:all)<br \/>\n<\/code><\/p>\n<h3>2. \u4f7f\u7528 :include <\/h3>\n<p>\u4f7f\u7528 :include \u907f\u514d N+1 \u6b21 queries \u7684\u554f\u984c\u3002<\/p>\n<p><code><br \/>\n  @events = Event.find(:all, :include => [:group] )<br \/>\n<\/code><\/p>\n<p><code><br \/>\n  @events.each do |e|<br \/>\n      e.group.title<br \/>\n   end<br \/>\n<\/code><\/p>\n<p>\u5982\u679c\u6c92\u6709\u52a0 :include \u628a\u76f8\u95dc\u7684 groups \u4e00\u8d77\u8f09\u5165\uff0c\u5728\u8ff4\u5708\u4e2d\u5c31\u6703\u7522\u751f @event.size \u6b21\u5c0d group \u7684\u500b\u5225 SQL \u67e5\u8a62\uff0c\u6703\u975e\u5e38\u50b7\u3002\u52a0\u4e86 :include \u4e4b\u5f8c\u7e3d\u5171\u53ea\u9700\u67e5\u8a62\u5169\u6b21\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u500b\u6bd4\u8f03\u5c11\u4eba\u77e5\u9053\u7684\u662f\uff0c\u5728\u8a2d\u5b9a Model associations \u6642\uff0c\u5982\u679c\u6709\u5f88\u660e\u986f\u7684\u60c5\u5883\u4e00\u5b9a\u6703\u9806\u9053\u8f09\u5165\u4e8c\u968e association model\uff0c\u53ef\u4ee5\u8a2d\u5b9a :include \u5728 has_many, belongs_to, has_one \u4e0a\u9762\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><code><br \/>\nclass User < ActiveRecord::Base \n has_one :foo, :include => [:bar]<br \/>\nend<br \/>\n<\/code><\/p>\n<p>\u5982\u6b64\u4fbf\u6703\u5728\u8f09\u5165 @user.foo \u7684\u540c\u6642\uff0c\u4e5f\u6703\u63d0\u65e9\u8f09\u5165 @user.foo.bar\u3002<\/p>\n<h3>3. \u8cc7\u6599\u5eab\u7d22\u5f15<\/h3>\n<p>\u91dd\u5c0d foreign key \u8981\u52a0\u4e0a\u8cc7\u6599\u5eab\u7d22\u5f15 index\u3002\u5728 migration \u4e0a\u900f\u904e add_index \u5c31\u53ef\u4ee5\u52a0\u4e0a\u53bb\u4e86\u3002<\/p>\n<h3>4. \u7279\u5b9a\u60c5\u6cc1\u4e0b\u53ef\u7528 :joins \u53d6\u4ee3 :include<\/h3>\n<p>\u5728\u53ea\u9700\u8981\u7528\u5230 :conditions \u800c\u4e0d\u9700\u8981\u8f09\u5165\u8a72 model \u7684\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u7528 :joins \u53d6\u4ee3 :include\u3002<br \/>\n<code><br \/>\n  Group.find(:all, :include => [ :group_memberships ], :conditions => [ \"group_memberships.created_at > ?\", Time.now - 30.days ] )<br \/>\n<\/code><br \/>\n\u56e0\u70ba\u5176\u4e2d\u6703\u8f09\u5165 group_memberships model \u53ea\u662f\u70ba\u4e86\u52a0\u689d\u4ef6\u5f0f\uff0c\u800c\u6c92\u6709\u8981\u6488\u51fa\u88e1\u9762\u7684\u8cc7\u6599\uff0c\u6240\u4ee5\u53ef\u4ee5\u6539\u7528 :joins\uff0c\u9019\u5176\u4e2d\u7684\u5dee\u7570\u4f60\u770b\u7522\u751f\u51fa\u7684SQL\u5c31\u77e5\u9053\u4e86 :)<\/p>\n<p><code><br \/>\n  Group.find(:all, :joins => [ :group_memberships ], :conditions => [ \"group_memberships.created_at > ?\", Time.now - 30.days ] )<br \/>\n<\/code><\/p>\n<h3>5. \u81ea\u5df1\u5beb SQL <\/h3>\n<p>ActiveRecord \u53ef\u4ee5\u76f4\u63a5\u5beb find_by_sql\u3002<\/p>\n<h3>6. denormalization \u9006\u6b63\u898f\u5316<\/h3>\n<p>\u7576\u8cc7\u6599\u975e\u5e38\u591a\uff0c\u53c8\u8981\u5e38\u5e38\u67e5\u8a62\u5176\u4e2d\u8a08\u7b97\u7684\u7d50\u679c\uff0c\u9019\u6642\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u9006\u6b63\u898f\u5316\u7684\u624b\u6cd5\uff0c\u5c07\u8a08\u7b97\u7684\u7d50\u679c\u4e5f\u7576\u505a\u8cc7\u6599\u5b58\u8d77\u4f86\u3002<\/p>\n<p>\u4e00\u500b\u6700\u5e38\u898b\u6700\u57fa\u672c\u7684\u7528\u6cd5\u5c31\u662f\u8a08\u7b97\u7e3d\u6578\u4e86\uff0c\u4f8b\u5982\u4ee5\u4e0b\u7684\u4f8b\u5b50\u53ef\u4ee5\u89e3\u6c7a\u9700\u8981\u5e38\u5e38\u67e5\u8a62 @topic.posts.size \u8a72\u7bc7\u4e3b\u984c\u6709\u591a\u5c11\u6587\u7ae0\u7684\u60c5\u5883\uff1a<\/p>\n<p><code><br \/>\nclass Topic < ActiveRecord::Base\n has_many :posts\nend\n\nclass Posts < ActiveRecord::Base\n   belongs_to :topic, :counter_cache => true<br \/>\nend<br \/>\n<\/code><\/p>\n<p>\u6211\u5011\u6703\u5728 Topic model \u65b0\u589e\u4e00\u500b\u6b04\u4f4d posts_count\uff0c\u7136\u5f8c :counter_cache \u5c31\u6703\u5e6b\u6211\u5011\u5728\u65b0\u589e\u522a\u9664 Post \u6642\uff0c\u81ea\u52d5\u66f4\u65b0\u6240\u5c6c Topic \u7684 posts_count \u6b04\u4f4d\u8cc7\u6599\u3002\u9019\u6642\u53ea\u8981\u6253 @topic.posts.size \u5c31\u6703\u76f4\u63a5\u56de\u50b3 posts_count\uff0c\u800c\u7121\u9700\u6bcf\u6b21\u90fd\u518d\u767c\u4e00\u500b SQL query \u53bb count \u6709\u591a\u5c11\u7b46 posts\u3002<\/p>\n<p>\u5176\u4ed6\u9006\u6b63\u898f\u5316\u7684\u65b9\u5f0f\u4e5f\u5305\u62ec\u4e86\u7d71\u8a08\u7d50\u7b97\u5831\u8868\u7b49\uff0c\u6211\u5011\u628a\u9700\u8981\u8907\u96dc\u8a08\u7b97\u7684\u7684\u7d50\u679c(\u53ef\u4ee5\u900f\u904e\u975e\u540c\u6b65\u7684\u6a5f\u5236\u5982 cron \u5b9a\u671f\u53bb\u89f8\u767c) \u5b58\u5230 Report model\uff0c\u9019\u6a23\u4f7f\u7528\u8005\u76f4\u63a5\u6488 Report \u7684\u7d50\u679c\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update(2008\/9\/5) \u88dc\u5145 named_scope \u4e5f\u53ef\u4ee5\u7528\u5728 :select\uff0c\u611f\u8b1d tsechi &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1766-optimize-activerecord-sql-query\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u6700\u4f73\u5316 ActiveRecord SQL \u67e5\u8a62\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[50,5,53],"tags":[],"class_list":["post-1766","post","type-post","status-publish","format-standard","hentry","category-performance","category-programming","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-su","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=1766"}],"version-history":[{"count":1,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1766\/revisions"}],"predecessor-version":[{"id":5560,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1766\/revisions\/5560"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}