{"id":53,"date":"2024-09-09T11:39:21","date_gmt":"2024-09-09T02:39:21","guid":{"rendered":"https:\/\/dexall.co.jp\/articles\/?p=53"},"modified":"2025-03-24T08:55:06","modified_gmt":"2025-03-23T23:55:06","slug":"ruby-on-rails%e3%81%aejoin%e3%83%9e%e3%82%b9%e3%82%bf%e3%83%bc%e8%a1%93%ef%bc%9a%e3%83%91%e3%83%95%e3%82%a9%e3%83%bc%e3%83%9e%e3%83%b3%e3%82%b9%e3%82%92%e6%9c%80%e5%a4%a7%e5%8c%96%e3%81%99%e3%82%8b7","status":"publish","type":"post","link":"https:\/\/dexall.co.jp\/articles\/?p=53","title":{"rendered":"Ruby on Rails\u306ejoin\u30de\u30b9\u30bf\u30fc\u8853\uff1a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u5927\u5316\u3059\u308b7\u3064\u306e\u5b9f\u8df5\u30c6\u30af\u30cb\u30c3\u30af"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ruby on Rails\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u306e1\u3064\u3067\u3042\u308bjoin\u30e1\u30bd\u30c3\u30c9\u3002<br>\u9069\u5207\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u52b9\u7387\u7684\u306b\u884c\u3044\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u672c\u8a18\u4e8b\u3067\u306f\u3001join\u306e\u57fa\u790e\u304b\u3089\u9ad8\u5ea6\u306a\u4f7f\u7528\u6cd5\u307e\u3067\u3001\u5b9f\u8df5\u7684\u306a\u4f8b\u3092\u4ea4\u3048\u3066\u8a73\u3057\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\">\u3053\u306e\u8a18\u4e8b\u3092\u901a\u3058\u3066\u7406\u89e3\u3067\u304d\u308b8\u3064\u306e\u3053\u3068<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<ul class=\"wp-block-list\">\n<li>join\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u3068\u5fdc\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7d50\u5408\u306e\u7a2e\u985e\uff08INNER JOIN\u3001LEFT OUTER JOIN\u7b49\uff09\u3068\u305d\u306e\u4f7f\u3044\u5206\u3051<\/li>\n\n\n\n<li>join\u3068includes\u306e\u9055\u3044\u3068\u9069\u5207\u306a\u4f7f\u7528\u30b7\u30fc\u30f3<\/li>\n\n\n\n<li>\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306ejoin\u6d3b\u7528\u65b9\u6cd5<\/li>\n\n\n\n<li>join\u3092\u4f7f\u7528\u3057\u305f\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u624b\u6cd5<\/li>\n\n\n\n<li>\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\uff08EC\u30b5\u30a4\u30c8\u3001SNS\u30a2\u30d7\u30ea\uff09\u3067\u306ejoin\u6d3b\u7528\u4f8b<\/li>\n\n\n\n<li>join\u30af\u30a8\u30ea\u306e\u30c6\u30b9\u30c8\u3068\u9577\u671f\u7684\u306a\u4fdd\u5b88\u306e\u65b9\u6cd5<\/li>\n\n\n\n<li>Ruby on Rails\u3067join\u3092\u4f7f\u3044\u3053\u306a\u3059\u305f\u3081\u306e\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u3068\u5b66\u7fd2\u30ea\u30bd\u30fc\u30b9<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>116<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>121<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>128<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\" data-dialog-title=\"\u76ee\u6b21\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">  <li class=\"first\">    <a href=\"#i-0\">1. Ruby on Rails\u306ejoin\u30e1\u30bd\u30c3\u30c9\uff1a\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">ActiveRecord\u306ejoin\u30e1\u30bd\u30c3\u30c9\u3068\u306f\u4f55\u304b\uff1f\u521d\u5fc3\u8005\u306b\u3082\u308f\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-2\">join\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u7684\u306a\u69cb\u6587\u3068\u4f7f\u3044\u65b9\uff1a\u30b3\u30fc\u30c9\u4f8b\u3067\u5b66\u3076<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-6\">2. \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7d50\u5408\u306e\u7a2e\u985e\u3092\u5fb9\u5e95\u7406\u89e3<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-7\">INNER JOIN\u3001LEFT OUTER JOIN\u3001RIGHT OUTER JOIN\u306e\u9055\u3044\u3068\u4f7f\u3044\u5206\u3051<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-8\">\u8907\u96d1\u306a\u7d50\u5408\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u73fe\u3059\u308bRails\u306e\u30de\u30b8\u30c3\u30af<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-12\">3. join\u3068includes\u306e\u9055\u3044\uff1a\u9069\u5207\u306a\u4f7f\u3044\u5206\u3051\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u5411\u4e0a<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-13\">N+1\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308bincludes\u306e\u7279\u5fb4\u3068\u6d3b\u7528\u30b7\u30fc\u30f3<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-14\">join\u3068includes\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u6700\u9069\u306a\u30af\u30a8\u30ea\u8a2d\u8a08<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-15\">4. \u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306ejoin\u6d3b\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-16\">\u591a\u5bfe\u591a\u95a2\u4fc2\u3092\u52b9\u7387\u7684\u306b\u6271\u3046join\u306e\u5fdc\u7528<\/a>      <\/li>      <li>        <a href=\"#i-17\">\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f7f\u3063\u305f\u9ad8\u5ea6\u306ajoin\u64cd\u4f5c\uff1a\u4e8b\u4f8b\u3068\u30b3\u30fc\u30c9\u89e3\u8aac<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-18\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u53ef\u8aad\u6027\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u8003\u616e\u3057\u305f\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-24\">5. \u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u5927\u5316\u3059\u308bjoin\u306e\u6700\u9069\u5316\u624b\u6cd5<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-25\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u9069\u5207\u306a\u8a2d\u8a08\u3068\u6d3b\u7528\uff1ajoin\u306e\u901f\u5ea6\u3092\u5287\u7684\u306b\u6539\u5584<\/a>      <\/li>      <li>        <a href=\"#i-26\">\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306ejoin\u64cd\u4f5c\uff1a\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306e\u6700\u9069\u5316\u30c6\u30af\u30cb\u30c3\u30af<\/a>      <\/li>      <li>        <a href=\"#i-30\">EXPLAIN\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u305f\u30af\u30a8\u30ea\u5206\u6790\u624b\u6cd5<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-31\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-37\">6. \u5b9f\u8df5\u7684\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\uff1aRails\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306ejoin\u6d3b\u7528\u4f8b<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-38\">EC\u30b5\u30a4\u30c8\u306e\u6ce8\u6587\u60c5\u5831\u53d6\u5f97\uff1a\u8907\u6570\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3059\u308b\u52b9\u7387\u7684\u306a\u30af\u30a8\u30ea\u8a2d\u8a08<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-39\">SNS\u30a2\u30d7\u30ea\u306e\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u751f\u6210\uff1a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8003\u616e\u3057\u305fjoin\u8a2d\u8a08<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-40\">7. join\u3092\u6d3b\u7528\u3057\u305f\u30af\u30a8\u30ea\u306e\u30c6\u30b9\u30c8\u3068\u4fdd\u5b88<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-41\">RSpec\u3092\u4f7f\u3063\u305fjoin\u30af\u30a8\u30ea\u306e\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u624b\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-42\">\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u306e\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\uff1a\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u306e\u5411\u4e0a<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-49\">\u307e\u3068\u3081\uff1a\u52b9\u679c\u7684\u306ajoin\u30af\u30a8\u30ea\u306e\u958b\u767a\u3068\u4fdd\u5b88<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-50\">\u307e\u3068\u3081\uff1aRuby on Rails\u3067join\u3092\u4f7f\u3044\u3053\u306a\u3059\u305f\u3081\u306e\u6b21\u306e\u30b9\u30c6\u30c3\u30d7<\/a>  <\/li><\/ul>\n      <a href=\"#\" class=\"sgb-toc-button js-toc-button\" rel=\"nofollow\" data-open-dialog=\"true\"><i class=\"fa fa-list\"><\/i><span class=\"sgb-toc-button__text\">\u76ee\u6b21\u3078<\/span><\/a>\n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">1. Ruby on Rails\u306ejoin\u30e1\u30bd\u30c3\u30c9\uff1a\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ruby on Rails\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u884c\u3046\u969b\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u5fc5\u8981\u306a\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u306f\u3088\u304f\u3042\u308a\u307e\u3059\u3002<br>\u305d\u3093\u306a\u6642\u306b\u529b\u3092\u767a\u63ee\u3059\u308b\u306e\u304c<code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<br>\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001ActiveRecord\u306e<code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u3001\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-1\">ActiveRecord\u306ejoin\u30e1\u30bd\u30c3\u30c9\u3068\u306f\u4f55\u304b\uff1f\u521d\u5fc3\u8005\u306b\u3082\u308f\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ActiveRecord\u306e<code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<br>\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001SQL\u306eJOIN\u53e5\u3092\u751f\u6210\u3057\u3001\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30c6\u30fc\u30d6\u30eb\u9593\u3067\u30c7\u30fc\u30bf\u3092\u7d50\u5408\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u306e3\u3064\u306e\u4e3b\u306a\u7279\u5fb4<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<ol class=\"wp-block-list\">\n<li>SQL\u306eJOIN\u53e5\u3092\u81ea\u52d5\u751f\u6210<\/li>\n\n\n\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067INNER JOIN\u3092\u4f7f\u7528<\/li>\n\n\n\n<li>\u30e2\u30c7\u30eb\u9593\u306e\u95a2\u9023\u4ed8\u3051\u3092\u901a\u3058\u3066\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u53ef\u80fd<\/li>\n<\/ol>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-2\">join\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u7684\u306a\u69cb\u6587\u3068\u4f7f\u3044\u65b9\uff1a\u30b3\u30fc\u30c9\u4f8b\u3067\u5b66\u3076<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u7684\u306a\u69cb\u6587\u306f\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Model.joins(:associated_model)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3067\u306f\u3001\u5177\u4f53\u7684\u306a\u30b3\u30fc\u30c9\u4f8b\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e2\u30c7\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class User &lt; ApplicationRecord\n  has_many :posts\n  has_many :comments\nend\n\nclass Post &lt; ApplicationRecord\n  belongs_to :user\n  has_many :comments\nend\n\nclass Comment &lt; ApplicationRecord\n  belongs_to :user\n  belongs_to :post\nend<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-3\">\u5358\u4e00\u306e\u95a2\u9023\u4ed8\u3051\u3092\u4f7f\u7528\u3057\u305fjoin<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30e6\u30fc\u30b6\u30fc\u3068\u6295\u7a3f\u3092\u7d50\u5408\nusers_with_posts = User.joins(:posts)\n# \u751f\u6210\u3055\u308c\u308bSQL: SELECT users.* FROM users INNER JOIN posts ON posts.user_id = users.id<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-4\">\u8907\u6570\u306e\u95a2\u9023\u4ed8\u3051\u3092\u4f7f\u7528\u3057\u305fjoin<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30e6\u30fc\u30b6\u30fc\u3068\u6295\u7a3f\u3068\u30b3\u30e1\u30f3\u30c8\u3092\u7d50\u5408\nusers_with_posts_and_comments = User.joins(:posts, :comments)\n# \u751f\u6210\u3055\u308c\u308bSQL: \n# SELECT users.* FROM users \n# INNER JOIN posts ON posts.user_id = users.id \n# INNER JOIN comments ON comments.user_id = users.id<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-5\">\u30cd\u30b9\u30c8\u3055\u308c\u305f\u95a2\u9023\u4ed8\u3051\u3092\u4f7f\u7528\u3057\u305fjoin<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30e6\u30fc\u30b6\u30fc\u3068\u3001\u305d\u306e\u30e6\u30fc\u30b6\u30fc\u306e\u6295\u7a3f\u306b\u5bfe\u3059\u308b\u30b3\u30e1\u30f3\u30c8\u3092\u7d50\u5408\nusers_with_post_comments = User.joins(posts: :comments)\n# \u751f\u6210\u3055\u308c\u308bSQL:\n# SELECT users.* FROM users \n# INNER JOIN posts ON posts.user_id = users.id \n# INNER JOIN comments ON comments.post_id = posts.id<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u4f8b\u304b\u3089\u308f\u304b\u308b\u3088\u3046\u306b\u3001<code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u95a2\u9023\u4ed8\u3051\u3082\u7c21\u5358\u306b\u8868\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5fdc\u7528\u4f8b\u3068\u3057\u3066\u3001\u6761\u4ef6\u4ed8\u304djoin\u3084\u30ab\u30b9\u30bf\u30e0SQL\u6587\u5b57\u5217\u3092\u4f7f\u7528\u3057\u305fjoin\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u6761\u4ef6\u4ed8\u304djoin\nUser.joins(:posts).where(posts: { published: true })\n\n# \u30ab\u30b9\u30bf\u30e0SQL\u6587\u5b57\u5217\u3092\u4f7f\u7528\u3057\u305fjoin\nUser.joins(\"LEFT OUTER JOIN posts ON posts.user_id = users.id\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u975e\u5e38\u306b\u5f37\u529b\u3067\u3059\u304c\u3001\u4f7f\u7528\u3059\u308b\u969b\u306f\u3044\u304f\u3064\u304b\u306e\u6ce8\u610f\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u4f7f\u7528\u6642\u306e3\u3064\u306e\u6ce8\u610f\u70b9<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<ol class=\"wp-block-list\">\n<li>N+1\u554f\u984c\u306b\u6ce8\u610f\u3059\u308b\uff08\u5fc5\u8981\u306b\u5fdc\u3058\u3066<code>includes<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\uff09<\/li>\n\n\n\n<li>\u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\u3092\u9078\u629e\u3057\u3001\u4e0d\u8981\u306a\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3092\u907f\u3051\u308b<\/li>\n\n\n\n<li>\u8907\u96d1\u306ajoin\u306f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u9069\u5207\u306b\u5229\u7528\u3059\u308b<\/li>\n<\/ol>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u3053\u306a\u3059\u3053\u3068\u3067\u3001\u52b9\u7387\u7684\u306a\u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3057\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7d50\u5408\u306e\u7a2e\u985e\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-6\">2. \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7d50\u5408\u306e\u7a2e\u985e\u3092\u5fb9\u5e95\u7406\u89e3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u306b\u304a\u3044\u3066\u3001\u7570\u306a\u308b\u30c6\u30fc\u30d6\u30eb\u9593\u306e\u30c7\u30fc\u30bf\u3092\u7d50\u5408\u3059\u308b\u5fc5\u8981\u304c\u3057\u3070\u3057\u3070\u751f\u3058\u307e\u3059\u3002<br>Ruby on Rails\u3067\u306f\u3001ActiveRecord\u3092\u901a\u3058\u3066\u69d8\u3005\u306a\u7a2e\u985e\u306e\u7d50\u5408\u3092\u7c21\u5358\u306b\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<br>\u3053\u3053\u3067\u306f\u3001\u4e3b\u8981\u306a\u7d50\u5408\u306e\u7a2e\u985e\u3068\u305d\u306e\u7279\u5fb4\u3001\u305d\u3057\u3066Rails\u3067\u306e\u5b9f\u88c5\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-7\">INNER JOIN\u3001LEFT OUTER JOIN\u3001RIGHT OUTER JOIN\u306e\u9055\u3044\u3068\u4f7f\u3044\u5206\u3051<\/h3>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:var(--sgb-pastel-color);color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\">INNER JOIN\uff08\u5185\u90e8\u7d50\u5408\uff09<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u7fa9\uff1a\u4e21\u65b9\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u4e00\u81f4\u3059\u308b\u884c\u306e\u307f\u3092\u8fd4\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u7279\u5fb4\uff1a\u6700\u3082\u4e00\u822c\u7684\u306a\u7d50\u5408\u30bf\u30a4\u30d7\u3067\u3059\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5834\u9762\uff1a\u95a2\u9023\u3059\u308b\u30c7\u30fc\u30bf\u304c\u5fc5\u305a\u5b58\u5728\u3059\u308b\u5834\u5408\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li>Rails\u3067\u306e\u5b9f\u88c5\uff1a<br><code>ruby User.joins(:posts) # SELECT users.* FROM users INNER JOIN posts ON posts.user_id = users.id<\/code><\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:var(--sgb-pastel-color);color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\">LEFT OUTER JOIN\uff08\u5de6\u5916\u90e8\u7d50\u5408\uff09<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u7fa9\uff1a\u5de6\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u3066\u306e\u884c\u3068\u3001\u53f3\u30c6\u30fc\u30d6\u30eb\u306e\u4e00\u81f4\u3059\u308b\u884c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u7279\u5fb4\uff1a\u5de6\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u306f\u5fc5\u305a\u542b\u307e\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5834\u9762\uff1a\u95a2\u9023\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308b\u5834\u5408\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li>Rails\u3067\u306e\u5b9f\u88c5\uff1a<br><code>ruby User.left_outer_joins(:posts) # SELECT users.* FROM users LEFT OUTER JOIN posts ON posts.user_id = users.id<\/code><\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:var(--sgb-pastel-color);color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\">RIGHT OUTER JOIN\uff08\u53f3\u5916\u90e8\u7d50\u5408\uff09<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u7fa9\uff1a\u53f3\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u3066\u306e\u884c\u3068\u3001\u5de6\u30c6\u30fc\u30d6\u30eb\u306e\u4e00\u81f4\u3059\u308b\u884c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u7279\u5fb4\uff1a\u53f3\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u306f\u5fc5\u305a\u542b\u307e\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5834\u9762\uff1aLEFT OUTER JOIN\u306e\u9006\u306e\u30b1\u30fc\u30b9\u3067\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001Rails\u3067\u306f\u76f4\u63a5\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\uff1aRails\u3067 RIGHT OUTER JOIN \u3092\u884c\u3044\u305f\u3044\u5834\u5408\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u9806\u5e8f\u3092\u5165\u308c\u66ff\u3048\u3066 LEFT OUTER JOIN \u3092\u4f7f\u7528\u3059\u308b\u304b\u3001\u30ab\u30b9\u30bf\u30e0SQL\u3092\u66f8\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u7d50\u5408\u30bf\u30a4\u30d7\u3092\u9069\u5207\u306b\u4f7f\u3044\u5206\u3051\u308b\u3053\u3068\u3067\u3001\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u52b9\u7387\u7684\u306b\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<br>\u4f8b\u3048\u3070\u3001\u5168\u3066\u306e\u30e6\u30fc\u30b6\u30fc\u3068\u305d\u306e\u6295\u7a3f\uff08\u6295\u7a3f\u304c\u306a\u3044\u5834\u5408\u3082\u542b\u3080\uff09\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f LEFT OUTER JOIN \u3092\u4f7f\u7528\u3057\u3001\u6295\u7a3f\u306e\u3042\u308b\u30e6\u30fc\u30b6\u30fc\u306e\u307f\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f INNER JOIN \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-8\">\u8907\u96d1\u306a\u7d50\u5408\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u73fe\u3059\u308bRails\u306e\u30de\u30b8\u30c3\u30af<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ruby on Rails\u306f\u3001\u8907\u96d1\u306a\u7d50\u5408\u64cd\u4f5c\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u73fe\u3059\u308b\u305f\u3081\u306e\u591a\u304f\u306e\u300c\u30de\u30b8\u30c3\u30af\u300d\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-9\">\u95a2\u9023\u4ed8\u3051\u3092\u5229\u7528\u3057\u305f\u81ea\u52d5\u7684\u306aJOIN\u751f\u6210<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Rails\u306e\u30e2\u30c7\u30eb\u95a2\u9023\u4ed8\u3051\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306aJOIN\u3092\u7c21\u5358\u306b\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   # \u6295\u7a3f\u3068\u305d\u306e\u30b3\u30e1\u30f3\u30c8\u3001\u3055\u3089\u306b\u30b3\u30e1\u30f3\u30c8\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u3092\u7d50\u5408\n   Post.joins(comments: :user)\n   # SELECT posts.* FROM posts\n   # INNER JOIN comments ON comments.post_id = posts.id\n   # INNER JOIN users ON users.id = comments.user_id<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-10\">\u30e1\u30bd\u30c3\u30c9\u30c1\u30a7\u30fc\u30f3\u306b\u3088\u308b\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306e\u69cb\u7bc9<br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u6570\u306e\u6761\u4ef6\u3084\u7d50\u5408\u3092\u3001\u30e1\u30bd\u30c3\u30c9\u30c1\u30a7\u30fc\u30f3\u3092\u4f7f\u3063\u3066\u76f4\u611f\u7684\u306b\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.joins(:posts)\n       .where(posts: { published: true })\n       .where(\"posts.created_at &gt; ?\", 1.week.ago)\n       .select(\"users.*, COUNT(posts.id) as posts_count\")\n       .group(\"users.id\")<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-11\">Arel\u3092\u4f7f\u7528\u3057\u305f\u9ad8\u5ea6\u306a\u30af\u30a8\u30ea\u69cb\u7bc9<br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u3088\u308a\u8907\u96d1\u306a\u6761\u4ef6\u3084\u7d50\u5408\u304c\u5fc5\u8981\u306a\u5834\u5408\u3001Arel\u3068\u3044\u3046SQL\u30d3\u30eb\u30c0\u30fc\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   users = User.arel_table\n   posts = Post.arel_table\n   User.joins(users.join(posts, Arel::Nodes::OuterJoin)\n              .on(users[:id].eq(posts[:user_id]))\n              .join_sources)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u99c6\u4f7f\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3082\u8aad\u307f\u3084\u3059\u304f\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30b3\u30fc\u30c9\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001<code>joins<\/code>\u3068<code>includes<\/code>\u306e\u9055\u3044\u306b\u3064\u3044\u3066\u6df1\u5800\u308a\u3057\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306e\u89b3\u70b9\u304b\u3089\u305d\u308c\u305e\u308c\u306e\u9069\u5207\u306a\u4f7f\u7528\u65b9\u6cd5\u3092\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-12\">3. join\u3068includes\u306e\u9055\u3044\uff1a\u9069\u5207\u306a\u4f7f\u3044\u5206\u3051\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u5411\u4e0a<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ruby on Rails\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30af\u30a8\u30ea\u3092\u6700\u9069\u5316\u3059\u308b\u969b\u3001<code>joins<\/code>\u3068<code>includes<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u9069\u5207\u306a\u4f7f\u3044\u5206\u3051\u304c\u91cd\u8981\u3067\u3059\u3002<br>\u3053\u308c\u3089\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u4e00\u898b\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u52d5\u4f5c\u3084\u7528\u9014\u306b\u5927\u304d\u306a\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002<br>\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u4e21\u8005\u306e\u9055\u3044\u3092\u7406\u89e3\u3057\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306e\u4f7f\u3044\u5206\u3051\u65b9\u3092\u5b66\u3073\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-13\">N+1\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308bincludes\u306e\u7279\u5fb4\u3068\u6d3b\u7528\u30b7\u30fc\u30f3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u307e\u305a\u3001<code>joins<\/code>\u3068<code>includes<\/code>\u306e\u57fa\u672c\u7684\u306a\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u304b\u3089\u59cb\u3081\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#b4e0fa;color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u52d5\u4f5c\uff1aSQL JOIN\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u53d6\u5f97\uff1a\u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u306f\u53d6\u5f97\u3057\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li>\u30e1\u30e2\u30ea\u4f7f\u7528\uff1a\u6bd4\u8f03\u7684\u5c11\u306a\u3044\u3067\u3059\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u30b7\u30fc\u30f3\uff1a\u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u306e\u6761\u4ef6\u3067\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3059\u308b\u5834\u5408\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#b4e0fa;color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\"><code>includes<\/code>\u30e1\u30bd\u30c3\u30c9<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u52d5\u4f5c\uff1a\u95a2\u9023\u30c7\u30fc\u30bf\u3092\u4e8b\u524d\u306b\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u53d6\u5f97\uff1a\u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3082\u53d6\u5f97\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30e1\u30e2\u30ea\u4f7f\u7528\uff1a<code>joins<\/code>\u3068\u6bd4\u3079\u3066\u591a\u304f\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u30b7\u30fc\u30f3\uff1a\u95a2\u9023\u30c7\u30fc\u30bf\u3092\u8868\u793a\u3059\u308b\u5834\u5408\u306b\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>includes<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u4e3b\u306a\u7279\u5fb4\u306f\u3001N+1\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u308b\u3053\u3068\u3067\u3059\u3002<br>N+1\u554f\u984c\u3068\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30af\u30a8\u30ea\u304c\u4e0d\u5fc5\u8981\u306b\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3055\u308c\u308b\u554f\u984c\u306e\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002<br>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067N+1\u554f\u984c\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># N+1\u554f\u984c\u304c\u767a\u751f\u3059\u308b\u30b3\u30fc\u30c9\nusers = User.all\nusers.each do |user|\n  puts user.posts.count\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u6700\u521d\u306b\u3059\u3079\u3066\u306e\u30e6\u30fc\u30b6\u30fc\u3092\u53d6\u5f97\u3057\u3001\u305d\u306e\u5f8c\u30e6\u30fc\u30b6\u30fc\u3054\u3068\u306b\u6295\u7a3f\u6570\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<br>\u30e6\u30fc\u30b6\u30fc\u6570\u304c\u5897\u3048\u308b\u307b\u3069\u3001\u5b9f\u884c\u3055\u308c\u308b\u30af\u30a8\u30ea\u306e\u6570\u3082\u5897\u52a0\u3057\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u4f4e\u4e0b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>includes<\/code>\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># includes\u3092\u4f7f\u7528\u3057\u3066N+1\u554f\u984c\u3092\u89e3\u6c7a\nusers = User.includes(:posts)\nusers.each do |user|\n  puts user.posts.count\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u3068\u6295\u7a3f\u306e\u30c7\u30fc\u30bf\u30921\u56de\u306e\u30af\u30a8\u30ea\u3067\u53d6\u5f97\u3057\u3001\u30e1\u30e2\u30ea\u4e0a\u306b\u4fdd\u6301\u3057\u307e\u3059\u3002<br>\u3053\u308c\u306b\u3088\u308a\u3001\u30eb\u30fc\u30d7\u5185\u3067\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a2\u30af\u30bb\u30b9\u304c\u4e0d\u8981\u306b\u306a\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-14\">join\u3068includes\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u6700\u9069\u306a\u30af\u30a8\u30ea\u8a2d\u8a08<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>joins<\/code>\u3068<code>includes<\/code>\u306f\u3001\u305d\u308c\u305e\u308c\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u9762\u3067\u4f7f\u7528\u3059\u308b\u3068\u52b9\u679c\u7684\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#b4e0fa;color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\"><code>joins<\/code>\u306e\u9069\u5207\u306a\u4f7f\u7528\u30b7\u30fc\u30f3<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u306e\u6761\u4ef6\u3067\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3059\u308b\u5834\u5408<\/li>\n\n\n\n<li>\u96c6\u8a08\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408<\/li>\n\n\n\n<li>\u95a2\u9023\u30c7\u30fc\u30bf\u3092\u8868\u793a\u3057\u306a\u3044\u5834\u5408<\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#b4e0fa;color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-lightbulb\"><\/i><\/span><div class=\"sng-box-msg__title\"><code>includes<\/code>\u306e\u9069\u5207\u306a\u4f7f\u7528\u30b7\u30fc\u30f3<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u95a2\u9023\u30c7\u30fc\u30bf\u3092\u8868\u793a\u3059\u308b\u5834\u5408<\/li>\n\n\n\n<li>N+1\u554f\u984c\u3092\u56de\u907f\u3057\u305f\u3044\u5834\u5408<\/li>\n\n\n\n<li>\u8907\u6570\u306e\u95a2\u9023\u30c7\u30fc\u30bf\u3092\u4e00\u5ea6\u306b\u53d6\u5f97\u3059\u308b\u5834\u5408<\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001<code>joins<\/code>\u3068<code>includes<\/code>\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u3055\u3089\u306b\u6700\u9069\u306a\u30af\u30a8\u30ea\u3092\u8a2d\u8a08\u3067\u304d\u307e\u3059\u3002<br>\u3053\u306e\u3068\u304d\u3001<code>references<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># joins\u3068includes\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u4f8b\nUser.includes(:posts).references(:posts).where(posts: { published: true })<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30af\u30a8\u30ea\u306f\u3001\u516c\u958b\u3055\u308c\u305f\u6295\u7a3f\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u53d6\u5f97\u3057\u3001\u540c\u6642\u306b\u305d\u308c\u3089\u306e\u30e6\u30fc\u30b6\u30fc\u306e\u6295\u7a3f\u30c7\u30fc\u30bf\u3082\u4e8b\u524d\u306b\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<br>\u3053\u308c\u306b\u3088\u308a\u3001\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3068\u95a2\u9023\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u30921\u56de\u306e\u30af\u30a8\u30ea\u3067\u52b9\u7387\u7684\u306b\u884c\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9069\u5207\u306b<code>joins<\/code>\u3068<code>includes<\/code>\u3092\u4f7f\u3044\u5206\u3051\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3055\u3089\u306b\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306e<code>join<\/code>\u306e\u6d3b\u7528\u30c6\u30af\u30cb\u30c3\u30af\u306b\u3064\u3044\u3066\u6398\u308a\u4e0b\u3052\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-15\">4. \u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306ejoin\u6d3b\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u3067\u306f\u3001\u5358\u7d14\u306a1\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u8d85\u3048\u305f\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u76f4\u9762\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002<br>\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u5bfe\u3057\u3066<code>join<\/code>\u3092\u52b9\u679c\u7684\u306b\u6d3b\u7528\u3059\u308b\u30c6\u30af\u30cb\u30c3\u30af\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-16\">\u591a\u5bfe\u591a\u95a2\u4fc2\u3092\u52b9\u7387\u7684\u306b\u6271\u3046join\u306e\u5fdc\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u5bfe\u591a\u95a2\u4fc2\u306f\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u4ee3\u8868\u7684\u306a\u4f8b\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u3001\u6295\u7a3f\u3001\u30bf\u30b0\u306e\u95a2\u4fc2\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br>\u4e00\u4eba\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u8907\u6570\u306e\u6295\u7a3f\u3092\u6301\u3061\u3001\u5404\u6295\u7a3f\u306b\u8907\u6570\u306e\u30bf\u30b0\u304c\u4ed8\u3051\u3089\u308c\u308b\u5834\u5408\u3001\u3053\u308c\u306f\u591a\u5bfe\u591a\u95a2\u4fc2\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u3088\u3046\u306a\u95a2\u4fc2\u3092\u52b9\u7387\u7684\u306b\u6271\u3046\u306b\u306f\u3001\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u305f<code>has_many :through<\/code>\u95a2\u9023\u4ed8\u3051\u3068<code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u306e\u304c\u52b9\u679c\u7684\u3067\u3059\u3002<br>\u4ee5\u4e0b\u306b\u3001\u7279\u5b9a\u306e\u30bf\u30b0\u304c\u4ed8\u3044\u305f\u6295\u7a3f\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u691c\u7d22\u3059\u308b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class User &lt; ApplicationRecord\n  has_many :posts\n  has_many :tags, through: :posts\nend\n\nclass Post &lt; ApplicationRecord\n  belongs_to :user\n  has_many :taggings\n  has_many :tags, through: :taggings\nend\n\nclass Tag &lt; ApplicationRecord\n  has_many :taggings\n  has_many :posts, through: :taggings\nend\n\nclass Tagging &lt; ApplicationRecord\n  belongs_to :post\n  belongs_to :tag\nend\n\n# \u7279\u5b9a\u306e\u30bf\u30b0\u304c\u4ed8\u3044\u305f\u6295\u7a3f\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u691c\u7d22\nUser.joins(posts: :tags).where(tags: { name: 'Ruby' }).distinct<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u4f8b\u3067\u306f\u3001<code>joins<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3001<code>where<\/code>\u30e1\u30bd\u30c3\u30c9\u3067\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<br><code>distinct<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u91cd\u8907\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u3092\u9664\u5916\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-17\">\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f7f\u3063\u305f\u9ad8\u5ea6\u306ajoin\u64cd\u4f5c\uff1a\u4e8b\u4f8b\u3068\u30b3\u30fc\u30c9\u89e3\u8aac<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u3088\u308a\u8907\u96d1\u306a\u6761\u4ef6\u3084\u96c6\u8a08\u3092\u5fc5\u8981\u3068\u3059\u308b\u5834\u5408\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f7f\u7528\u3057\u305f\u9ad8\u5ea6\u306a<code>join<\/code>\u64cd\u4f5c\u304c\u6709\u52b9\u3067\u3059\u3002\u30b5\u30d6\u30af\u30a8\u30ea\u3068\u306f\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u306e\u4e2d\u306b\u57cb\u3081\u8fbc\u307e\u308c\u305f\u5225\u306e\u30af\u30a8\u30ea\u306e\u3053\u3068\u3067\u3001\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u3067\u5358\u4e00\u306e\u30af\u30a8\u30ea\u3067\u3088\u308a\u8907\u96d1\u306a\u64cd\u4f5c\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u306b\u3001\u904e\u53bb1\u30f6\u6708\u9593\u3067\u6700\u3082\u591a\u304f\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u53d7\u3051\u53d6\u3063\u305f\u6295\u7a3f\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u691c\u7d22\u3059\u308b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">popular_posts = Post.select('posts.user_id, COUNT(comments.id) AS comment_count')\n                    .joins(:comments)\n                    .where('comments.created_at &gt; ?', 1.month.ago)\n                    .group('posts.user_id')\n                    .order('comment_count DESC')\n                    .limit(10)\n\nUser.joins(\"INNER JOIN (#{popular_posts.to_sql}) AS popular_posts ON users.id = popular_posts.user_id\")\n    .select('users.*, popular_posts.comment_count')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30af\u30a8\u30ea\u3067\u306f\u3001\u307e\u305a<code>popular_posts<\/code>\u3068\u3044\u3046\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3057\u3001\u305d\u308c\u3092\u30e1\u30a4\u30f3\u306e\u30e6\u30fc\u30b6\u30fc\u30af\u30a8\u30ea\u306b<code>join<\/code>\u3057\u3066\u3044\u307e\u3059\u3002<br><code>to_sql<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30b5\u30d6\u30af\u30a8\u30ea\u3092SQL\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u6761\u4ef6\u3092\u5358\u4e00\u306e\u30af\u30a8\u30ea\u3067\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306a<code>join<\/code>\u30af\u30a8\u30ea\u3092\u69cb\u7bc9\u3059\u308b\u969b\u306f\u3001\u4ee5\u4e0b\u306e\u624b\u9806\u3092\u610f\u8b58\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\"><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u5229\u7528\u6642\u306b\u610f\u8b58\u3059\u3079\u304d\u624b\u9806\u30105STEP\u3011<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<ol class=\"wp-block-list\">\n<li>\u5fc5\u8981\u306a\u30c6\u30fc\u30d6\u30eb\u3068\u7d50\u5408\u6761\u4ef6\u3092\u6d17\u3044\u51fa\u3059<\/li>\n\n\n\n<li><code>join<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u57fa\u672c\u7684\u306a\u7d50\u5408\u3092\u884c\u3046<\/li>\n\n\n\n<li><code>where<\/code>\u30e1\u30bd\u30c3\u30c9\u3067\u7d5e\u308a\u8fbc\u307f\u6761\u4ef6\u3092\u8ffd\u52a0\u3059\u308b<\/li>\n\n\n\n<li><code>select<\/code>\u30e1\u30bd\u30c3\u30c9\u3067\u53d6\u5f97\u3059\u308b\u30ab\u30e9\u30e0\u3092\u6307\u5b9a\u3059\u308b<\/li>\n\n\n\n<li><code>order<\/code>\u30e1\u30bd\u30c3\u30c9\u3067\u4e26\u3073\u66ff\u3048\u3092\u884c\u3046<\/li>\n<\/ol>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-18\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u53ef\u8aad\u6027\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u8003\u616e\u3057\u305f\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306a<code>join<\/code>\u64cd\u4f5c\u3092\u884c\u3046\u969b\u306f\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u53ef\u8aad\u6027\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u53d6\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306b\u3044\u304f\u3064\u304b\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-19\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u9069\u5207\u306a\u8a2d\u5b9a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7d50\u5408\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ab\u30e9\u30e0\u306b\u306f\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4ed8\u4e0e\u3057\u3001\u30af\u30a8\u30ea\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-20\">\u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30c7\u30fc\u30bf\u53d6\u5f97<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>select<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.joins(:posts).select('users.id, users.name, COUNT(posts.id) AS post_count')<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-21\">\u30af\u30a8\u30ea\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u5316<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306f<code>scope<\/code>\u3092\u4f7f\u3063\u3066\u30e2\u30b8\u30e5\u30fc\u30eb\u5316\u3057\u3001\u53ef\u8aad\u6027\u3068\u518d\u5229\u7528\u6027\u3092\u9ad8\u3081\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   class User &lt; ApplicationRecord\n     scope :with_recent_posts, -&gt; { joins(:posts).where('posts.created_at &gt; ?', 1.week.ago) }\n   end<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-22\">Eager Loading<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5fc5\u8981\u306b\u5fdc\u3058\u3066<code>includes<\/code>\u3092\u4f75\u7528\u3057\u3001N+1\u554f\u984c\u3092\u56de\u907f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.includes(:posts).joins(:comments).where('comments.created_at &gt; ?', 1.week.ago)<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-23\">\u30af\u30a8\u30ea\u306e\u5206\u5272<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u975e\u5e38\u306b\u8907\u96d1\u306a\u5834\u5408\u306f\u3001\u8907\u6570\u306e\u30af\u30a8\u30ea\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u3082\u691c\u8a0e\u3057\u307e\u3059\u3002\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u5f15\u304d\u63db\u3048\u306b\u53ef\u8aad\u6027\u304c\u5411\u4e0a\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u99c6\u4f7f\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u306b\u5bfe\u3057\u3066\u3082\u52b9\u7387\u7684\u304b\u3064\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30af\u30a8\u30ea\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e<code>join<\/code>\u64cd\u4f5c\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u5927\u5316\u3059\u308b\u305f\u3081\u306e\u6700\u9069\u5316\u624b\u6cd5\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-24\">5. \u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u5927\u5316\u3059\u308bjoin\u306e\u6700\u9069\u5316\u624b\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u6271\u3046\u969b\u3001join\u306e\u9069\u5207\u306a\u4f7f\u7528\u306f\u4e0d\u53ef\u6b20\u3067\u3059\u304c\u3001\u540c\u6642\u306b\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3078\u306e\u5f71\u97ff\u3082\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br>\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001join\u306e\u901f\u5ea6\u3092\u5287\u7684\u306b\u6539\u5584\u3057\u3001\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u3082\u52b9\u7387\u7684\u306b\u52d5\u4f5c\u3055\u305b\u308b\u305f\u3081\u306e\u6700\u9069\u5316\u624b\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-25\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u9069\u5207\u306a\u8a2d\u8a08\u3068\u6d3b\u7528\uff1ajoin\u306e\u901f\u5ea6\u3092\u5287\u7684\u306b\u6539\u5584<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u4e0a\u3067\u975e\u5e38\u306b\u91cd\u8981\u306a\u8981\u7d20\u3067\u3059\u3002<br>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3059\u308b\u7d22\u5f15\u3068\u3057\u3066\u6a5f\u80fd\u3057\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e1\u30ea\u30c3\u30c8\/\u30c7\u30e1\u30ea\u30c3\u30c8\u304c\u5b58\u5728\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#b4e0fa;color:#009EF3\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#009EF3\"><i class=\"far fa-thumbs-up\"><\/i><\/span><div class=\"sng-box-msg__title\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8ffd\u52a0\u306e\u30e1\u30ea\u30c3\u30c8<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ol class=\"wp-block-list\">\n<li>\u691c\u7d22\u901f\u5ea6\u306e\u5411\u4e0a<\/li>\n\n\n\n<li>\u30bd\u30fc\u30c8\u64cd\u4f5c\u306e\u52b9\u7387\u5316<\/li>\n\n\n\n<li>\u4e00\u610f\u6027\u5236\u7d04\u306e\u5b9f\u73fe<\/li>\n<\/ol>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#ffebeb;color:#f88080\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#f88080\"><i class=\"fas fa-exclamation\"><\/i><\/span><div class=\"sng-box-msg__title\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8ffd\u52a0\u306e\u30c7\u30e1\u30ea\u30c3\u30c8<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ol class=\"wp-block-list\">\n<li>\u66f8\u304d\u8fbc\u307f\u64cd\u4f5c\u306e\u901f\u5ea6\u4f4e\u4e0b<\/li>\n\n\n\n<li>\u30c7\u30a3\u30b9\u30af\u5bb9\u91cf\u306e\u5897\u52a0<\/li>\n<\/ol>\n<\/div><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u3057\u305f\u304c\u3063\u3066\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u8a2d\u8a08\u306b\u306f\u6ce8\u610f\u6df1\u3044\u8003\u616e\u304c\u5fc5\u8981\u3067\u3059\u3002<br>\u4ee5\u4e0b\u306b\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8a2d\u8a08\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u3044\u304f\u3064\u304b\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8ffd\u52a0\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u30104STEP\u3011<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<ol class=\"wp-block-list\">\n<li>\u983b\u7e41\u306b\u691c\u7d22\u30fb\u7d50\u5408\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ab\u30e9\u30e0\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f5c\u6210\u3059\u308b<\/li>\n\n\n\n<li>\u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u9069\u5207\u306b\u4f7f\u7528\u3059\u308b<\/li>\n\n\n\n<li>\u30ab\u30fc\u30c7\u30a3\u30ca\u30ea\u30c6\u30a3\uff08\u5217\u306e\u5024\u306e\u7a2e\u985e\uff09\u304c\u9ad8\u3044\u30ab\u30e9\u30e0\u3092\u512a\u5148\u3059\u308b<\/li>\n\n\n\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u6570\u306f\u5fc5\u8981\u6700\u5c0f\u9650\u306b\u6291\u3048\u308b<\/li>\n<\/ol>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">join\u306e\u901f\u5ea6\u3092\u6539\u5584\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u7d50\u5408\u6761\u4ef6\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ab\u30e9\u30e0\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4ed8\u4e0e\u3059\u308b\u3053\u3068\u304c\u52b9\u679c\u7684\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class AddIndexToPostsUserId &lt; ActiveRecord::Migration[6.1]\n  def change\n    add_index :posts, :user_id\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u6570\u306e\u30ab\u30e9\u30e0\u3092\u4f7f\u7528\u3057\u305f\u7d50\u5408\u6761\u4ef6\u306b\u306f\u3001\u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u6709\u52b9\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class AddCompositeIndexToPosts &lt; ActiveRecord::Migration[6.1]\n  def change\n    add_index :posts, [:user_id, :created_at]\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u306b\u3088\u308a\u3001<code>User.joins(:posts).where('posts.created_at &gt; ?', 1.week.ago)<\/code>\u306e\u3088\u3046\u306a\u30af\u30a8\u30ea\u304c\u5927\u5e45\u306b\u9ad8\u901f\u5316\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-26\">\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306ejoin\u64cd\u4f5c\uff1a\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306e\u6700\u9069\u5316\u30c6\u30af\u30cb\u30c3\u30af<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u898f\u6a21\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u6271\u3046\u969b\u306f\u3001\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306e\u6700\u9069\u5316\u304c\u91cd\u8981\u306b\u306a\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306b\u3044\u304f\u3064\u304b\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-27\">\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u5ea6\u306b\u53d6\u5f97\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001<code>limit<\/code>\u3068<code>offset<\/code>\u3092\u4f7f\u7528\u3057\u3066\u30da\u30fc\u30b8\u5358\u4f4d\u3067\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.joins(:posts).limit(50).offset(150)<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-28\">\u30d0\u30c3\u30c1\u51e6\u7406<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>find_each<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30c7\u30fc\u30bf\u3092\u5c0f\u3055\u306a\u584a\u306b\u5206\u3051\u3066\u51e6\u7406\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.joins(:posts).find_each(batch_size: 1000) do |user|\n     # \u51e6\u7406\n   end<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-29\">\u30d1\u30fc\u30c6\u30a3\u30b7\u30e7\u30cb\u30f3\u30b0<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u975e\u5e38\u306b\u5927\u898f\u6a21\u306a\u30c6\u30fc\u30d6\u30eb\u306e\u5834\u5408\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ec\u30d9\u30eb\u3067\u30c6\u30fc\u30d6\u30eb\u3092\u5206\u5272\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u307e\u3059\u3002<br>\u3053\u308c\u306b\u3088\u308a\u3001\u691c\u7d22\u5bfe\u8c61\u306e\u30c7\u30fc\u30bf\u91cf\u3092\u6e1b\u3089\u3057\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>PostgreSQL\u306a\u3069\u306e\u4e00\u90e8\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u30d1\u30fc\u30c6\u30a3\u30b7\u30e7\u30cb\u30f3\u30b0\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u3082\u52b9\u7387\u7684\u306ajoin\u64cd\u4f5c\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-30\">EXPLAIN\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u305f\u30af\u30a8\u30ea\u5206\u6790\u624b\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u9069\u5316\u3059\u308b\u4e0a\u3067\u3001EXPLAIN\u30b3\u30de\u30f3\u30c9\u306f\u975e\u5e38\u306b\u5f37\u529b\u306a\u30c4\u30fc\u30eb\u3067\u3059\u3002<br>\u3053\u306e\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u30af\u30a8\u30ea\u3092\u3069\u306e\u3088\u3046\u306b\u5b9f\u884c\u3059\u308b\u304b\u3092\u8a73\u7d30\u306b\u5206\u6790\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Rails\u74b0\u5883\u3067EXPLAIN\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">users = User.joins(:posts).where('posts.created_at &gt; ?', 1.week.ago)\nputs ActiveRecord::Base.connection.explain(users.to_sql)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fa\u529b\u3055\u308c\u308b\u5b9f\u884c\u8a08\u753b\u3092\u5206\u6790\u3059\u308b\u969b\u306f\u3001\u4ee5\u4e0b\u306e\u30dd\u30a4\u30f3\u30c8\u306b\u6ce8\u76ee\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b9f\u884c\u8a08\u753b\u306e\u78ba\u8a8d\uff1a\u30af\u30a8\u30ea\u304c\u3069\u306e\u3088\u3046\u306a\u9806\u5e8f\u3067\u5b9f\u884c\u3055\u308c\u308b\u304b<\/li>\n\n\n\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u4f7f\u7528\u72b6\u6cc1\uff1a\u9069\u5207\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u30c6\u30fc\u30d6\u30eb\u30b9\u30ad\u30e3\u30f3\u306e\u6709\u7121\uff1a\u30d5\u30eb\u30c6\u30fc\u30d6\u30eb\u30b9\u30ad\u30e3\u30f3\u304c\u767a\u751f\u3057\u3066\u3044\u306a\u3044\u304b<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u51fa\u529b\u304c\u5f97\u3089\u308c\u305f\u5834\u5408<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">EXPLAIN for: SELECT \"users\".* FROM \"users\" INNER JOIN \"posts\" ON \"posts\".\"user_id\" = \"users\".\"id\" WHERE (posts.created_at &gt; '2023-09-01 00:00:00')\n                                QUERY PLAN                                \n--------------------------------------------------------------------------\n Hash Join  (cost=33.38..66.89 rows=388 width=16)\n   Hash Cond: (posts.user_id = users.id)\n   -&gt;  Seq Scan on posts  (cost=0.00..28.88 rows=388 width=4)\n         Filter: (created_at &gt; '2023-09-01 00:00:00'::timestamp without time zone)\n   -&gt;  Hash  (cost=14.00..14.00 rows=1000 width=16)\n         -&gt;  Seq Scan on users  (cost=0.00..14.00 rows=1000 width=16)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u51fa\u529b\u304b\u3089\u3001<code>posts<\/code>\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3057\u3066\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u30b9\u30ad\u30e3\u30f3\u304c\u884c\u308f\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<br>\u3053\u306e\u5834\u5408\u3001<code>created_at<\/code>\u30ab\u30e9\u30e0\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u5584\u3067\u304d\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-31\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5f8c\u306b\u3001join\u64cd\u4f5c\u3092\u542b\u3080\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306b\u95a2\u3059\u308b\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u307e\u3068\u3081\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-32\">\u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30c7\u30fc\u30bf\u306e\u307f\u3092\u53d6\u5f97\u3059\u308b<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.joins(:posts).select('users.id, users.name, COUNT(posts.id) as post_count')<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-33\">N+1\u554f\u984c\u3092\u56de\u907f\u3059\u308b<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.includes(:posts).where(posts: { published: true })<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-34\">\u30af\u30a8\u30ea\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u6d3b\u7528\u3059\u308b<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   User.cache do\n     User.joins(:posts).where('posts.created_at &gt; ?', 1.week.ago)\n   end<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-35\">\u9069\u5207\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u6b63\u898f\u5316\u3092\u884c\u3046<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u8907\u30c7\u30fc\u30bf\u3092\u6700\u5c0f\u9650\u306b\u6291\u3048\u3001\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-36\">\u5b9a\u671f\u7684\u306a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30e2\u30cb\u30bf\u30ea\u30f3\u30b0\u3068\u6700\u9069\u5316\u3092\u884c\u3046<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">New Relic\u3084Scout APM\u306a\u3069\u306e\u30c4\u30fc\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3001\u5b9a\u671f\u7684\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u76e3\u8996\u3059\u308b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u6700\u9069\u5316\u624b\u6cd5\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306ajoin\u64cd\u4f5c\u3092\u542b\u3080\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u77e5\u8b58\u3092\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u3069\u306e\u3088\u3046\u306b\u6d3b\u7528\u3059\u308b\u304b\u3001\u5177\u4f53\u7684\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3092\u901a\u3058\u3066\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-37\">6. \u5b9f\u8df5\u7684\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\uff1aRails\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306ejoin\u6d3b\u7528\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u307e\u3067\u5b66\u3093\u3067\u304d\u305fjoin\u306e\u6280\u8853\u3092\u3001\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u3069\u306e\u3088\u3046\u306b\u6d3b\u7528\u3059\u308b\u304b\u3001\u5177\u4f53\u7684\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3092\u901a\u3058\u3066\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<br>\u3053\u3053\u3067\u306f\u3001EC\u30b5\u30a4\u30c8\u3068SNS\u30a2\u30d7\u30ea\u3068\u3044\u30462\u3064\u306e\u4e00\u822c\u7684\u306a\u30a6\u30a7\u30d6\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f8b\u306b\u3001\u52b9\u7387\u7684\u306a\u30af\u30a8\u30ea\u8a2d\u8a08\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306e\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-38\">EC\u30b5\u30a4\u30c8\u306e\u6ce8\u6587\u60c5\u5831\u53d6\u5f97\uff1a\u8907\u6570\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3059\u308b\u52b9\u7387\u7684\u306a\u30af\u30a8\u30ea\u8a2d\u8a08<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u307e\u305a\u3001EC\u30b5\u30a4\u30c8\u3067\u306e\u6ce8\u6587\u60c5\u5831\u53d6\u5f97\u3092\u4f8b\u306b\u3001\u8907\u6570\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3059\u308b\u52b9\u7387\u7684\u306a\u30af\u30a8\u30ea\u8a2d\u8a08\u3092\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c7\u30fc\u30bf\u30e2\u30c7\u30eb\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User: \u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>Order: \u6ce8\u6587\u60c5\u5831\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>OrderItem: \u6ce8\u6587\u5185\u306e\u500b\u5225\u5546\u54c1\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>Product: \u5546\u54c1\u60c5\u5831\u3092\u7ba1\u7406<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30e2\u30c7\u30eb\u9593\u306e\u95a2\u9023\u4ed8\u3051\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class User &lt; ApplicationRecord\n  has_many :orders\nend\n\nclass Order &lt; ApplicationRecord\n  belongs_to :user\n  has_many :order_items\n  has_many :products, through: :order_items\nend\n\nclass OrderItem &lt; ApplicationRecord\n  belongs_to :order\n  belongs_to :product\nend\n\nclass Product &lt; ApplicationRecord\n  has_many :order_items\n  has_many :orders, through: :order_items\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u3053\u3067\u3001\u904e\u53bb30\u65e5\u9593\u306e\u6ce8\u6587\u60c5\u5831\u3092\u3001\u30e6\u30fc\u30b6\u30fc\u540d\u3001\u6ce8\u6587\u65e5\u3001\u5408\u8a08\u91d1\u984d\u3001\u8cfc\u5165\u5546\u54c1\u6570\u3068\u3068\u3082\u306b\u53d6\u5f97\u3059\u308b\u30af\u30a8\u30ea\u3092\u8a2d\u8a08\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5fc5\u8981\u306a\u60c5\u5831\u306e\u6d17\u3044\u51fa\u3057<\/li>\n\n\n\n<li>\u30c6\u30fc\u30d6\u30eb\u9593\u306e\u95a2\u9023\u4ed8\u3051\u306e\u78ba\u8a8d<\/li>\n\n\n\n<li>join\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u7d50\u5408<\/li>\n\n\n\n<li>where\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u7d5e\u308a\u8fbc\u307f<\/li>\n\n\n\n<li>select\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u53d6\u5f97\u30ab\u30e9\u30e0\u306e\u6307\u5b9a<\/li>\n\n\n\n<li>group_by\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u96c6\u7d04<\/li>\n\n\n\n<li>order\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u4e26\u3073\u66ff\u3048<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u3001\u30af\u30a8\u30ea\u3092\u6bb5\u968e\u7684\u306b\u69cb\u7bc9\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">recent_orders = Order.joins(:user, :order_items)\n  .where('orders.created_at &gt; ?', 30.days.ago)\n  .select('orders.id, users.name as user_name, orders.created_at, \n           SUM(order_items.price * order_items.quantity) as total_amount, \n           COUNT(DISTINCT order_items.product_id) as product_count')\n  .group('orders.id, users.name, orders.created_at')\n  .order('orders.created_at DESC')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30af\u30a8\u30ea\u306f\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3001\u5fc5\u8981\u306a\u60c5\u5831\u3092\u52b9\u7387\u7684\u306b\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<br>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u9069\u5207\u306a\u8a2d\u5b9a<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><code>orders.created_at<\/code>\u3001<code>order_items.order_id<\/code>\u3001<code>order_items.product_id<\/code>\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Eager Loading\u306e\u6d3b\u7528<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u95a2\u9023\u3059\u308b\u30c7\u30fc\u30bf\u3092\u30d7\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u3067\u3001N+1\u554f\u984c\u3092\u56de\u907f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   recent_orders = Order.includes(:user, :order_items)\n     .where('orders.created_at &gt; ?', 30.days.ago)\n     # ... (\u4ee5\u4e0b\u3001\u524d\u8ff0\u306e\u30af\u30a8\u30ea\u3068\u540c\u3058)<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u30af\u30a8\u30ea\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u4f7f\u7528<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u983b\u7e41\u306b\u5b9f\u884c\u3055\u308c\u308b\u30af\u30a8\u30ea\u306e\u5834\u5408\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u6d3b\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u6e1b\u3089\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   Rails.cache.fetch('recent_orders', expires_in: 1.hour) do\n     Order.joins(:user, :order_items)\n       # ... (\u30af\u30a8\u30ea\u306e\u7d9a\u304d)\n   end<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u6700\u9069\u5316\u3092\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u5927\u898f\u6a21\u306aEC\u30b5\u30a4\u30c8\u3067\u3082\u52b9\u7387\u7684\u306b\u6ce8\u6587\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-39\">SNS\u30a2\u30d7\u30ea\u306e\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u751f\u6210\uff1a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8003\u616e\u3057\u305fjoin\u8a2d\u8a08<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b21\u306b\u3001SNS\u30a2\u30d7\u30ea\u306e\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u751f\u6210\u3092\u4f8b\u306b\u3001\u3088\u308a\u8907\u96d1\u306ajoin\u64cd\u4f5c\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c7\u30fc\u30bf\u30e2\u30c7\u30eb\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User: \u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>Post: \u6295\u7a3f\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>Like: \u3044\u3044\u306d\u60c5\u5831\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>Comment: \u30b3\u30e1\u30f3\u30c8\u3092\u7ba1\u7406<\/li>\n\n\n\n<li>Relationship: \u30e6\u30fc\u30b6\u30fc\u9593\u306e\u30d5\u30a9\u30ed\u30fc\u95a2\u4fc2\u3092\u7ba1\u7406<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u30e2\u30c7\u30eb\u9593\u306e\u95a2\u9023\u4ed8\u3051\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class User &lt; ApplicationRecord\n  has_many :posts\n  has_many :likes\n  has_many :comments\n  has_many :follower_relationships, foreign_key: :followed_id, class_name: 'Relationship'\n  has_many :followers, through: :follower_relationships, source: :follower\n  has_many :following_relationships, foreign_key: :follower_id, class_name: 'Relationship'\n  has_many :following, through: :following_relationships, source: :followed\nend\n\nclass Post &lt; ApplicationRecord\n  belongs_to :user\n  has_many :likes\n  has_many :comments\nend\n\nclass Like &lt; ApplicationRecord\n  belongs_to :user\n  belongs_to :post\nend\n\nclass Comment &lt; ApplicationRecord\n  belongs_to :user\n  belongs_to :post\nend\n\nclass Relationship &lt; ApplicationRecord\n  belongs_to :follower, class_name: 'User'\n  belongs_to :followed, class_name: 'User'\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u3053\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u30fc\u306e\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u3092\u751f\u6210\u3059\u308b\u30af\u30a8\u30ea\u3092\u8a2d\u8a08\u3057\u307e\u3059\u3002<br>\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u306b\u306f\u3001\u30d5\u30a9\u30ed\u30fc\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u306e\u6700\u65b0\u306e\u6295\u7a3f\u3001\u305d\u308c\u305e\u308c\u306e\u6295\u7a3f\u306b\u5bfe\u3059\u308b\u3044\u3044\u306d\u6570\u3068\u30b3\u30e1\u30f3\u30c8\u6570\u3092\u8868\u793a\u3059\u308b\u3082\u306e\u3068\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30af\u30a8\u30ea\u306e\u8a2d\u8a08\u624b\u9806\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30d5\u30a9\u30ed\u30fc\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u306e\u53d6\u5f97<\/li>\n\n\n\n<li>\u30d5\u30a9\u30ed\u30fc\u30e6\u30fc\u30b6\u30fc\u306e\u6295\u7a3f\u53d6\u5f97<\/li>\n\n\n\n<li>\u3044\u3044\u306d\u3068\u30b3\u30e1\u30f3\u30c8\u60c5\u5831\u306e\u53d6\u5f97<\/li>\n\n\n\n<li>\u8907\u6570\u306ejoin\u306e\u7d44\u307f\u5408\u308f\u305b<\/li>\n\n\n\n<li>\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u88c5<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u3001\u30af\u30a8\u30ea\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def timeline(current_user, page = 1)\n  Post.joins(:user)\n      .joins(\"LEFT OUTER JOIN relationships ON relationships.followed_id = users.id\")\n      .joins(\"LEFT OUTER JOIN likes ON likes.post_id = posts.id\")\n      .joins(\"LEFT OUTER JOIN comments ON comments.post_id = posts.id\")\n      .where(\"relationships.follower_id = ? OR posts.user_id = ?\", current_user.id, current_user.id)\n      .select(\"posts.*, users.name as author_name, \n               COUNT(DISTINCT likes.id) as likes_count, \n               COUNT(DISTINCT comments.id) as comments_count\")\n      .group(\"posts.id, users.name\")\n      .order(\"posts.created_at DESC\")\n      .page(page).per(20)\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30af\u30a8\u30ea\u306f\u8907\u96d1\u3067\u3059\u304c\u3001\u4e00\u5ea6\u306e\u64cd\u4f5c\u3067\u5fc5\u8981\u306a\u60c5\u5831\u3092\u5168\u3066\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<br>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u6d3b\u7528<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   add_index :posts, [:user_id, :created_at]\n   add_index :relationships, [:follower_id, :followed_id]\n   add_index :likes, [:post_id, :user_id]\n   add_index :comments, [:post_id, :user_id]<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u30ab\u30a6\u30f3\u30bf\u30fc\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u5229\u7528<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u3044\u3044\u306d\u6570\u3068\u30b3\u30e1\u30f3\u30c8\u6570\u3092\u6295\u7a3f\u30c6\u30fc\u30d6\u30eb\u306b\u30ad\u30e3\u30c3\u30b7\u30e5\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   class Post &lt; ApplicationRecord\n     belongs_to :user\n     has_many :likes, counter_cache: true\n     has_many :comments, counter_cache: true\n   end<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u30d0\u30c3\u30c1\u51e6\u7406\u306e\u5c0e\u5165<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u91cf\u306e\u6295\u7a3f\u3092\u6271\u3046\u5834\u5408\u306f\u3001\u30d0\u30c3\u30c1\u51e6\u7406\u3092\u5c0e\u5165\u3057\u3066\u8ca0\u8377\u3092\u5206\u6563\u3055\u305b\u307e\u3059\u3002<\/p>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Redis\u306a\u3069\u306e\u9ad8\u901f\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u6d3b\u7528<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u30c7\u30fc\u30bf\u3092Redis\u306b\u30ad\u30e3\u30c3\u30b7\u30e5\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u6e1b\u3089\u3057\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u6642\u9593\u3092\u77ed\u7e2e\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def cached_timeline(current_user, page = 1)\n  cache_key = \"user_#{current_user.id}_timeline_page_#{page}\"\n  Rails.cache.fetch(cache_key, expires_in: 15.minutes) do\n    timeline(current_user, page)\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u6700\u9069\u5316\u30c6\u30af\u30cb\u30c3\u30af\u3092\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u5927\u898f\u6a21\u306aSNS\u30a2\u30d7\u30ea\u3067\u3082\u9ad8\u901f\u3067\u52b9\u7387\u7684\u306a\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u751f\u6210\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e21\u65b9\u306e\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u306b\u5171\u901a\u3059\u308b\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u306f\u3001\u5fc5\u8981\u306a\u60c5\u5831\u306e\u307f\u3092\u53d6\u5f97\u3057\u3001\u9069\u5207\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a2d\u5b9a\u3057\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u52b9\u679c\u7684\u306b\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3059\u3002<br>\u307e\u305f\u3001\u30af\u30a8\u30ea\u306e\u8907\u96d1\u3055\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u53d6\u308b\u3053\u3068\u3082\u91cd\u8981\u3067\u3059\u3002<br>\u6642\u306b\u306f\u3001\u8907\u6570\u306e\u5c0f\u3055\u306a\u30af\u30a8\u30ea\u306b\u5206\u5272\u3059\u308b\u3053\u3068\u3067\u3001\u5168\u4f53\u7684\u306a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3059\u308b\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u624b\u6cd5\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u8981\u4ef6\u3068\u898f\u6a21\u306b\u5fdc\u3058\u3066\u6700\u9069\u306a\u8a2d\u8a08\u3092\u884c\u3046\u3053\u3068\u304c\u6c42\u3081\u3089\u308c\u307e\u3059\u3002<br>\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u3092\u30c6\u30b9\u30c8\u3057\u3001\u9577\u671f\u7684\u306b\u4fdd\u5b88\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-40\">7. join\u3092\u6d3b\u7528\u3057\u305f\u30af\u30a8\u30ea\u306e\u30c6\u30b9\u30c8\u3068\u4fdd\u5b88<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u3092\u5b9f\u88c5\u3057\u305f\u5f8c\u3001\u305d\u308c\u3089\u3092\u9069\u5207\u306b\u30c6\u30b9\u30c8\u3057\u3001\u9577\u671f\u7684\u306b\u4fdd\u5b88\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002<br>\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001RSpec\u3092\u4f7f\u7528\u3057\u305fjoin\u30af\u30a8\u30ea\u306e\u30c6\u30b9\u30c8\u65b9\u6cd5\u3068\u3001\u30af\u30a8\u30ea\u306e\u53ef\u8aad\u6027\u30fb\u4fdd\u5b88\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306e\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u624b\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-41\">RSpec\u3092\u4f7f\u3063\u305fjoin\u30af\u30a8\u30ea\u306e\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u624b\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">RSpec\u3092\u4f7f\u7528\u3057\u3066join\u30af\u30a8\u30ea\u3092\u30c6\u30b9\u30c8\u3059\u308b\u969b\u306e\u57fa\u672c\u7684\u306a\u69cb\u9020\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RSpec.describe User, type: :model do\n  describe '.with_recent_posts' do\n    context 'when users have recent posts' do\n      it 'returns users with posts from the last week' do\n        # \u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\n      end\n    end\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u6e96\u5099\u306b\u306f\u3001FactoryBot\u3092\u4f7f\u7528\u3059\u308b\u3068\u52b9\u7387\u7684\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">FactoryBot.define do\n  factory :user do\n    name { Faker::Name.name }\n\n    factory :user_with_posts do\n      transient do\n        posts_count { 5 }\n      end\n\n      after(:create) do |user, evaluator|\n        create_list(:post, evaluator.posts_count, user: user)\n      end\n    end\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u3067\u306f\u3001\u671f\u5f85\u3055\u308c\u308b\u7d50\u679c\u3068\u5b9f\u969b\u306e\u7d50\u679c\u3092\u6bd4\u8f03\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">it 'returns users with posts from the last week' do\n  user_with_recent_posts = create(:user_with_posts)\n  user_with_old_posts = create(:user_with_posts)\n  user_with_old_posts.posts.update_all(created_at: 2.weeks.ago)\n\n  result = User.with_recent_posts\n\n  expect(result).to include(user_with_recent_posts)\n  expect(result).not_to include(user_with_old_posts)\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u3092\u30c6\u30b9\u30c8\u3059\u308b\u969b\u306f\u3001\u4ee5\u4e0b\u306e\u70b9\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u5341\u5206\u306a\u591a\u69d8\u6027\u3092\u78ba\u4fdd\u3059\u308b<\/li>\n\n\n\n<li>\u5883\u754c\u6761\u4ef6\uff08\u4f8b\uff1a\u3061\u3087\u3046\u30697\u65e5\u524d\u306e\u6295\u7a3f\uff09\u3092\u30c6\u30b9\u30c8\u3059\u308b<\/li>\n\n\n\n<li>\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c6\u30b9\u30c8\u3092\u884c\u3046<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-42\">\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u306e\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\uff1a\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u306e\u5411\u4e0a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u306f\u3001\u6642\u9593\u3068\u3068\u3082\u306b\u7406\u89e3\u304c\u96e3\u3057\u304f\u306a\u308a\u3001\u4fdd\u5b88\u304c\u56f0\u96e3\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<br>\u4ee5\u4e0b\u306e\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u624b\u6cd5\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-43\">\u30b9\u30b3\u30fc\u30d7\u306e\u6d3b\u7528<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   class User &lt; ApplicationRecord\n     scope :with_recent_posts, -&gt; { joins(:posts).where('posts.created_at &gt; ?', 1.week.ago).distinct }\n   end<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-44\">\u30af\u30a8\u30ea\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5c0e\u5165<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   class RecentPostsQuery\n     def initialize(relation = User.all)\n       @relation = relation\n     end\n\n     def call\n       @relation.joins(:posts)\n                .where('posts.created_at &gt; ?', 1.week.ago)\n                .distinct\n     end\n   end\n\n   # \u4f7f\u7528\u4f8b\n   RecentPostsQuery.new.call<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-45\">\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   def self.with_high_engagement\n     subquery = Post.select('user_id, COUNT(*) as post_count')\n                    .group(:user_id)\n                    .having('COUNT(*) &gt; 10')\n\n     joins(\"INNER JOIN (#{subquery.to_sql}) high_engagement_posts ON users.id = high_engagement_posts.user_id\")\n   end<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u624b\u6cd5\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30fc\u30c9\u306e\u518d\u5229\u7528\u6027\u304c\u5411\u4e0a\u3057\u3001\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3092\u3088\u308a\u7ba1\u7406\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30af\u30a8\u30ea\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u305f\u3081\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\">\u30af\u30a8\u30ea\u306e\u53ef\u8aad\u6027\/\u4fdd\u5b88\u6027\u5411\u4e0a\u306e\u305f\u3081\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u30104STEP\u3011<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<ol class=\"wp-block-list\">\n<li>\u9069\u5207\u306a\u547d\u540d\u898f\u5247\u3092\u4f7f\u7528\u3059\u308b\uff08\u4f8b\uff1a<code>users_with_recent_popular_posts<\/code>\uff09<\/li>\n\n\n\n<li>\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306b\u306f\u8aac\u660e\u7684\u306a\u30b3\u30e1\u30f3\u30c8\u3092\u8ffd\u52a0\u3059\u308b<\/li>\n\n\n\n<li>\u975e\u5e38\u306b\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306f\u5c0f\u3055\u306a\u90e8\u5206\u306b\u5206\u5272\u3059\u308b<\/li>\n\n\n\n<li>\u5b9a\u671f\u7684\u306b\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u3092\u5b9f\u65bd\u3057\u3001\u30c1\u30fc\u30e0\u5168\u4f53\u3067\u30af\u30a8\u30ea\u306e\u54c1\u8cea\u3092\u7dad\u6301\u3059\u308b<\/li>\n<\/ol>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u3055\u3089\u306b\u3001\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u7d99\u7d9a\u7684\u306b\u76e3\u8996\u3057\u3001\u6700\u9069\u5316\u3059\u308b\u3053\u3068\u3082\u91cd\u8981\u3067\u3059\u3002<br>\u4ee5\u4e0b\u306e\u30c4\u30fc\u30eb\u3068\u624b\u6cd5\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c6\u30b9\u30c8\u3092\u52b9\u679c\u7684\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-46\">benchmark-ips gem \u306e\u4f7f\u7528<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7570\u306a\u308b\u30af\u30a8\u30ea\u5b9f\u88c5\u306e\u6027\u80fd\u3092\u6bd4\u8f03\u3059\u308b\u306e\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   require 'benchmark\/ips'\n\n   Benchmark.ips do |x|\n     x.report(\"Original Query\") { User.with_recent_posts.to_a }\n     x.report(\"Optimized Query\") { User.optimized_with_recent_posts.to_a }\n     x.compare!\n   end<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-47\">bullet gem \u306e\u6d3b\u7528<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">N+1\u30af\u30a8\u30ea\u554f\u984c\u3092\u691c\u51fa\u3057\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u63a8\u5968\u3092\u884c\u3044\u307e\u3059\u3002<br>\u30c6\u30b9\u30c8\u74b0\u5883\u3084\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u306b\u7d44\u307f\u8fbc\u3080\u3053\u3068\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u554f\u984c\u3092\u65e9\u671f\u306b\u767a\u898b\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   # config\/environments\/test.rb\n   config.after_initialize do\n     Bullet.enable = true\n     Bullet.bullet_logger = true\n     Bullet.raise = true # N+1\u30af\u30a8\u30ea\u304c\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u306b\u4f8b\u5916\u3092\u767a\u751f\u3055\u305b\u308b\n   end<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-48\">\u30e1\u30c8\u30ea\u30af\u30b9\u306e\u6e2c\u5b9a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u884c\u6642\u9593\u3001\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30af\u30a8\u30ea\u6570\u306a\u3069\u306e\u91cd\u8981\u306a\u30e1\u30c8\u30ea\u30af\u30b9\u3092\u6e2c\u5b9a\u3057\u3001\u6700\u9069\u5316\u306e\u52b9\u679c\u3092\u5b9a\u91cf\u7684\u306b\u8a55\u4fa1\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">   require 'memory_profiler'\n\n   report = MemoryProfiler.report do\n     User.with_recent_posts.to_a\n   end\n\n   report.pretty_print<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30c6\u30b9\u30c8\u3068\u6700\u9069\u5316\u306e\u624b\u6cd5\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001join\u30af\u30a8\u30ea\u306e\u54c1\u8cea\u3068\u6027\u80fd\u3092\u9577\u671f\u7684\u306b\u7dad\u6301\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-49\">\u307e\u3068\u3081\uff1a\u52b9\u679c\u7684\u306ajoin\u30af\u30a8\u30ea\u306e\u958b\u767a\u3068\u4fdd\u5b88<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u306e\u958b\u767a\u3068\u4fdd\u5b88\u306f\u3001\u4ee5\u4e0b\u306e\u30dd\u30a4\u30f3\u30c8\u3092\u62bc\u3055\u3048\u308b\u3053\u3068\u3067\u52b9\u679c\u7684\u306b\u884c\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9069\u5207\u306a\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u4f5c\u6210\n<ul class=\"wp-block-list\">\n<li>\u591a\u69d8\u306a\u30c7\u30fc\u30bf\u30d1\u30bf\u30fc\u30f3\u3092\u30ab\u30d0\u30fc<\/li>\n\n\n\n<li>\u5883\u754c\u6761\u4ef6\u306e\u30c6\u30b9\u30c8<\/li>\n\n\n\n<li>\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c6\u30b9\u30c8<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u306e\u5411\u4e0a\n<ul class=\"wp-block-list\">\n<li>\u30b9\u30b3\u30fc\u30d7\u3084\u30af\u30a8\u30ea\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u6d3b\u7528<\/li>\n\n\n\n<li>\u9069\u5207\u306a\u547d\u540d\u3068\u30b3\u30e1\u30f3\u30c8\u306e\u8ffd\u52a0<\/li>\n\n\n\n<li>\u8907\u96d1\u306a\u30af\u30a8\u30ea\u306e\u5206\u5272<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u7d99\u7d9a\u7684\u306a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\n<ul class=\"wp-block-list\">\n<li>\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u30c4\u30fc\u30eb\u306e\u4f7f\u7528<\/li>\n\n\n\n<li>N+1\u30af\u30a8\u30ea\u554f\u984c\u306e\u691c\u51fa<\/li>\n\n\n\n<li>\u91cd\u8981\u306a\u30e1\u30c8\u30ea\u30af\u30b9\u306e\u5b9a\u671f\u7684\u306a\u6e2c\u5b9a<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u30c1\u30fc\u30e0\u5168\u4f53\u3067\u306e\u54c1\u8cea\u7dad\u6301\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u671f\u7684\u306a\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u306e\u5b9f\u65bd<\/li>\n\n\n\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c6\u30b9\u30c8\u306e\u81ea\u52d5\u5316<\/li>\n\n\n\n<li>\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306e\u5171\u6709\u3068\u66f4\u65b0<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u5b9f\u8df5\u306b\u3088\u308a\u3001Ruby on Rails\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u304a\u3051\u308b\u8907\u96d1\u306ajoin\u30af\u30a8\u30ea\u306e\u958b\u767a\u3068\u4fdd\u5b88\u304c\u3001\u3088\u308a\u52b9\u7387\u7684\u304b\u3064\u52b9\u679c\u7684\u306b\u884c\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<br>\u5e38\u306b\u5909\u5316\u3059\u308b\u30c7\u30fc\u30bf\u69cb\u9020\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u8981\u4ef6\u306b\u9069\u5fdc\u3057\u3064\u3064\u3001\u30b3\u30fc\u30c9\u306e\u54c1\u8cea\u3092\u7dad\u6301\u3059\u308b\u3053\u3068\u304c\u3001\u9577\u671f\u7684\u306a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u6210\u529f\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-50\">\u307e\u3068\u3081\uff1aRuby on Rails\u3067join\u3092\u4f7f\u3044\u3053\u306a\u3059\u305f\u3081\u306e\u6b21\u306e\u30b9\u30c6\u30c3\u30d7<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u8a18\u4e8b\u3067\u306f\u3001Ruby on Rails\u306b\u304a\u3051\u308b<code>join<\/code>\u306e\u4f7f\u7528\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067\u5e45\u5e83\u304f\u89e3\u8aac\u3057\u3066\u304d\u307e\u3057\u305f\u3002<br>\u4e3b\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3068\u3057\u3066\u3001join\u30e1\u30bd\u30c3\u30c9\u306e\u57fa\u672c\u3068\u5fdc\u7528\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7d50\u5408\u306e\u7a2e\u985e\u3068\u4f7f\u3044\u5206\u3051\u3001join\u3068includes\u306e\u9069\u5207\u306a\u4f7f\u7528\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u306e\u6d3b\u7528\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u624b\u6cd5\u3001\u5b9f\u8df5\u7684\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3001\u305d\u3057\u3066\u30c6\u30b9\u30c8\u3068\u4fdd\u5b88\u306e\u91cd\u8981\u6027\u306b\u3064\u3044\u3066\u5b66\u3073\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u77e5\u8b58\u3092\u66f4\u306b\u6df1\u3081\u3001\u5b9f\u8df5\u306b\u6d3b\u304b\u3059\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u7d99\u7d9a\u7684\u306a\u5b66\u7fd2\u30ea\u30bd\u30fc\u30b9\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/guides.rubyonrails.org\/\">Rails\u516c\u5f0f\u30ac\u30a4\u30c9<\/a>\uff1a\u5e38\u306b\u6700\u65b0\u306e\u60c5\u5831\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li><a href=\"http:\/\/railscasts.com\/\">RailsCasts<\/a>\uff1a\u5b9f\u8df5\u7684\u306a\u30c6\u30af\u30cb\u30c3\u30af\u3092\u52d5\u753b\u3067\u5b66\u3079\u307e\u3059\u3002<\/li>\n\n\n\n<li><a href=\"https:\/\/railstutorial.jp\/\">Ruby on Rails \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb<\/a>\uff1a\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067\u4f53\u7cfb\u7684\u306b\u5b66\u7fd2\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u3055\u3089\u306a\u308b\u5b9f\u8df5\u306e\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u30a8\u30af\u30b5\u30b5\u30a4\u30ba\u306b\u6311\u6226\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-message\"><div class=\"memo sng-shadow-0\" style=\"border-radius:0px;background-color:#fff9e6;color:#ffb36b\"><div class=\"memo_ttl dfont\"><span class=\"sng-box-msg__icon\" style=\"background:#ffb36b\"><i class=\"fas fa-pencil-alt\"><\/i><\/span><div class=\"sng-box-msg__title\">\u304a\u3059\u3059\u3081\u306e\u7c21\u5358\u306a\u30a8\u30af\u30b5\u30b5\u30a4\u30ba\u3068\u306f\u2026\uff1f<\/div><\/div><div class=\"sng-box-msg__contents\">\n<ul class=\"wp-block-list\">\n<li>\u8907\u96d1\u306a\u591a\u5bfe\u591a\u95a2\u4fc2\u3092\u6301\u3064\u30d6\u30ed\u30b0\u30b7\u30b9\u30c6\u30e0\u306e\u69cb\u7bc9<\/li>\n\n\n\n<li>\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u30d7\u30ed\u30b8\u30a7\u30af\u30c8<\/li>\n\n\n\n<li>\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u30c7\u30fc\u30bf\u66f4\u65b0\u3092\u542b\u3080\u30c0\u30c3\u30b7\u30e5\u30dc\u30fc\u30c9\u306e\u4f5c\u6210<\/li>\n<\/ul>\n<\/div><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5f8c\u306b\u3001ActiveRecord\u3068join\u306e\u5c06\u6765\u5c55\u671b\u3068\u3057\u3066\u3001NoSQL\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u7d71\u5408\u3001GraphQL\u306e\u30cd\u30a4\u30c6\u30a3\u30d6\u30b5\u30dd\u30fc\u30c8\u3001\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u3068\u306e\u9023\u643a\u5f37\u5316\u306a\u3069\u304c\u671f\u5f85\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br>\u3053\u308c\u3089\u306e\u65b0\u3057\u3044\u6280\u8853\u3068\u306e\u878d\u5408\u306b\u3088\u308a\u3001Rails\u3067\u306e\u30c7\u30fc\u30bf\u64cd\u4f5c\u306f\u3055\u3089\u306b\u5f37\u529b\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u8a18\u4e8b\u3067\u5b66\u3093\u3060\u5185\u5bb9\u3092\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u9069\u7528\u3057\u3001\u7d99\u7d9a\u7684\u306b\u5b66\u7fd2\u3092\u91cd\u306d\u308b\u3053\u3068\u3067\u3001Ruby on Rails\u3067\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u306e\u30b9\u30ad\u30eb\u3092\u66f4\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3084\u5927\u898f\u6a21\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u3082\u81ea\u4fe1\u3092\u6301\u3063\u3066\u53d6\u308a\u7d44\u3081\u308b\u3001\u771f\u306eRails\u30de\u30b9\u30bf\u30fc\u3092\u76ee\u6307\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby on Rails\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u306e1\u3064\u3067\u3042\u308bjoin\u30e1\u30bd\u30c3\u30c9\u3002\u9069\u5207\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u52b9\u7387\u7684\u306b\u884c\u3044\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u672c\u8a18\u4e8b\u3067\u306f\u3001join\u306e\u57fa\u790e\u304b\u3089\u9ad8 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":3149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,44],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","has-post-thumbnail","category-ruby","category-ruby-rails"],"_links":{"self":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=53"}],"version-history":[{"count":3,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions\/105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/media\/3149"}],"wp:attachment":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}