{"id":343896,"date":"2023-01-13T15:01:37","date_gmt":"2023-01-13T15:01:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=343896"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=343896","title":{"rendered":"<span>PostgreSQL Antipatterns: \u0418\u043d\u0434\u0438\u0430\u043d\u0430 \u0414\u0436\u043e\u043d\u0441 \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0430, \u0438\u043b\u0438 \u0412 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u00ab\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445\u00bb \u0437\u0430\u043f\u0438\u0441\u0435\u0439<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u044f\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u043f\u043e\u043b\u043d\u0435 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u0430 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 \u0443\u0447\u0435\u0442\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c &#8212; \u043f\u043e\u0438\u0441\u043a \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 <strong>\u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0439<\/strong>. \u0427\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 &#171;<strong><em>\u043f\u043e\u043a\u0430\u0436\u0438 \u043c\u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0437\u0430\u043a\u0430\u0437 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/em><\/strong>&#171;, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u0443\u044e \u043e\u0431\u043b\u0430\u0441\u0442\u044c.<\/p>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0443\u0442 \u0438 \u0441\u043f\u043e\u0442\u043a\u043d\u0443\u0442\u044c\u0441\u044f-\u0442\u043e \u043d\u0435\u0433\u0434\u0435 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 &#8212; \u043d\u043e \u0432\u0441\u0435 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b7e\/b82\/fea\/b7eb82feabb0d0b74f9cacdd0433e041.png\" alt=\"\u041f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043d\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f\" title=\"\u041f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043d\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f\" width=\"791\" height=\"511\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b7e\/b82\/fea\/b7eb82feabb0d0b74f9cacdd0433e041.png\"\/><figcaption>\u041f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043d\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f<\/figcaption><\/figure>\n<details class=\"spoiler\">\n<summary>\u041a\u0414\u041f\u0412<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0418\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u044b <a href=\"https:\/\/mixnews.lv\/men\/2021\/01\/28\/20-faktov-o-filme-indiana-dzhons-i-posledniy-krestovyy-pohod\/\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a>.<\/p>\n<\/div>\n<\/details>\n<p>\u0414\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430 PostgreSQL 13 \u0438 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e \u043d\u0430\u0448\u0438\u0445 &#171;\u0437\u0430\u043a\u0430\u0437\u0430\u0445&#187;:<\/p>\n<pre><code class=\"sql\">CREATE TABLE orders AS SELECT   (random() * 1e3)::integer client_id        -- \u043e\u0434\u0438\u043d \u0438\u0437 1K \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 , now()::date - (random() * 1e3)::integer dt -- \u0434\u0430\u0442\u0430 \u0437\u0430\u043a\u0430\u0437\u0430 , (random() * 1e3)::integer order_data       -- \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0437\u0430\u043a\u0430\u0437\u0443 FROM   generate_series(1, 1e5); -- 100K \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0445<\/code><\/pre>\n<p>\u041a\u0430\u043a \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e &#171;<em>\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0437\u0430\u043a\u0430\u0437 \u043f\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443<\/em>&#187; \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e <code>(client_id, dt)<\/code>:<\/p>\n<pre><code class=\"sql\">CREATE INDEX ON orders(client_id, dt);<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ea9\/b69\/b26\/ea9b69b26f0dfb927471e24e0d895094.png\" alt=\"\u0412\u043e\u0442 \u0441 \u044d\u0442\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u043f\u043e\u043b\u0435\u0442\u0438\u0442!..\" title=\"\u0412\u043e\u0442 \u0441 \u044d\u0442\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u043f\u043e\u043b\u0435\u0442\u0438\u0442!..\" width=\"809\" height=\"526\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ea9\/b69\/b26\/ea9b69b26f0dfb927471e24e0d895094.png\"\/><figcaption>\u0412\u043e\u0442 \u0441 \u044d\u0442\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u043f\u043e\u043b\u0435\u0442\u0438\u0442!..<\/figcaption><\/figure>\n<h2>max + JOIN = 2 x Seq Scan<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430 \u0443\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0441\u0430\u043c\u044b\u0439 &#171;\u0442\u0443\u043f\u043e\u0439&#187; \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#8212; \u043d\u0430\u0439\u0442\u0438 \u044d\u0442\u0438 \u0441\u0430\u043c\u044b\u0435 &#171;\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435&#187; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0439, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   (     SELECT       client_id     , max(dt) dt     FROM       orders     GROUP BY       client_id   ) T JOIN   orders     USING(client_id, dt);<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u0430\u0435\u0442 \u0432\u043e\u0442 <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/0fe767269ae0d94efb63ee552e63ffb9:0:2023-01-13\">\u0442\u0430\u043a\u043e\u0439 \u043f\u043b\u0430\u043d<\/a> \u043d\u0430 <strong>47\u043c\u0441\/1082 buffers<\/strong>, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 <code>orders<\/code> \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ad9\/743\/a16\/ad9743a16bb03aeeb7d8be39bd915d32.png\" alt=\"\u0414\u0432\u043e\u0439\u043d\u043e\u0439 Seq Scan\" title=\"\u0414\u0432\u043e\u0439\u043d\u043e\u0439 Seq Scan\" width=\"954\" height=\"455\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ad9\/743\/a16\/ad9743a16bb03aeeb7d8be39bd915d32.png\"\/><figcaption>\u0414\u0432\u043e\u0439\u043d\u043e\u0439 Seq Scan<\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9c6\/ae7\/fea\/9c6ae7feaca1cd6e84169a15173a2915.png\" alt=\"&quot;\u0414\u0432\u0430 \u0440\u0430\u0437\u0430, \u041a\u0430\u0440\u043b, \u0434\u0432\u0430 \u0440\u0430\u0437\u0430!&quot;... \u0445\u043e\u0442\u044f, \u044d\u0442\u043e \u043d\u0435 \u043e\u0442\u0441\u044e\u0434\u0430\" title=\"&quot;\u0414\u0432\u0430 \u0440\u0430\u0437\u0430, \u041a\u0430\u0440\u043b, \u0434\u0432\u0430 \u0440\u0430\u0437\u0430!&quot;... \u0445\u043e\u0442\u044f, \u044d\u0442\u043e \u043d\u0435 \u043e\u0442\u0441\u044e\u0434\u0430\" width=\"796\" height=\"500\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9c6\/ae7\/fea\/9c6ae7feaca1cd6e84169a15173a2915.png\"\/><figcaption>&#171;\u0414\u0432\u0430 \u0440\u0430\u0437\u0430, \u041a\u0430\u0440\u043b, \u0434\u0432\u0430 \u0440\u0430\u0437\u0430!&#187;&#8230; \u0445\u043e\u0442\u044f, \u044d\u0442\u043e \u043d\u0435 \u043e\u0442\u0441\u044e\u0434\u0430<\/figcaption><\/figure>\n<h2>DISTINCT ON<\/h2>\n<p>\u041d\u043e \u0432\u0440\u043e\u0434\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0441\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u0432\u0430\u0436\u0434\u044b &#8212; \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e <code>client_id<\/code>, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 <code>DISTINCT ON<\/code>:<\/p>\n<pre><code class=\"sql\">SELECT DISTINCT ON(client_id)   * FROM   orders ORDER BY   client_id, dt DESC;<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3db\/7e6\/4b7\/3db7e64b78776975498f27fa213de05e.png\" alt=\"\u0417\u0430\u043b\u0435\u0437\u043b\u0438 \u0432 temp buffers\" title=\"\u0417\u0430\u043b\u0435\u0437\u043b\u0438 \u0432 temp buffers\" width=\"894\" height=\"310\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3db\/7e6\/4b7\/3db7e64b78776975498f27fa213de05e.png\"\/><figcaption>\u0417\u0430\u043b\u0435\u0437\u043b\u0438 \u0432 temp buffers<\/figcaption><\/figure>\n<p>\u041d\u0435\u0437\u0430\u0434\u0430\u0447\u0430&#8230; <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/057356eeaf7cadfc576d1935af78025d:0:2023-01-13\">\u0441\u0442\u0430\u043b\u043e \u0432\u0434\u0432\u043e\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435<\/a> \u0438\u0437-\u0437\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u0434\u0438\u0441\u043a.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f6a\/f30\/080\/f6af30080be23f78b75e7475bd095dc3.png\" alt=\"\u041f\u0443\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0442\u0435\u0440\u043d\u0438\u0441\u0442 \u0438 \u043e\u043f\u0430\u0441\u0435\u043d\" title=\"\u041f\u0443\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0442\u0435\u0440\u043d\u0438\u0441\u0442 \u0438 \u043e\u043f\u0430\u0441\u0435\u043d\" width=\"803\" height=\"518\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f6a\/f30\/080\/f6af30080be23f78b75e7475bd095dc3.png\"\/><figcaption>\u041f\u0443\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0442\u0435\u0440\u043d\u0438\u0441\u0442 \u0438 \u043e\u043f\u0430\u0441\u0435\u043d<\/figcaption><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432 \u043e\u0431\u044a\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/13\/runtime-config-resource#GUC-WORK-MEM\">work_mem<\/a>, \u043a\u0430\u043a \u0441\u043e\u0432\u0435\u0442\u0443\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0430 \u043d\u0430 <a href=\"https:\/\/explain.tensor.ru\/\">explain.tensor.ru<\/a>:<\/p>\n<pre><code class=\"sql\">SET work_mem = '8MB';<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c4e\/988\/d08\/c4e988d08f406c97fcc1e683df5d1aed.png\" alt=\"\u0422\u0435\u043f\u0435\u0440\u044c work_mem \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0434\u043b\u044f Sort Memory\" title=\"\u0422\u0435\u043f\u0435\u0440\u044c work_mem \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0434\u043b\u044f Sort Memory\" width=\"821\" height=\"317\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c4e\/988\/d08\/c4e988d08f406c97fcc1e683df5d1aed.png\"\/><figcaption>\u0422\u0435\u043f\u0435\u0440\u044c work_mem \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0434\u043b\u044f Sort Memory<\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d49\/306\/859\/d49306859c9c7f105c2fc3874ee832b1.png\" alt=\"\u0417\u0430\u0431\u0440\u0435\u0437\u0436\u0438\u043b \u0441\u0432\u0435\u0442 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0442\u0443\u043d\u043d\u0435\u043b\u044f\" title=\"\u0417\u0430\u0431\u0440\u0435\u0437\u0436\u0438\u043b \u0441\u0432\u0435\u0442 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0442\u0443\u043d\u043d\u0435\u043b\u044f\" width=\"806\" height=\"526\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d49\/306\/859\/d49306859c9c7f105c2fc3874ee832b1.png\"\/><figcaption>\u0417\u0430\u0431\u0440\u0435\u0437\u0436\u0438\u043b \u0441\u0432\u0435\u0442 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0442\u0443\u043d\u043d\u0435\u043b\u044f<\/figcaption><\/figure>\n<h2>\u041f\u043e\u043c\u043d\u0438 \u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435!<\/h2>\n<p>\u041d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: <code>Sort Key: client_id, dt DESC<\/code>  &#8212; \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430-\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u043c \u043d\u0430\u043c\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438&#8230;<\/p>\n<pre><code class=\"sql\">CREATE INDEX ON orders(client_id, dt DESC);<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bd5\/bdc\/170\/bd5bdc1703a5ac59f3b610d84db1f07b.png\" alt=\"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\" title=\"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\" width=\"1067\" height=\"228\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bd5\/bdc\/170\/bd5bdc1703a5ac59f3b610d84db1f07b.png\"\/><figcaption>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441<\/figcaption><\/figure>\n<p>\u041f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/cfb8b504db0718ce68ce42ff18c3447c:0:2023-01-13\">\u0441\u0440\u0430\u0432\u043d\u044f\u043b\u0438\u0441\u044c \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c<\/a>, \u043d\u043e \u0432\u043e\u0442 buffers \u0442\u0435\u043f\u0435\u0440\u044c <strong>\u0432 100 \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435<\/strong>!<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5b4\/4ec\/00b\/5b44ec00bfb136cae9e14563305b131a.png\" alt=\"\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0433\u043e\u0440\u0430\u0435\u0442\" title=\"\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0433\u043e\u0440\u0430\u0435\u0442\" width=\"807\" height=\"513\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5b4\/4ec\/00b\/5b44ec00bfb136cae9e14563305b131a.png\"\/><figcaption>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0433\u043e\u0440\u0430\u0435\u0442<\/figcaption><\/figure>\n<h2>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 &#171;DISTINCT&#187;<\/h2>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0437\u0430\u043a\u0430\u0437\u043e\u0432, \u0434\u0430 \u0438 \u0432 \u043e\u0431\u0449\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u0438\u0441\u043a\u0430 &#171;\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445&#187; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <code>client_id<\/code> \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u0430:<\/p>\n<pre><code class=\"sql\">WITH RECURSIVE rec AS (   SELECT     -1 client_id                  -- \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u043a\u043b\u044e\u0447 \u0438\u043d\u0434\u0435\u043a\u0441\u0430   , NULL::orders r UNION ALL   SELECT     (X).client_id   , X   FROM     rec   , LATERAL (       SELECT         X                         -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0446\u0435\u043b\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b       FROM         orders X       WHERE         client_id > rec.client_id -- \u0448\u0430\u0433\u0430\u0435\u043c \u043a \"\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443\" \u043a\u043b\u0438\u0435\u043d\u0442\u0443       ORDER BY         client_id, dt DESC        -- \u043f\u043e\u043c\u043d\u0438\u043c \u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435       LIMIT 1                     -- \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c     ) T ) SELECT   (r).*   -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u044b FROM   rec OFFSET 1; -- \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/989\/adb\/efc\/989adbefc76b87fa1aec895a6c7d2534.png\" alt=\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 DISTINCT\" title=\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 DISTINCT\" width=\"1169\" height=\"531\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/989\/adb\/efc\/989adbefc76b87fa1aec895a6c7d2534.png\"\/><figcaption>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 DISTINCT<\/figcaption><\/figure>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0441\u0442\u0430\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0437\u0430\u0442\u043e <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/86cedc8f518b175f26b59d4f7bea724c:0:2023-01-13#explain\">\u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442<\/a> \u0443\u0436\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435 \u0432 \u043f\u043b\u0430\u043d\u0435 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438: 9.5\u043c\u0441\/3008 buffers &#8212; <strong>\u0432 5 \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/strong> \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430!<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eab\/0fd\/628\/eab0fd62807abd483ed7a4205be477db.png\" alt=\"\u0422\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u043c\u044b - \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u044b!\" title=\"\u0422\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u043c\u044b - \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u044b!\" width=\"806\" height=\"529\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eab\/0fd\/628\/eab0fd62807abd483ed7a4205be477db.png\"\/><figcaption>\u0422\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u043c\u044b &#8212; \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u044b!<\/figcaption><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><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\/company\/tensor\/blog\/710400\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/710400\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u044f\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u043f\u043e\u043b\u043d\u0435 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u0430 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 \u0443\u0447\u0435\u0442\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c &#8212; \u043f\u043e\u0438\u0441\u043a \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 <strong>\u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0439<\/strong>. \u0427\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 &#171;<strong><em>\u043f\u043e\u043a\u0430\u0436\u0438 \u043c\u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0437\u0430\u043a\u0430\u0437 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/em><\/strong>&#171;, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u0443\u044e \u043e\u0431\u043b\u0430\u0441\u0442\u044c.<\/p>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0443\u0442 \u0438 \u0441\u043f\u043e\u0442\u043a\u043d\u0443\u0442\u044c\u0441\u044f-\u0442\u043e \u043d\u0435\u0433\u0434\u0435 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 &#8212; \u043d\u043e \u0432\u0441\u0435 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e.<\/p>\n<figure class=\"full-width\"><figcaption>\u041f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043d\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f<\/figcaption><\/figure>\n<details class=\"spoiler\">\n<summary>\u041a\u0414\u041f\u0412<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0418\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u044b <a href=\"https:\/\/mixnews.lv\/men\/2021\/01\/28\/20-faktov-o-filme-indiana-dzhons-i-posledniy-krestovyy-pohod\/\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a>.<\/p>\n<\/div>\n<\/details>\n<p>\u0414\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430 PostgreSQL 13 \u0438 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e \u043d\u0430\u0448\u0438\u0445 &#171;\u0437\u0430\u043a\u0430\u0437\u0430\u0445&#187;:<\/p>\n<pre><code class=\"sql\">CREATE TABLE orders AS SELECT   (random() * 1e3)::integer client_id        -- \u043e\u0434\u0438\u043d \u0438\u0437 1K \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 , now()::date - (random() * 1e3)::integer dt -- \u0434\u0430\u0442\u0430 \u0437\u0430\u043a\u0430\u0437\u0430 , (random() * 1e3)::integer order_data       -- \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0437\u0430\u043a\u0430\u0437\u0443 FROM   generate_series(1, 1e5); -- 100K \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0445<\/code><\/pre>\n<p>\u041a\u0430\u043a \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e &#171;<em>\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0437\u0430\u043a\u0430\u0437 \u043f\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443<\/em>&#187; \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e <code>(client_id, dt)<\/code>:<\/p>\n<pre><code class=\"sql\">CREATE INDEX ON orders(client_id, dt);<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u0412\u043e\u0442 \u0441 \u044d\u0442\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u043f\u043e\u043b\u0435\u0442\u0438\u0442!..<\/figcaption><\/figure>\n<h2>max + JOIN = 2 x Seq Scan<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430 \u0443\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0441\u0430\u043c\u044b\u0439 &#171;\u0442\u0443\u043f\u043e\u0439&#187; \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#8212; \u043d\u0430\u0439\u0442\u0438 \u044d\u0442\u0438 \u0441\u0430\u043c\u044b\u0435 &#171;\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435&#187; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0439, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   (     SELECT       client_id     , max(dt) dt     FROM       orders     GROUP BY       client_id   ) T JOIN   orders     USING(client_id, dt);<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u0430\u0435\u0442 \u0432\u043e\u0442 <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/0fe767269ae0d94efb63ee552e63ffb9:0:2023-01-13\">\u0442\u0430\u043a\u043e\u0439 \u043f\u043b\u0430\u043d<\/a> \u043d\u0430 <strong>47\u043c\u0441\/1082 buffers<\/strong>, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 <code>orders<\/code> \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b:<\/p>\n<figure class=\"full-width\"><figcaption>\u0414\u0432\u043e\u0439\u043d\u043e\u0439 Seq Scan<\/figcaption><\/figure>\n<figure class=\"full-width\"><figcaption>&#171;\u0414\u0432\u0430 \u0440\u0430\u0437\u0430, \u041a\u0430\u0440\u043b, \u0434\u0432\u0430 \u0440\u0430\u0437\u0430!&#187;&#8230; \u0445\u043e\u0442\u044f, \u044d\u0442\u043e \u043d\u0435 \u043e\u0442\u0441\u044e\u0434\u0430<\/figcaption><\/figure>\n<h2>DISTINCT ON<\/h2>\n<p>\u041d\u043e \u0432\u0440\u043e\u0434\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0441\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u0432\u0430\u0436\u0434\u044b &#8212; \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e <code>client_id<\/code>, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 <code>DISTINCT ON<\/code>:<\/p>\n<pre><code class=\"sql\">SELECT DISTINCT ON(client_id)   * FROM   orders ORDER BY   client_id, dt DESC;<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u0417\u0430\u043b\u0435\u0437\u043b\u0438 \u0432 temp buffers<\/figcaption><\/figure>\n<p>\u041d\u0435\u0437\u0430\u0434\u0430\u0447\u0430&#8230; <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/057356eeaf7cadfc576d1935af78025d:0:2023-01-13\">\u0441\u0442\u0430\u043b\u043e \u0432\u0434\u0432\u043e\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435<\/a> \u0438\u0437-\u0437\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u0434\u0438\u0441\u043a.<\/p>\n<figure class=\"full-width\"><figcaption>\u041f\u0443\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0442\u0435\u0440\u043d\u0438\u0441\u0442 \u0438 \u043e\u043f\u0430\u0441\u0435\u043d<\/figcaption><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432 \u043e\u0431\u044a\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/13\/runtime-config-resource#GUC-WORK-MEM\">work_mem<\/a>, \u043a\u0430\u043a \u0441\u043e\u0432\u0435\u0442\u0443\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0430 \u043d\u0430 <a href=\"https:\/\/explain.tensor.ru\/\">explain.tensor.ru<\/a>:<\/p>\n<pre><code class=\"sql\">SET work_mem = '8MB';<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u0422\u0435\u043f\u0435\u0440\u044c work_mem \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0434\u043b\u044f Sort Memory<\/figcaption><\/figure>\n<figure class=\"full-width\"><figcaption>\u0417\u0430\u0431\u0440\u0435\u0437\u0436\u0438\u043b \u0441\u0432\u0435\u0442 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0442\u0443\u043d\u043d\u0435\u043b\u044f<\/figcaption><\/figure>\n<h2>\u041f\u043e\u043c\u043d\u0438 \u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435!<\/h2>\n<p>\u041d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: <code>Sort Key: client_id, dt DESC<\/code>  &#8212; \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430-\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u043c \u043d\u0430\u043c\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438&#8230;<\/p>\n<pre><code class=\"sql\">CREATE INDEX ON orders(client_id, dt DESC);<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441<\/figcaption><\/figure>\n<p>\u041f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/cfb8b504db0718ce68ce42ff18c3447c:0:2023-01-13\">\u0441\u0440\u0430\u0432\u043d\u044f\u043b\u0438\u0441\u044c \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c<\/a>, \u043d\u043e \u0432\u043e\u0442 buffers \u0442\u0435\u043f\u0435\u0440\u044c <strong>\u0432 100 \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435<\/strong>!<\/p>\n<figure class=\"full-width\"><figcaption>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0433\u043e\u0440\u0430\u0435\u0442<\/figcaption><\/figure>\n<h2>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 &#171;DISTINCT&#187;<\/h2>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0437\u0430\u043a\u0430\u0437\u043e\u0432, \u0434\u0430 \u0438 \u0432 \u043e\u0431\u0449\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u0438\u0441\u043a\u0430 &#171;\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445&#187; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <code>client_id<\/code> \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u0430:<\/p>\n<pre><code class=\"sql\">WITH RECURSIVE rec AS (   SELECT     -1 client_id                  -- \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u043a\u043b\u044e\u0447 \u0438\u043d\u0434\u0435\u043a\u0441\u0430   , NULL::orders r UNION ALL   SELECT     (X).client_id   , X   FROM     rec   , LATERAL (       SELECT         X                         -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0446\u0435\u043b\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b       FROM         orders X       WHERE         client_id > rec.client_id -- \u0448\u0430\u0433\u0430\u0435\u043c \u043a \"\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443\" \u043a\u043b\u0438\u0435\u043d\u0442\u0443       ORDER BY         client_id, dt DESC        -- \u043f\u043e\u043c\u043d\u0438\u043c \u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435       LIMIT 1                     -- \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c     ) T ) SELECT   (r).*   -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u044b FROM   rec OFFSET 1; -- \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 DISTINCT<\/figcaption><\/figure>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0441\u0442\u0430\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0437\u0430\u0442\u043e <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/86cedc8f518b175f26b59d4f7bea724c:0:2023-01-13#explain\">\u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442<\/a> \u0443\u0436\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435 \u0432 \u043f\u043b\u0430\u043d\u0435 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438: 9.5\u043c\u0441\/3008 buffers &#8212; <strong>\u0432 5 \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/strong> \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430!<\/p>\n<figure class=\"full-width\"><figcaption>\u0422\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u043c\u044b &#8212; \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u044b!<\/figcaption><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><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\/company\/tensor\/blog\/710400\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/710400\/<\/a><br \/><\/br><\/br><\/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-343896","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/343896","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=343896"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/343896\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}