{"id":277814,"date":"2016-04-07T10:36:02","date_gmt":"2016-04-07T06:36:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=277814"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=277814","title":{"rendered":"\u041e\u0431\u044a\u044f\u0441\u043d\u044f\u044f \u043d\u0435\u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043c\u043e\u0435. \u0427\u0430\u0441\u0442\u044c 4"},"content":{"rendered":"<p>       <i>\u041a\u043e\u043d\u0444\u0435\u0440\u0435\u043d\u0446\u0438\u044f PG Day\u201916 \u0441 \u043a\u0430\u0436\u0434\u044b\u043c \u0434\u043d\u0435\u043c \u0432\u0441\u0451 \u0431\u043b\u0438\u0436\u0435, \u0430 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439 <a href=\"http:\/\/www.depesz.com\/\">Hubert Lubaczewski<\/a> \u043e\u0431 \u0430\u043d\u0430\u043b\u0438\u0437\u0435 explain \u0438 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445.<\/i><\/p>\n<p>  \u0412 \u044d\u0442\u043e\u043c, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, <a href=\"https:\/\/habrahabr.ru\/post\/275851\/\">\u043f\u0440\u0435\u0434\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c<\/a> <a href=\"https:\/\/habrahabr.ru\/post\/276973\/\">\u043f\u043e\u0441\u0442\u0435<\/a> <a href=\"https:\/\/habrahabr.ru\/post\/279255\/\">\u0441\u0435\u0440\u0438\u0438<\/a> \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0432 \u0432\u044b\u0432\u043e\u0434\u0435 explain.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/89e\/4b9\/c11\/89e4b9c11f4e4d45b0134edbd13a0db6.jpg\"\/><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4><font color=\"#000000\">Unique<\/font><\/h4>\n<p>  \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0441\u0430\u043c\u043e \u0437\u0430 \u0441\u0435\u0431\u044f \u2013 \u043e\u043d\u0430 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<p>  \u0422\u0430\u043a\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"sql\">select distinct field from table <\/code><\/pre>\n<p>  \u0412 \u0431\u043e\u043b\u0435\u0435 \u0441\u0432\u0435\u0436\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u041f\u043e\u0441\u0442\u0433\u0440\u0435\u0441\u0430 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e HashAggregate.<\/p>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 Unique \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043d\u0435\u0451 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b. \u041d\u0435 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u044d\u0442\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0431\u044b\u043b\u0438 \u00ab\u0432\u043c\u0435\u0441\u0442\u0435\u00bb.<\/p>\n<p>  \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 Unique \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 (\u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c), \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u0438. \u041e\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435, \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0435\u0433\u043e. \u0412\u043e\u0442 \u0438 \u0432\u0441\u0451.<\/p>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0442\u0438\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0432 \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"sql\">$ explain select distinct relkind from (select relkind from pg_class order by relkind) as x;                               QUERY PLAN -----------------------------------------------------------------------  Unique  (cost=22.88..27.26 rows=4 width=1)    -&gt;  Sort  (cost=22.88..23.61 rows=292 width=1)          Sort Key: pg_class.relkind          -&gt;  Seq Scan on pg_class  (cost=0.00..10.92 rows=292 width=1) (4 rows) <\/code><\/pre>\n<h4><font color=\"#000000\">Append<\/font><\/h4>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u043b\u0430\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u0443\u0431\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u044b\u0435 \u0438\u043c\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0431\u0449\u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430.<\/p>\n<p>  \u042d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 UNION\/UNION ALL:<\/p>\n<pre><code class=\"sql\">$ explain select oid from pg_class union all select oid from pg_proc union all select oid from pg_database;                            QUERY PLAN                             -----------------------------------------------------------------  Append  (cost=0.00..104.43 rows=2943 width=4)    -&gt;  Seq Scan on pg_class  (cost=0.00..10.92 rows=292 width=4)    -&gt;  Seq Scan on pg_proc  (cost=0.00..92.49 rows=2649 width=4)    -&gt;  Seq Scan on pg_database  (cost=0.00..1.02 rows=2 width=4) (4 rows) <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435, \u043a\u0430\u043a append \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0442\u0440\u0438 \u0441\u043a\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e \u0442\u0440\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0438 \u0432\u0435\u0440\u043d\u0443\u043b \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432\u043c\u0435\u0441\u0442\u0435.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b UNION ALL. \u0415\u0441\u043b\u0438 \u0431\u044b \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b UNION, \u043c\u044b \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"sql\">$ explain select oid from pg_class union select oid from pg_proc union select oid from pg_database;                               QUERY PLAN                                -----------------------------------------------------------------------  HashAggregate  (cost=141.22..170.65 rows=2943 width=4)    -&gt;  Append  (cost=0.00..133.86 rows=2943 width=4)          -&gt;  Seq Scan on pg_class  (cost=0.00..10.92 rows=292 width=4)          -&gt;  Seq Scan on pg_proc  (cost=0.00..92.49 rows=2649 width=4)          -&gt;  Seq Scan on pg_database  (cost=0.00..1.02 rows=2 width=4) (5 rows) <\/code><\/pre>\n<p>  \u0422\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e UNION \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0448\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0447\u0442\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 HashAggregate.<\/p>\n<h4><font color=\"#000000\">Result<\/font><\/h4>\n<p>  Result \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c, \u0432 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445. \u042d\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u0448 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u043a\u0430\u043a\u043e\u0435-\u043b\u0438\u0431\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (\u0438\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f):<\/p>\n<pre><code class=\"sql\">$ explain select 1, 2;                 QUERY PLAN                 ------------------------------------------  Result  (cost=0.00..0.01 rows=1 width=0) (1 row) <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u0438\u043c\u043e \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u0430\u044e\u0442 \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u00ab\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445\u00bb:<\/p>\n<pre><code class=\"sql\">$ explain insert into t (i) select 1 where not exists (select * from t where i = 1);                              QUERY PLAN                               ---------------------------------------------------------------------  Insert on t  (cost=3.33..3.35 rows=1 width=4)    -&gt;  Result  (cost=3.33..3.34 rows=1 width=0)          One-Time Filter: (NOT $0)          InitPlan 1 (returns $0)            -&gt;  Seq Scan on t t_1  (cost=0.00..40.00 rows=12 width=0)                  Filter: (i = 1) (6 rows) <\/code><\/pre>\n<p>  <\/p>\n<h4><font color=\"#000000\">Values Scan<\/font><\/h4>\n<p>  \u0422\u0430\u043a \u0436\u0435, \u043a\u0430\u043a Result, Values Scan \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 VALUES().<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0432\u044b \u043d\u0435 \u0432 \u043a\u0443\u0440\u0441\u0435, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0431\u0435\u0437 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 VALUES, \u043a\u0430\u043a \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435:<\/p>\n<pre><code class=\"sql\">$ select * from ( values (1, 'hubert'), (2, 'depesz'), (3, 'lubaczewski') ) as t (a,b);  a |      b       ---+-------------  1 | hubert  2 | depesz  3 | lubaczewski (3 rows) <\/code><\/pre>\n<p>  \u041f\u043b\u0430\u043d \u0442\u0430\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">                          QUERY PLAN                           --------------------------------------------------------------  Values Scan on &quot;*VALUES*&quot;  (cost=0.00..0.04 rows=3 width=36) (1 row) <\/code><\/pre>\n<p>  \u042d\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 INSERT\u2019\u0430\u0445, \u043d\u043e \u0443 \u043d\u0435\u0451 \u0435\u0441\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"http:\/\/www.depesz.com\/2010\/07\/25\/how-to-order-by-some-random-query-defined-values\/\">\u043a\u0430\u0441\u0442\u043e\u043c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/a>.<\/p>\n<h4><font color=\"#000000\">GroupAggregate<\/font><\/h4>\n<p>  \u042d\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0445\u043e\u0436\u0430 \u0441 HashAggregate, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b <a href=\"https:\/\/habrahabr.ru\/post\/279255\/\">\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u0440\u0430\u043d\u0435\u0435<\/a>.<\/p>\n<p>  \u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b GroupAggregate \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u0438 GROUP BY.<\/p>\n<p>  \u041a\u0430\u043a \u0438 Unique, GroupAggregate \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">$ explain select relkind, count(*) from (select relkind from pg_class order by relkind) x group by relkind;                               QUERY PLAN                                -----------------------------------------------------------------------  GroupAggregate  (cost=22.88..28.03 rows=4 width=1)    -&gt;  Sort  (cost=22.88..23.61 rows=292 width=1)          Sort Key: pg_class.relkind          -&gt;  Seq Scan on pg_class  (cost=0.00..10.92 rows=292 width=1) (4 rows) <\/code><\/pre>\n<p>  <\/p>\n<h4><font color=\"#000000\">HashSetOp<\/font><\/h4>\n<p>  \u042d\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 INTERSECT\/EXCEPT (\u0441 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c \u00abALL\u00bb).<\/p>\n<p>  \u041e\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0441\u0443\u0431\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 Append \u0434\u043b\u044f \u043f\u0430\u0440\u044b \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0430 \u0437\u0430\u0442\u0435\u043c, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 ALL, \u0440\u0435\u0448\u0430\u0435\u0442, \u043a\u0430\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0443\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c. \u042f \u043d\u0435 \u0437\u0430\u043b\u0435\u0437\u0430\u043b \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043c\u043e\u0433\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e, \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0445 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u0431\u0430\u0437\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430.<\/p>\n<p>  \u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 UNION, \u044d\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0441 \u0434\u0432\u0443\u043c\u044f \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"sql\">$ explain select * from (select oid from pg_Class order by oid) x intersect all select * from (select oid from pg_proc order by oid) y;                                                  QUERY PLAN                                                   -------------------------------------------------------------------------------------------------------------  HashSetOp Intersect All  (cost=0.15..170.72 rows=292 width=4)    -&gt;  Append  (cost=0.15..163.36 rows=2941 width=4)          -&gt;  Subquery Scan on &quot;*SELECT* 1&quot;  (cost=0.15..18.37 rows=292 width=4)                -&gt;  Index Only Scan using pg_class_oid_index on pg_class  (cost=0.15..12.53 rows=292 width=4)          -&gt;  Subquery Scan on &quot;*SELECT* 2&quot;  (cost=0.28..145.00 rows=2649 width=4)                -&gt;  Index Only Scan using pg_proc_oid_index on pg_proc  (cost=0.28..92.02 rows=2649 width=4) (6 rows) <\/code><\/pre>\n<p>  \u0410 \u0441 \u0442\u0440\u0435\u043c\u044f \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u043c\u0438 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e:<\/p>\n<pre><code class=\"sql\">$ explain select * from (select oid from pg_Class order by oid) x intersect all select * from (select oid from pg_proc order by oid) y intersect all select * from (Select oid from pg_database order by oid) as w;                                                           QUERY PLAN                                                            -------------------------------------------------------------------------------------------------------------------------------  HashSetOp Intersect All  (cost=1.03..172.53 rows=2 width=4)    -&gt;  Append  (cost=1.03..171.79 rows=294 width=4)          -&gt;  Subquery Scan on &quot;*SELECT* 3&quot;  (cost=1.03..1.07 rows=2 width=4)                -&gt;  Sort  (cost=1.03..1.03 rows=2 width=4)                      Sort Key: pg_database.oid                      -&gt;  Seq Scan on pg_database  (cost=0.00..1.02 rows=2 width=4)          -&gt;  Result  (cost=0.15..170.72 rows=292 width=4)                -&gt;  HashSetOp Intersect All  (cost=0.15..170.72 rows=292 width=4)                      -&gt;  Append  (cost=0.15..163.36 rows=2941 width=4)                            -&gt;  Subquery Scan on &quot;*SELECT* 1&quot;  (cost=0.15..18.37 rows=292 width=4)                                  -&gt;  Index Only Scan using pg_class_oid_index on pg_class  (cost=0.15..12.53 rows=292 width=4)                            -&gt;  Subquery Scan on &quot;*SELECT* 2&quot;  (cost=0.28..145.00 rows=2649 width=4)                                  -&gt;  Index Only Scan using pg_proc_oid_index on pg_proc  (cost=0.28..92.02 rows=2649 width=4) (13 rows) <\/code><\/pre>\n<p>  <\/p>\n<h4><font color=\"#000000\">CTE Scan<\/font><\/h4>\n<p>  \u042d\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0445\u043e\u0436\u0430 \u0441 <a href=\"https:\/\/habrahabr.ru\/post\/279255\/\">\u0443\u0436\u0435 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0439<\/a> \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 Materialize. \u041e\u043d\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0435\u0435 \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u0440\u0443\u0433\u043e\u0439 \u0447\u0430\u0441\u0442\u044c\u044e (\u0438\u043b\u0438 \u0447\u0430\u0441\u0442\u044f\u043c\u0438) \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">$ explain analyze with x as (select relname, relkind from pg_class) select relkind, count(*), (select count(*) from x) from x group by relkind;                                                    QUERY PLAN                                                     -----------------------------------------------------------------------------------------------------------------  HashAggregate  (cost=24.80..26.80 rows=200 width=1) (actual time=0.466..0.468 rows=6 loops=1)    CTE x      -&gt;  Seq Scan on pg_class  (cost=0.00..10.92 rows=292 width=65) (actual time=0.009..0.127 rows=295 loops=1)    InitPlan 2 (returns $1)      -&gt;  Aggregate  (cost=6.57..6.58 rows=1 width=0) (actual time=0.085..0.085 rows=1 loops=1)            -&gt;  CTE Scan on x x_1  (cost=0.00..5.84 rows=292 width=0) (actual time=0.000..0.055 rows=295 loops=1)    -&gt;  CTE Scan on x  (cost=0.00..5.84 rows=292 width=1) (actual time=0.012..0.277 rows=295 loops=1)  Total runtime: 0.524 ms (8 rows) <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e pg_class \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u2013 \u0441\u0442\u0440\u043e\u043a\u0430 #6. \u041d\u043e \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u201cx&quot; \u0438 \u043f\u043e\u0442\u043e\u043c \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b \u2013 \u0432\u043d\u0443\u0442\u0440\u0438 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430 (\u0441\u0442\u0440\u043e\u043a\u0430 #9) \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 HashAggregate (10).<\/p>\n<p>  \u0412 \u0447\u0435\u043c \u0436\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 Materialize? \u0427\u0442\u043e\u0431\u044b \u0434\u0430\u0442\u044c \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441, \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u0442\u044c\u0441\u044f \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434, \u043d\u043e \u044f \u0431\u044b \u0441\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u0444\u0430\u043a\u0442\u0435, \u0447\u0442\u043e CTE \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a Materialize \u2013 \u044d\u0442\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u041f\u043e\u0441\u0442\u0433\u0440\u0435\u0441 \u0440\u0435\u0448\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u043c.<\/p>\n<p>  \u0412\u0430\u0436\u043d\u043e\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435: CTE \u0432\u0441\u0435\u0433\u0434\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a, \u043a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438 \u043d\u0435 \u0441\u0430\u043c\u044b\u0435 \u0443\u0434\u0430\u0447\u043d\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a.<\/p>\n<h4><font color=\"#000000\">InitPlan<\/font><\/h4>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u043b\u0430\u043d \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 (\u0438\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u0430) \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u0430 \u043f\u0435\u0440\u0435\u0434 \u0432\u0441\u0435\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043d\u0438 \u043e\u0442 \u0447\u0435\u0433\u043e \u0432 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0447\u0430\u0441\u0442\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">$ explain select * from pg_class where relkind = (select relkind from pg_class order by random() limit 1);                                         QUERY PLAN                                         ------------------------------------------------------------------------------------------  Seq Scan on pg_class  (cost=13.11..24.76 rows=73 width=203)    Filter: (relkind = $0)    InitPlan 1 (returns $0)      -&gt;  Limit  (cost=13.11..13.11 rows=1 width=1)            -&gt;  Sort  (cost=13.11..13.84 rows=292 width=1)                  Sort Key: (random())                  -&gt;  Seq Scan on pg_class pg_class_1  (cost=0.00..11.65 rows=292 width=1) (7 rows) <\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c limit\/sort\/seq-scan \u0434\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043a\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e pg_class, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u041f\u043e\u0441\u0442\u0433\u0440\u0435\u0441\u0443 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 relkind \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u044b\u043c \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c.<\/p>\n<p>  \u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u044f \u043c\u043e\u0433 \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c:<\/p>\n<pre><code class=\"sql\">$ explain select *, (select length('depesz')) from pg_class;                          QUERY PLAN                           -------------------------------------------------------------  Seq Scan on pg_class  (cost=0.01..10.93 rows=292 width=203)    InitPlan 1 (returns $0)      -&gt;  Result  (cost=0.00..0.01 rows=1 width=0) (3 rows) <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u0442\u0433\u0440\u0435\u0441 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0432\u0438\u0434\u0438\u0442, \u0447\u0442\u043e \u0441\u0442\u043e\u043b\u0431\u0435\u0446 subselect \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043d\u0438 \u043e\u0442 \u043a\u0430\u043a\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b pg_class, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0433\u043d\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0443 \u0432\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u044b\u0445 \u043f\u043b\u0430\u043d\u043e\u0432 (init plans), \u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c:<\/p>\n<pre><code class=\"sql\">$ explain select *, (select length('depesz')) from pg_class where relkind = (select relkind from pg_class order by random() limit 1);                                         QUERY PLAN                                         ------------------------------------------------------------------------------------------  Seq Scan on pg_class  (cost=13.12..24.77 rows=73 width=203)    Filter: (relkind = $1)    InitPlan 1 (returns $0)      -&gt;  Result  (cost=0.00..0.01 rows=1 width=0)    InitPlan 2 (returns $1)      -&gt;  Limit  (cost=13.11..13.11 rows=1 width=1)            -&gt;  Sort  (cost=13.11..13.84 rows=292 width=1)                  Sort Key: (random())                  -&gt;  Seq Scan on pg_class pg_class_1  (cost=0.00..11.65 rows=292 width=1) (9 rows) <\/code><\/pre>\n<p>  \u041d\u043e \u0441\u0442\u043e\u0438\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0434\u0435\u0442\u0430\u043b\u044c \u2013 init plan\u2019\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0443\u043c\u0435\u0440\u0443\u044e\u0442\u0441\u044f \u00ab\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u00bb, \u0430 \u043d\u0435 \u043f\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c.<\/p>\n<h4><font color=\"#000000\">SubPlan<\/font><\/h4>\n<p>  SubPlan\u2019\u044b \u0447\u0435\u043c-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 NestedLoop. \u0412 \u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435, \u0447\u0442\u043e \u043e\u043d\u0438 \u0442\u043e\u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437.<\/p>\n<p>  SubPlan \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0441\u0443\u0431\u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">$ explain analyze select c.relname, c.relkind, (Select count(*) from pg_Class x where c.relkind = x.relkind) from pg_Class c;                                                       QUERY PLAN                                                        -----------------------------------------------------------------------------------------------------------------------  Seq Scan on pg_class c  (cost=0.00..3468.93 rows=292 width=65) (actual time=0.135..26.717 rows=295 loops=1)    SubPlan 1      -&gt;  Aggregate  (cost=11.83..11.84 rows=1 width=0) (actual time=0.090..0.090 rows=1 loops=295)            -&gt;  Seq Scan on pg_class x  (cost=0.00..11.65 rows=73 width=0) (actual time=0.010..0.081 rows=93 loops=295)                  Filter: (c.relkind = relkind)                  Rows Removed by Filter: 202  Total runtime: 26.783 ms (7 rows) <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u043e\u0439 \u0441\u043a\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043e \u00abpg_class as c\u00bb, \u041f\u043e\u0441\u0442\u0433\u0440\u0435\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c SubPlan, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u0432 pg_class \u0438\u043c\u0435\u044e\u0442 \u0442\u0430\u043a\u043e\u0435 \u0436\u0435 (\u043a\u0430\u043a \u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438) \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 relkind.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u00abloops=295\u00bb \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 \u00abSeq Scan on pg_class x\u00bb \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u0435\u043c\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u00abrows=295\u00bb \u0432 \u0443\u0437\u043b\u0435 \u00abSeq Scan on pg_class c\u00bb.<\/p>\n<h4><font color=\"#000000\">\u0414\u0440\u0443\u0433\u0438\u0435?<\/font><\/h4>\n<p>  \u0414\u0430, \u0435\u0441\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0440\u0435\u0434\u043a\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0441\u0442\u043e\u0438\u0442\u044c\u0441\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0443\u0447\u0435\u0441\u0442\u044c, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0437\u043d\u0430\u043d\u0438\u0439 \u2013 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438), \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f (\u043a\u0430\u043a \u044f \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u044e) \u0441\u0442\u0430\u0440\u044b\u043c\u0438 \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438 \u043d\u043e\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u043b\u0430\u043d \u0441 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u043d\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b, \u0438 \u0432\u0430\u043c \u043e\u043d \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u0435\u043d, \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043c\u043d\u0435, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0432\u044b\u0432\u043e\u0434 explain \u043d\u0430 <a href=\"http:\/\/explain.depesz.com\">explain.depesz.com<\/a>, \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0432\u0435\u0440\u0441\u0438\u044e \u041f\u043e\u0441\u0433\u0440\u0435\u0441\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d\u0430 \u0432\u0430\u043c \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c. \u042f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043d\u0430\u0439\u0442\u0438 \u0432\u0441\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u043e \u0442\u0430\u043a\u0438\u043c \u043a\u0435\u0439\u0441\u0430\u043c \u0438 \u0434\u0430\u0442\u044c \u0432\u0430\u043c \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u0439 \u043e\u0442\u0432\u0435\u0442.       <\/p>\n<div class=\"clear\"><\/div>\n<p> \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:\/\/habrahabr.ru\/post\/281036\/\"> https:\/\/habrahabr.ru\/post\/281036\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <i>\u041a\u043e\u043d\u0444\u0435\u0440\u0435\u043d\u0446\u0438\u044f PG Day\u201916 \u0441 \u043a\u0430\u0436\u0434\u044b\u043c \u0434\u043d\u0435\u043c \u0432\u0441\u0451 \u0431\u043b\u0438\u0436\u0435, \u0430 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439 <a href=\"http:\/\/www.depesz.com\/\">Hubert Lubaczewski<\/a> \u043e\u0431 \u0430\u043d\u0430\u043b\u0438\u0437\u0435 explain \u0438 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445.<\/i><\/p>\n<p>  \u0412 \u044d\u0442\u043e\u043c, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, <a href=\"https:\/\/habrahabr.ru\/post\/275851\/\">\u043f\u0440\u0435\u0434\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c<\/a> <a href=\"https:\/\/habrahabr.ru\/post\/276973\/\">\u043f\u043e\u0441\u0442\u0435<\/a> <a href=\"https:\/\/habrahabr.ru\/post\/279255\/\">\u0441\u0435\u0440\u0438\u0438<\/a> \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0432 \u0432\u044b\u0432\u043e\u0434\u0435 explain.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/89e\/4b9\/c11\/89e4b9c11f4e4d45b0134edbd13a0db6.jpg\"\/>  <\/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-277814","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/277814","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=277814"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/277814\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=277814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=277814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=277814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}