{"id":4243,"date":"2016-05-10T10:06:57","date_gmt":"2016-05-10T01:06:57","guid":{"rendered":"http:\/\/www.lancard.com\/blog\/?p=4243"},"modified":"2025-03-12T11:27:31","modified_gmt":"2025-03-12T02:27:31","slug":"%e3%81%8a%e6%b0%97%e6%a5%bd%e3%82%b9%e3%83%ad%e3%83%bc%e3%82%af%e3%82%a8%e3%83%aajoin-group%e7%b7%a8","status":"publish","type":"post","link":"https:\/\/www.lancard.com\/blog\/2016\/05\/10\/%e3%81%8a%e6%b0%97%e6%a5%bd%e3%82%b9%e3%83%ad%e3%83%bc%e3%82%af%e3%82%a8%e3%83%aajoin-group%e7%b7%a8\/","title":{"rendered":"\u304a\u6c17\u697d\u30b9\u30ed\u30fc\u30af\u30a8\u30ea(JOIN &#038; GROUP\u7de8)"},"content":{"rendered":"<p>\u904b\u7528\u4e2d\u306e\u696d\u52d9\u7cfb\u30b7\u30b9\u30c6\u30e0\u304c\u3042\u308b\u3093\u3067\u3059\u304c\u3001\u304a\u5ba2\u69d8\u304b\u3089\u300c\u9045\u3044\u300d\u3068\u3054\u6307\u6458\u3092\u9802\u3044\u305f\u6a5f\u80fd\u3092\u8abf\u3079\u305f\u3068\u3053\u308d\u53f0\u5e33\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3001\u96c6\u8a08\u3057\u3066\u3044\u308b\u30af\u30a8\u30ea\u304c\u975e\u5e38\u306b\u9045\u3044\u4e8b\u304c\u5224\u660e\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u8abf\u67fb\u3057\u3066\u3044\u308b\u74b0\u5883\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306f PostgreSQL 9.3 \u3067\u3059\u3002\u3053\u3093\u306a\u6642\u306f EXPLAIN ANALYZE\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u3061\u306a\u307f\u306bhaisou_daityou\u304c\u53f0\u5e33\u3067\u73fe\u57282850\u4e07\u4ef6\u7a0b\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308a\u65e5\u3005\u5897\u3048\u7d9a\u3051\u3066\u3044\u307e\u3059\u3002<br \/>\nsyouhin_detail\u306f\u30de\u30b9\u30bf\u3067380\u4ef6\u7a0b\u5ea6\u3067\u3059\u3002<\/p>\n<pre lang=\"sql\">\nEXPLAIN ANALYZE \n\nSELECT \n    haisou_daityou.syouhin_detail_id, \n    syouhin_detail.code as syouhin_detail_code,\n    haisou_daityou.haisou_bi,\n    date_part('day'::text, haisou_daityou.haisou_bi)::int as day,\n    sum(haisou_daityou.haisou_suu) AS haisou_sum\n\n  FROM haisou_daityou\n  LEFT OUTER JOIN syouhin_detail ON haisou_daityou.syouhin_detail_id = syouhin_detail.id\n\n  WHERE \n      \"YearMonth\"(haisou_daityou.haisou_bi) = \"YearMonth\"('2016-03-01'::date)\n  \n    GROUP BY \n        haisou_daityou.syouhin_detail_id, \n        syouhin_detail.code, \n        haisou_daityou.haisou_bi\n    \n    ORDER BY\n        syouhin_detail.code, \n        haisou_daityou.haisou_bi \n<\/pre>\n<p>\u7d50\u679c<\/p>\n<pre lang=\"sql\">\nGroupAggregate  (cost=1141834.38..1173281.22 rows=1048228 width=18) (actual time=30736.728..35349.575 rows=9393 loops=1)\n  ->  Sort  (cost=1141834.38..1144454.95 rows=1048228 width=18) (actual time=30735.447..34864.067 rows=1024085 loops=1)\n        Sort Key: syouhin_detail.code, haisou_daityou.haisou_bi, haisou_daityou.syouhin_detail_id\n        Sort Method: external merge  Disk: 30048kB\n        ->  Hash Left Join  (cost=650914.54..994020.09 rows=1048228 width=18) (actual time=1812.137..5063.999 rows=1024085 loops=1)\n              Hash Cond: (haisou_daityou.syouhin_detail_id = syouhin_detail.id)\n              ->  Bitmap Heap Scan on haisou_daityou  (cost=650901.04..979593.45 rows=1048228 width=12) (actual time=1811.261..4238.580 rows=1024085 loops=1)\n                    Recheck Cond: ((((date_part('year'::text, (haisou_bi)::timestamp without time zone))::integer * 100) + (date_part('month'::text, (haisou_bi)::timestamp without time zone))::integer) = 201603)\n                    Rows Removed by Index Recheck: 191609\n                    ->  Bitmap Index Scan on haisou_daityou_tokuisaki_yearmonth_updated  (cost=0.00..650638.98 rows=1048228 width=0) (actual time=1808.060..1808.060 rows=1024085 loops=1)\n                          Index Cond: ((((date_part('year'::text, (haisou_bi)::timestamp without time zone))::integer * 100) + (date_part('month'::text, (haisou_bi)::timestamp without time zone))::integer) = 201603)\n              ->  Hash  (cost=8.78..8.78 rows=378 width=10) (actual time=0.847..0.847 rows=379 loops=1)\n                    Buckets: 1024  Batches: 1  Memory Usage: 17kB\n                    ->  Seq Scan on syouhin_detail  (cost=0.00..8.78 rows=378 width=10) (actual time=0.007..0.697 rows=379 loops=1)\nTotal runtime: 35426.470 ms\n<\/pre>\n<p>\u3046\u308f\u3041\u3002\u304a\u30fc\u305d\u30fc\u3044\u30fc\u3002\u958b\u767a\u74b0\u5883\u306a\u306e\u3067\u672c\u756a\u3068\u6bd4\u3079\u308b\u3068\u975e\u5e38\u306b\u975e\u529b\u3067\u306f\u3042\u308a\u307e\u3059\u304c\u2026<\/p>\n<p>\u7279\u306b<\/p>\n<p>\u300cHash LEFT JOIN  (cost=650914.54..994020.09 ROWS=1048228 width=18) (actual TIME=1812.137..5063.999 ROWS=1024085 loops=1)\u300d<\/p>\n<p>\u304a\u5ba2\u3055\u3093\u304b\u3089\u306e\u300c\u521d\u56de\u304c\u7279\u306b\u9045\u3044\u300d\u3068\u3044\u3046\u8a71\u306b\u3082\u7d0d\u5f97(\u521d\u56de\u306f\u30cf\u30c3\u30b7\u30e5\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u3089\u308c\u307e\u3059\u304c\u4ee5\u964d\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u3055\u308c\u307e\u3059\u306e\u3067)\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u524d\u8a18\u306e\u7d50\u679c\u306f\u521d\u56de\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3084\u3070\u3044\u3002<\/p>\n<p>INDEX\u3092\u8ffd\u52a0\u3057\u3066\u307f\u305f\u308a\u3001\u96c6\u8a08\u3055\u308c\u3066\u3044\u308b\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u3063\u3066\u306e\u518d\u5b9f\u88c5\u3092\u691c\u8a0e\u3057\u305f\u308a(\u5b9f\u969b\u306b\u30af\u30a8\u30ea\u3092\u66f8\u3044\u3066\u307f\u305f\u308a)\u3001WINDOW\u95a2\u6570\u3067\u306e\u518d\u5b9f\u88c5\u3092\u691c\u8a0e\u3057\u305f\u308a\u3057\u307e\u3057\u305f\u304c\u3001\u7d50\u5c40\u3001\u6b21\u306e\u3088\u3046\u306a\u66f8\u304d\u63db\u3048\u3068\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre lang=\"sql\">\nEXPLAIN ANALYZE \n\nSELECT  syouhin_detail.code as syouhin_detail_code, sum_tmp.*\n  FROM\n    (\n    SELECT \n      haisou_daityou.syouhin_detail_id, \n      haisou_daityou.haisou_bi,\n      date_part('day'::text, haisou_daityou.haisou_bi)::int as day,\n      sum(haisou_daityou.haisou_suu) AS haisou_sum\n\n      FROM haisou_daityou\n\n      WHERE \n        haisou_daityou.haisou_bi BETWEEN '2016-03-01'::date AND '2016-03-31'::date\n  \n      GROUP BY \n        haisou_daityou.syouhin_detail_id, \n        haisou_daityou.haisou_bi\n    ) as sum_tmp\n\n  LEFT OUTER JOIN syouhin_detail ON sum_tmp.syouhin_detail_id = syouhin_detail.id\n\n  ORDER BY\n    syouhin_detail.code, \n    sum_tmp.haisou_bi \n<\/pre>\n<p>\u7d50\u679c<\/p>\n<pre lang=\"sql\">\nSort  (cost=87961.52..87988.41 rows=10756 width=26) (actual time=1640.387..1641.442 rows=9393 loops=1)\n  Sort Key: syouhin_detail.code, haisou_daityou.haisou_bi\n  Sort Method: quicksort  Memory: 971kB\n  ->  Hash Left Join  (cost=86797.56..87241.25 rows=10756 width=26) (actual time=1516.623..1529.725 rows=9393 loops=1)\n        Hash Cond: (haisou_daityou.syouhin_detail_id = syouhin_detail.id)\n        ->  HashAggregate  (cost=86784.06..86972.29 rows=10756 width=12) (actual time=1516.362..1524.763 rows=9393 loops=1)\n              ->  Index Scan using haisou_daityou_haisou_bi on haisou_daityou  (cost=0.44..78691.12 rows=1079058 width=12) (actual time=0.028..758.021 rows=1024085 loops=1)\n                    Index Cond: ((haisou_bi >= '2016-03-01'::date) AND (haisou_bi <= '2016-03-31'::date))\n        ->  Hash  (cost=8.78..8.78 rows=378 width=10) (actual time=0.245..0.245 rows=379 loops=1)\n              Buckets: 1024  Batches: 1  Memory Usage: 17kB\n              ->  Seq Scan on syouhin_detail  (cost=0.00..8.78 rows=378 width=10) (actual time=0.005..0.135 rows=379 loops=1)\nTotal runtime: 1642.233 ms\n<\/pre>\n<p>\u96c6\u8a08\u3057\u305f\u5f8c\u306bJOIN\u3059\u308b\u3053\u3068\u306720\u500d\u304f\u3089\u3044\u901f\u304f\u306a\u308a\u307e\u3057\u305f\u3002\u521d\u56de\u3067\u306f\u3082\u3063\u3068\u3082\u3063\u3068\u5dee\u304c\u3067\u308b\u3082\u306e\u3068\u601d\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u96c6\u8a08\u306e\u305f\u3081\u306b\u304a\u6c17\u697d\u306bJOIN\u3057\u305f\u5f8c\u306bGROUP BY\u3057\u305f\u306e\u304c\u60aa\u304b\u3063\u305f\u6a21\u69d8\u3002<\/p>\n<p>\u304a\u6c17\u697d\u306b\u66f8\u3044\u305f\u30af\u30a8\u30ea\u304c\u30b9\u30ed\u30fc\u30af\u30a8\u30ea\u5316\u3057\u3066\u6ce3\u3044\u305f\u304a\u8a71\u3067\u3057\u305f\u3002\u300cJOIN &amp; GROUP\u7de8\u300d\u3068\u306f\u3057\u305f\u304c\u7d9a\u7de8\u306f\u306a\u3044\uff01(\u3068\u3044\u3044\u306a)<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F4243&#038;t=%E3%81%8A%E6%B0%97%E6%A5%BD%E3%82%B9%E3%83%AD%E3%83%BC%E3%82%AF%E3%82%A8%E3%83%AA%28JOIN%20%26%20GROUP%E7%B7%A8%29&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F4243&#038;p&#091;images&#093;&#091;0&#093;=&#038;p&#091;title&#093;=%E3%81%8A%E6%B0%97%E6%A5%BD%E3%82%B9%E3%83%AD%E3%83%BC%E3%82%AF%E3%82%A8%E3%83%AA%28JOIN%20%26%20GROUP%E7%B7%A8%29\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/facebook.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"http:\/\/twitter.com\/share?url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F4243&#038;text=%E3%81%8A%E6%B0%97%E6%A5%BD%E3%82%B9%E3%83%AD%E3%83%BC%E3%82%AF%E3%82%A8%E3%83%AA%28JOIN%20%26%20GROUP%E7%B7%A8%29\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/twitter.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F4243&#038;title=%E3%81%8A%E6%B0%97%E6%A5%BD%E3%82%B9%E3%83%AD%E3%83%BC%E3%82%AF%E3%82%A8%E3%83%AA%28JOIN%20%26%20GROUP%E7%B7%A8%29\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/linkedin.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-tumblr nolightbox\" data-provider=\"tumblr\" target=\"_blank\" rel=\"nofollow\" title=\"Share on tumblr\" href=\"https:\/\/tumblr.com\/share?s=&#038;v=3&#038;t=%E3%81%8A%E6%B0%97%E6%A5%BD%E3%82%B9%E3%83%AD%E3%83%BC%E3%82%AF%E3%82%A8%E3%83%AA%28JOIN%20%26%20GROUP%E7%B7%A8%29&#038;u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F4243\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"tumblr\" title=\"Share on tumblr\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/tumblr.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=%E3%81%8A%E6%B0%97%E6%A5%BD%E3%82%B9%E3%83%AD%E3%83%BC%E3%82%AF%E3%82%A8%E3%83%AA%28JOIN%20%26%20GROUP%E7%B7%A8%29&#038;body=%E3%82%B7%E3%82%A7%E3%82%A2%E3%81%99%E3%82%8B%EF%BC%9A:%20https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F4243\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/mail.png\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>\u904b\u7528\u4e2d\u306e\u696d\u52d9\u7cfb\u30b7\u30b9\u30c6\u30e0\u304c\u3042\u308b\u3093\u3067\u3059\u304c\u3001\u304a\u5ba2\u69d8\u304b\u3089\u300c\u9045\u3044\u300d\u3068\u3054\u6307\u6458\u3092\u9802\u3044\u305f\u6a5f\u80fd\u3092\u8abf\u3079\u305f\u3068\u3053\u308d\u53f0\u5e33\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3001\u96c6\u8a08\u3057\u3066\u3044\u308b\u30af\u30a8\u30ea\u304c\u975e\u5e38\u306b\u9045\u3044\u4e8b\u304c\u5224\u660e\u3057\u307e\u3057\u305f\u3002 \u8abf\u67fb\u3057\u3066\u3044\u308b\u74b0\u5883\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306f PostgreSQL  [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[135,149],"class_list":["post-4243","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-postgresql","tag-sql"],"_links":{"self":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/4243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/comments?post=4243"}],"version-history":[{"count":19,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/4243\/revisions"}],"predecessor-version":[{"id":4262,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/4243\/revisions\/4262"}],"wp:attachment":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/media?parent=4243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/categories?post=4243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/tags?post=4243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}