{"id":295724,"date":"2019-12-17T16:25:16","date_gmt":"2019-12-17T16:25:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=295724"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=295724","title":{"rendered":"PostgreSQL Antipatterns: \u0441\u0438\u0437\u0438\u0444\u043e\u0432 JOIN \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479920\/\">\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u00ab\u0441\u043a\u043b\u0435\u0438\u0442\u044c\u00bb \u0432\u043d\u0443\u0442\u0440\u0438 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0441 \u0442\u0435\u043c\u0438 \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u00ab\u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c\u00bb.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041a\u0430\u043a \u044d\u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0434\u0435\u043b\u0430\u044e\u0442:<\/p>\n<pre><code class=\"sql\">WITH T1 AS (   SELECT     row_number() OVER() rn   , unnest v1   FROM     unnest('{1,2,3,4}'::integer[]) ) , T2 AS (   SELECT     row_number() OVER() rn   , unnest v2   FROM     unnest('{5,6}'::integer[]) ) SELECT   T1.v1 , T2.v2 FROM   T1 LEFT JOIN   T2     USING(rn);<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">v1 | v2 -------  1 |  5  2 |  6  3 |  4 | <\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0431\u044b\u043b \u00ab\u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u00bb \u0432 \u0432\u044b\u0431\u043e\u0440\u043a\u0443, \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d, \u0430 \u0437\u0430\u0442\u0435\u043c \u044d\u0442\u043e\u0442 \u043d\u043e\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u043a\u0430\u043a \u043a\u043b\u044e\u0447 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f CTE\u2026<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bk\/xs\/dv\/bkxsdvtrdgysym_ttipbkmolnp4.png\"><br \/>  <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/c91872ca09942d3309d7023ecae108c6:0:2019-12-12#explain\" rel=\"nofollow\">[\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 explain.tensor.ru]<\/a><\/p>\n<h3>WITH ORDINALITY<\/h3>\n<p>  \u0411\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u0438 \u0432\u0441\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0443\u0448\u043b\u043e \u043d\u0430 \u043f\u0430\u0440\u0443 WindowAgg!<\/p>\n<p>  \u041d\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432\u0435\u0440\u0441\u0438\u044e PG \u043d\u0435 \u043d\u0438\u0436\u0435 9.4, \u0442\u043e <a href=\"https:\/\/www.depesz.com\/2013\/07\/30\/waiting-for-9-4-add-sql-standard-with-ordinality-support-for-unnest-and-any-other-srf\/\" rel=\"nofollow\">\u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c WITH ORDINALITY<\/a> \u0434\u043b\u044f \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043b\u044e\u0431\u043e\u0439 SRF, \u0432\u043a\u043b\u044e\u0447\u0430\u044f unnest:<\/p>\n<pre><code class=\"sql\">WITH T1 AS (   SELECT     *   FROM     unnest('{1,2,3,4}'::integer[])     WITH ORDINALITY T(v1, rn) ) , T2 AS (   SELECT     *   FROM     unnest('{5,6}'::integer[])     WITH ORDINALITY T(v2, rn) ) SELECT   T1.v1 , T2.v2 FROM   T1 LEFT JOIN   T2     USING(rn);<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ul\/uy\/sn\/uluysnthaeh35kgniztd0tzwt14.png\"><a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/1fd471de56e71d8da16219048bdea9b0:0:2019-12-12\" rel=\"nofollow\">[\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 explain.tensor.ru]<\/a>.<\/p>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u0432\u043e\u043e\u0431\u0449\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u043a\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439.<\/p>\n<h3>Multi-argument UNNEST<\/h3>\n<p>  \u041d\u043e \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u2014 \u043f\u043e\u0447\u0442\u0438 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0443\u0448\u043b\u0430 \u043d\u0430 Hash Left Join.<\/p>\n<p>  \u0414\u0430 \u0438 \u0430\u0432\u0442\u043e\u0440 \u044f\u0432\u043d\u043e \u0438\u0441\u0445\u043e\u0434\u0438\u043b \u0438\u0437 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0442\u043e\u0447\u043d\u043e \u0434\u043b\u0438\u043d\u043d\u0435\u0435 \u2014 \u043f\u043e\u0442\u043e\u043c\u0443 \u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f LEFT JOIN. \u041d\u043e \u044d\u0442\u043e \u0434\u043e\u043f\u0443\u0449\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430, \u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <a href=\"https:\/\/www.depesz.com\/2013\/11\/24\/waiting-for-9-4-support-multi-argument-unnest-and-table-syntax-for-multiple-functions\/\" rel=\"nofollow\">unnest \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0441 \u0442\u043e\u0439 \u0436\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 9.4:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   unnest(     '{1,2,3,4}'::integer[]   , '{5,6}'::integer[]   ) T(v1, v2);<\/code><\/pre>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0438 \u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u0447\u0442\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u0438 \u043e\u0442 \u043f\u043b\u0430\u043d\u0430:<\/p>\n<pre><code class=\"plaintext\">Function Scan on t  (cost=0.01..1.00 rows=100 width=8) (actual time=0.006..0.007 rows=4 loops=1)<\/code><\/pre>\n<p>  \u0417\u043d\u0430\u0447\u0438\u0442, \u0438 \u0448\u0430\u043d\u0441\u043e\u0432 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u2014 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u044c\u0448\u0435. \u0414\u0430 \u0438 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0443\u043b\u0443\u0447\u0448\u0438\u043b\u0438 \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u2014 \u0430 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u044d\u0444\u0444\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0435\u0449\u0435 \u0437\u0430\u043c\u0435\u0442\u043d\u0435\u0435.<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/479920\/\"> https:\/\/habr.com\/ru\/post\/479920\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479920\/\">\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u00ab\u0441\u043a\u043b\u0435\u0438\u0442\u044c\u00bb \u0432\u043d\u0443\u0442\u0440\u0438 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0441 \u0442\u0435\u043c\u0438 \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u00ab\u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c\u00bb.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-295724","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=295724"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295724\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}