{"id":345749,"date":"2023-02-21T09:03:23","date_gmt":"2023-02-21T09:03:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345749"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2023-02-21T09:03:23","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345749","title":{"rendered":"<span>SQL HowTo: \u043a\u0440\u0443\u043f\u0438\u0446\u044b \u0437\u043e\u043b\u043e\u0442\u0430 \u0432 \u0440\u0435\u0435\u0441\u0442\u0440\u0435<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/911\/1f4\/765\/9111f4765bb4e21f925c59a55ec2db7f.jpeg\" width=\"770\" height=\"416\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/911\/1f4\/765\/9111f4765bb4e21f925c59a55ec2db7f.jpeg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412\u00a0\u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0443\u0447\u0435\u0442\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u0442\u0438\u043f\u0430 <a href=\"https:\/\/sbis.ru\/all_services\">\u043d\u0430\u0448\u0435\u0433\u043e \u0421\u0411\u0418\u0421<\/a>, \u0440\u0430\u043d\u043e \u0438\u043b\u0438\u00a0\u043f\u043e\u0437\u0434\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 <strong>\u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0440\u0435\u0435\u0441\u0442\u0440\u0430<\/strong>, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u00a0\u043f\u0440\u043e\u0441\u044c\u0431\u0430\u043c \u0431\u0438\u0437\u043d\u0435\u0441\u2011\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430\u043a\u0440\u0443\u0447\u0435\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u0441\u00a0\u043e\u0447\u0435\u043d\u044c \u0440\u0435\u0434\u043a\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u043e\u0439, \u043d\u0443 \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u00a0\u043b\u043e\u0436\u0430\u0449\u0438\u0445\u0441\u044f \u0432\u00a0\u0432\u0430\u0448\u0443 \u043a\u0440\u0430\u0441\u0438\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0440\u0435\u0435\u0441\u0442\u0440\u0430\u00a0\u2014 \u0447\u0442\u043e\u2011\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0438\u043f\u0430 \u00ab<em>\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e\u043a\u0443\u043f\u0430\u0442\u0435\u043b\u044f\u043c, \u0447\u0435\u0439 \u0434\u0435\u043d\u044c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 29\u00a0\u0444\u0435\u0432\u0440\u0430\u043b\u044f<\/em>\u00ab.<\/p>\n<p>\u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u00ab\u0445\u043e\u0440\u043e\u0448\u043e\u00bb \u0442\u0443\u0442 \u043d\u0435\u0442, \u043d\u043e\u00a0\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e\u00a0\u043c\u043e\u0434\u0435\u043b\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0430\u043c <strong>\u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e\u00a0\u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043e\u0442\u043a\u043b\u0438\u043a<\/strong>, \u043d\u043e\u00a0\u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u0432\u0435\u0441\u044c\u043c\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0441\u00a0\u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f PostgreSQL.<\/p>\n<hr\/>\n<p>\u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u043e, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439 \u043f\u043e\u00a0\u0440\u0435\u0435\u0441\u0442\u0440\u0443:<\/p>\n<ul>\n<li>\n<p>\u0432\u044b\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0435 (\u0437\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0434\u043b\u044f \u00ab\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e\u00bb \u043f\u0435\u0439\u0434\u0436\u0438\u043d\u0433\u0430);<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u00ab\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443\u00bb \u0441\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>OFFSET<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0447\u0438\u0442\u0430\u0442\u044c \u0431\u043b\u043e\u043a \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u00ab\u043f\u043e \u043a\u0443\u0440\u0441\u043e\u0440\u0443\u00bb.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0438\u0445 \u043e\u0431\u0445\u043e\u0434\u0430 \u044f \u0443\u0436\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b \u0432 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 <a href=\"\/ru\/post\/498740\/\">&#171;PostgreSQL Antipatterns: \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u043f\u043e \u0440\u0435\u0435\u0441\u0442\u0440\u0443&#187;<\/a> \u0438 <a href=\"\/ru\/post\/520294\/\">&#171;PostgreSQL 13: happy pagination WITH TIES&#187;<\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u043c\u0438 \u0432\u0441\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0438 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 &#171;\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438&#187; \u0442\u0430\u043c \u0434\u0430\u0436\u0435 \u0431\u043b\u0438\u0437\u043a\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0442\u043e \u0441 \u0442\u0440\u0435\u0442\u044c\u0438\u043c \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<hr\/>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0442 \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0431\u0430\u0437\u044b:<\/p>\n<pre><code class=\"sql\">CREATE TABLE client(   client_id     serial       PRIMARY KEY , client_dt     date );  CREATE TABLE sale(   sale_id     serial       PRIMARY KEY , sale_dt     date , client_id     integer       REFERENCES client         ON DELETE RESTRICT );<\/code><\/pre>\n<p>\u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e\u00a0\u0442\u0443\u0442 \u043d\u0435\u00a0\u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0434\u043b\u044f\u00a0\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>foreign key(client_id)<\/code>, \u0438 \u0432\u00a0\u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u0431\u0435\u0437\u00a0\u043d\u0435\u0433\u043e <a href=\"\/ru\/post\/665118\/\">\u043c\u043e\u0436\u043d\u043e \u0445\u043b\u0435\u0431\u043d\u0443\u0442\u044c \u0433\u043e\u0440\u044f<\/a>, \u043d\u043e\u00a0\u0434\u043b\u044f\u00a0\u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u043d \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0438 \u043c\u044b \u043d\u0435\u00a0\u0441\u0442\u0430\u043d\u0435\u043c \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0410\u00a0\u0432\u043e\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f\u00a0\u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0432\u00a0\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u0445\u0440\u043e\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043d\u0430\u043c \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"sql\">-- \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e \u0440\u0435\u0435\u0441\u0442\u0440\u0443 CREATE UNIQUE INDEX ON sale(sale_dt DESC, sale_id DESC);<\/code><\/pre>\n<p>\u041d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u043d\u0430\u0448\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438:<\/p>\n<pre><code class=\"sql\">INSERT INTO client(client_dt) SELECT (now() - random() * '10 year'::interval)::date FROM generate_series(1, 1e5); -- 100K \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432  INSERT INTO sale(client_id, sale_dt) SELECT (random() * (1e5 - 1))::integer + 1 ,(now() - random() * '10 year'::interval)::date FROM generate_series(1, 1e6); -- 1M \u043f\u0440\u043e\u0434\u0430\u0436 \u0437\u0430 10 \u043b\u0435\u0442<\/code><\/pre>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u043c \u0432\u044b\u0448\u0435 \u0441\u0442\u0430\u0442\u044c\u044f\u043c \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e\u00a0\u043d\u0430\u0447\u0438\u0442\u043a\u0430 \u0431\u043b\u043e\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u00a0\u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0441\u044c\u043c\u0430\u00a0\u0431\u044b\u0441\u0442\u0440\u043e\u0439:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   sale WHERE   (sale_dt, sale_id) &lt; ($1, $2) -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0448\u0430\u0433\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f ORDER BY   sale_dt DESC, sale_id DESC LIMIT 25;<\/code><\/pre>\n<h2>\u0418\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0435<\/h2>\n<p>\u0412\u043e\u00a0\u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043f\u0440\u0438 <a href=\"\/ru\/post\/491162\/\">\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435<\/a> \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a\u00a0\u043b\u044e\u0431\u0438\u043c\u043e\u043c\u0443 \u043c\u043d\u043e\u0433\u0438\u043c\u0438 ORM \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0443 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0438 \u043d\u0430\u0431\u043e\u0440\u0430 <code>id<\/code> \u043c\u0435\u0436\u0434\u0443 \u0431\u0430\u0437\u043e\u0439 \u0438 \u0431\u0438\u0437\u043d\u0435\u0441\u2011\u043b\u043e\u0433\u0438\u043a\u043e\u0439:<\/p>\n<pre><code class=\"javascript\">ids &lt;- `SELECT id FROM ... LIMIT 25` key &lt;- last(ids) res &lt;- `SELECT * FROM ... WHERE id IN (${ids.join(',')}) AND &lt;cond>`<\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043a\u0435\u0439\u0441 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432\u00a0\u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"\/ru\/post\/573214\/\">\u00abPostgreSQL Antipatterns: \u201e\u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0437\u043e\u043b\u043e\u0442\u0430\u201c\u00bb<\/a>, \u0438 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0430\u0441\u044c\u00a0\u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e\u00a0\u2014 \u0432\u043d\u0435\u0441\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u00a0\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0435\u0439 \u0432\u00a0\u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0435\u0441\u043b\u0438\u00a0\u0431\u044b \u043d\u0435\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u043b\u044f\u00a0\u0447\u0442\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 <strong>\u00ab\u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439\u00bb \u043a\u043b\u044e\u0447<\/strong>.<\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0440\u0435\u0435\u0441\u0442\u0440\u0430 \u0432\u00a0\u0442\u0430\u043a\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u043d\u0430\u00a0\u0411\u041b \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 \u0434\u043e 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 (\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043b\u0435\u0437\u0430\u0435\u0442 \u043d\u0430\u00a0\u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043a\u0440\u0430\u043d \u0431\u0435\u0437\u00a0\u0441\u043a\u0440\u043e\u043b\u043b\u0430) = ~<strong>30\u043c\u0441<\/strong>\u00a0\u2014 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442\u00a0\u043b\u0430\u0433\u043e\u0432 \u043d\u0430\u00a0\u0441\u0435\u0442\u0438 \u043e\u0442\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043e\u00a0\u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u0411\u041b \u0442\u0440\u0430\u0442\u0438\u0442 <strong>10\u043c\u0441<\/strong> \u043d\u0430\u00a0\u0432\u044b\u0447\u0438\u0442\u043a\u0443 \u0431\u043b\u043e\u043a\u0430 \u0438\u0437 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0435 \u00ab\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430\u00bb<\/p>\n<\/li>\n<li>\n<p>\u0411\u041b \u0442\u0440\u0430\u0442\u0438\u0442 \u0435\u0449\u0435 <strong>10\u043c\u0441<\/strong> \u043d\u0430\u00a0\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043f\u043e\u00a0\u0411\u0414 \u0434\u043b\u044f 25\u00a0\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0440\u0430\u043d\u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432<\/p>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043a\u043e\u043c\u044b\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u00a0\u0447\u0430\u0441\u0442\u043e\u0442\u043e\u0439 1:1000, \u0442\u043e \u0434\u043b\u044f\u00a0\u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043a\u0440\u0430\u043d\u0430 \u043d\u0430 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0430 1000\u00a0\u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u0432 \u043d\u0430\u00a0\u044d\u0442\u043e (30ms + 10ms + 10ms) x 1000\u00a0= <strong>50\u00a0\u0441\u0435\u043a\u0443\u043d\u0434<\/strong>.<\/p>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c\u00a0\u0431\u044b, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u00a0\u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u00a0\u0411\u041b \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043d\u0435\u00a0\u043f\u043e 25, \u0430\u00a0\u043f\u043e 100\u00a0\u0438\u043b\u0438\u00a0\u0434\u0430\u0436\u0435 1000\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439! \u041d\u043e\u00a0\u0432\u00a0\u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0440\u0438\u0441\u043a\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u2011\u0442\u0430\u043a\u0438 \u0432\u0441\u0435 \u044d\u0442\u0438 1000\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 (\u0435\u0441\u043b\u0438 \u0444\u0438\u043b\u044c\u0442\u0440 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043d\u0435\u00a0\u0442\u0430\u043a\u043e\u0439 \u0443\u0436 \u0440\u0435\u0434\u043a\u0438\u0439) \u0441\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0432\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435\u00a0\u2014 \u0430 <strong>\u044d\u0442\u043e \u0434\u043e\u043b\u0433\u043e<\/strong>!<\/p>\n<h2>\u0418\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u043f\u043e\u043d\u044f\u0442\u043d\u0430\u00a0\u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u00ab\u043f\u0440\u0438\u0437\u0435\u043c\u043b\u0438\u0442\u044c\u00bb \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0430\u043a\u00a0\u043c\u043e\u0436\u043d\u043e \u0431\u043b\u0438\u0436\u0435 \u043a\u00a0\u0434\u0430\u043d\u043d\u044b\u043c. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0430\u043c\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c\u0438 \u0432\u00a0\u0441\u0442\u0430\u0442\u044c\u044f\u0445 <a href=\"\/ru\/post\/486072\/\">\u00abSQL HowTo: \u043f\u0438\u0448\u0435\u043c while\u2011\u0446\u0438\u043a\u043b \u043f\u0440\u044f\u043c\u043e \u0432\u00a0\u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0438\u043b\u0438 \u201e\u042d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u0430\u044f \u0442\u0440\u0435\u0445\u0445\u043e\u0434\u043e\u0432\u043a\u0430\u201c\u00bb<\/a> \u0438 <a href=\"\/ru\/post\/676784\/\">\u00abSQL HowTo: \u043d\u0430\u043f\u0435\u0440\u0435\u0433\u043e\u043d\u043a\u0438 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c\u00bb<\/a>.<\/p>\n<p>\u0412\u00a0\u0446\u0435\u043b\u043e\u043c, \u0447\u0442\u043e\u00a0\u043c\u044b \u0445\u043e\u0442\u0438\u043c:<\/p>\n<ul>\n<li>\n<p>\u043a\u0430\u0436\u0434\u0430\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u00ab\u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f\u00bb \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430\u00a0\u0431\u044b\u0442\u044c <strong>\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430 \u043f\u043e\u00a0\u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/strong>\u00a0\u2014 \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u00a0\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c\u0441\u044f \u0434\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0441\u0438\u0445\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u0433\u043e\u0442\u043e\u0432 \u0436\u0434\u0430\u0442\u044c \u0434\u043e\u00a0\u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0435\u0441\u0442\u044c\u00a0\u2014 \u0441\u043a\u0430\u0436\u0435\u043c, 100\u043c\u0441<\/p>\n<\/li>\n<li>\n<p>\u0442\u0430\u043a\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <strong>\u043d\u0435\u00a0\u0441\u0438\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u043d\u043e\u0433\u043e\u00a0\u043b\u0438\u043c\u0438\u0442\u0430<\/strong> \u0437\u0430\u043f\u0438\u0441\u0435\u0439 (\u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0445\u043e\u0442\u044f\u00a0\u0431\u044b \u0432 2x)<\/p>\n<\/li>\n<li>\n<p>\u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c <strong>\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043f\u043e\u0438\u0441\u043a\u0430<\/strong> \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0434\u0430\u0442\u043e\u0439\u00a0\u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<h4>\u0412\u043e\u0437\u0432\u0440\u0430\u0442 &#171;\u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e&#187; \u043a\u043b\u044e\u0447\u0430<\/h4>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0440\u0430\u0437 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u00a0\u0431\u0430\u0437\u0435 \u043a\u0430\u043a\u0438\u0435\u2011\u0442\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043d\u043e\u00a0\u0432\u00a0SQL\u2011\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u044d\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432\u00a0\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443:<\/p>\n<pre><code class=\"sql\">WITH RECURSIVE R AS (   ... -- \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 JSON \u0441 \u043a\u043b\u044e\u0447\u043e\u043c \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 UNION ALL   ... -- \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e \u0431\u0430\u0437\u0435 ) ( -- \u043f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 resultset - \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   ... ) UNION ALL ( -- \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 - \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442   ... );<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u0432 \u043d\u0430\u0448\u0435\u043c resultset \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u043b\u043e\u043a\u0430 &#171;\u0434\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438&#187;, \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 &#8212; \u0443\u0436\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u0435\u0433\u043c\u0435\u043d\u0442.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u043b\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 CTE:<\/p>\n<ul>\n<li>\n<p><code>i<\/code> &#8212; \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p><code>k<\/code> &#8212; \u043a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p><code>a<\/code> &#8212; \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439;<\/p>\n<\/li>\n<li>\n<p><code>s<\/code> &#8212; \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u043e\u0433\u0434\u0430 \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u0448\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432\u043e\u0442 \u0432 \u0442\u0430\u043a\u0443\u044e:<\/p>\n<pre><code class=\"sql\">WITH RECURSIVE R AS (   SELECT     -- \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     0 i     -- \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   , json_populate_record(       -- \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c       NULL::sale       -- \u0441\u044e\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c json-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c $1 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     , '{\"sale_dt\" : \"infinity\", \"sale_id\" : 0}'::json     ) k     -- \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , '{}'::sale[] a     -- \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , 0 s UNION ALL   ... ) ( -- \u043f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 resultset - \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   SELECT     (k).* -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0435-\u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b   FROM     R   ORDER BY     i DESC -- \u043a\u043b\u044e\u0447 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   LIMIT 1 ) UNION ALL ( -- \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 - \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442   SELECT     (unnest(a)).* -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0430 \u0437\u0430\u0442\u0435\u043c - \u0437\u0430\u043f\u0438\u0441\u0438   FROM     R );<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0435, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0441\u0445\u0435\u043c\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0441\u0442\u0440\u043e\u0438\u043c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/833\/f9b\/9b3\/833f9b9b32b34a98c067f4275e7af272.png\" alt=\"\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430\" title=\"\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430\" width=\"876\" height=\"1001\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/833\/f9b\/9b3\/833f9b9b32b34a98c067f4275e7af272.png\"\/><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/figcaption><\/figure>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0433 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438 \u0435\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0420\u0430\u0437 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u0430\u043a\u00a0\u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0437\u0430\u00a0\u043e\u0434\u043d\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u0443\u0436\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0442\u0430\u043a \u0438 <a href=\"\/ru\/post\/676784\/\">\u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/a>, \u043e\u0431\u0430 \u044d\u0442\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e:<\/p>\n<pre><code class=\"sql\">  WHERE     -- limit time     clock_timestamp() &lt; statement_timestamp() + '100 msec'::interval AND     -- limit resultset     R.s &lt; 25 -- pageLimit<\/code><\/pre>\n<p>\u0418, \u0432\u043e\u0442 \u043e\u043d, \u0441\u0430\u043c\u044b\u0439 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f &#171;\u043c\u0430\u0433\u0438\u044f&#187;:<\/p>\n<pre><code class=\"sql\">  SELECT     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439     R.i + 1     -- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u044e\u0447   , T.kn     -- \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0431\u043b\u043e\u043a \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , T.a     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e   , R.s + coalesce(array_length(T.a, 1), 0)   FROM     R   , LATERAL (       -- \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0438\u0437 pageLimit \u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       WITH Ts AS (         SELECT           T         FROM           sale T         WHERE           (sale_dt, sale_id) &lt; ((k).sale_dt, (k).sale_id) -- !!!         ORDER BY           sale_dt DESC, sale_id DESC -- !!! \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u0443         LIMIT 25 -- pageLimit       )       SELECT         (           -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 - \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u044e\u0447           TABLE Ts OFFSET 24 -- pageLimit - 1         ) kn         -- \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       , ARRAY(           SELECT             T           FROM             Ts           WHERE             -- \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432             (T).client_id = ANY(ARRAY(               SELECT                 client_id               FROM                 client               WHERE                 client_id = ANY(ARRAY( -- \u0441\u0432\u0435\u0440\u0442\u043a\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432                   SELECT                     (T).client_id                   FROM                     Ts                 )) AND                 client_dt::text ~ '-02-29$' -- \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438             ))         ) a     ) T<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">WITH RECURSIVE R AS (   SELECT     -- \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     0 i     -- \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   , json_populate_record(       -- \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c       NULL::sale       -- \u0441\u044e\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c json-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c $1 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     , '{\"sale_dt\" : \"infinity\", \"sale_id\" : 0}'::json     ) k     -- \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , '{}'::sale[] a     -- \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , 0 s UNION ALL   SELECT     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439     R.i + 1     -- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u044e\u0447   , T.kn     -- \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0431\u043b\u043e\u043a \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , T.a     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e   , R.s + coalesce(array_length(T.a, 1), 0)   FROM     R   , LATERAL (       -- \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0438\u0437 pageLimit \u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       WITH Ts AS (         SELECT           T         FROM           sale T         WHERE           (sale_dt, sale_id) &lt; ((k).sale_dt, (k).sale_id) -- !!!         ORDER BY           sale_dt DESC, sale_id DESC -- !!!         LIMIT 25 -- pageLimit       )       SELECT         (           -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 - \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u044e\u0447           TABLE Ts OFFSET 24 -- pageLimit - 1         ) kn         -- \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       , ARRAY(           SELECT             T           FROM             Ts           WHERE             -- \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432             (T).client_id = ANY(ARRAY(               SELECT                 client_id               FROM                 client               WHERE                 client_id = ANY(ARRAY( -- \u0441\u0432\u0435\u0440\u0442\u043a\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432                   SELECT                     (T).client_id                   FROM                     Ts                 )) AND                 client_dt::text ~ '-02-29$' -- \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438             ))         ) a     ) T   WHERE     -- limit time     clock_timestamp() &lt; statement_timestamp() + '100 msec'::interval AND     -- limit resultset     R.s &lt; 25 -- pageLimit ) ( -- \u043f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 resultset - \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   SELECT     (k).* -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0435-\u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b   FROM     R   ORDER BY     i DESC -- \u043a\u043b\u044e\u0447 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   LIMIT 1 ) UNION ALL ( -- \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 - \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442   SELECT     (unnest(a)).* -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0430 \u0437\u0430\u0442\u0435\u043c - \u0437\u0430\u043f\u0438\u0441\u0438   FROM     R );<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c <a href=\"https:\/\/explain.tensor.ru\/archive\/explain\/298d4d3673e3598582a2445d9964d400:0:2023-02-21\">\u043f\u043b\u0430\u043d \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/a> \u0434\u043b\u044f \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430 \u043d\u0430\u0448\u0438\u0445 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1ae\/635\/d20\/1ae635d20404b8311956c2db4d61354e.png\" alt=\"\u041f\u043b\u0430\u043d \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430\" title=\"\u041f\u043b\u0430\u043d \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430\" width=\"1077\" height=\"870\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1ae\/635\/d20\/1ae635d20404b8311956c2db4d61354e.png\"\/><figcaption>\u041f\u043b\u0430\u043d \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/figcaption><\/figure>\n<p>\u041a\u0430\u043a\u00a0\u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 <strong>\u043e\u0442\u0441\u0435\u0447\u043a\u0430 \u043f\u043e\u00a0\u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0443<\/strong> \u043d\u0430 100ms, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0443\u0436\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 10\u00a0\u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u00ab\u0432\u043d\u0443\u0442\u0440\u0438\u00bb PostgreSQL \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u0442\u044c <strong>637\u00a0\u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439<\/strong> \u0432\u044b\u0447\u0438\u0442\u043a\u0438\u2011\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a\u00a0\u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0439 \u043e\u0446\u0435\u043d\u043a\u0435, \u0447\u0442\u043e\u00a0\u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043b\u0438\u0442\u0441\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 50ms, \u0442\u043e 637\u00a0\u2014 <strong>\u043e\u043a\u043e\u043b\u043e 30\u00a0\u0441\u0435\u043a\u0443\u043d\u0434<\/strong>!<\/p>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e\u00a0\u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e\u00a0\u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e <strong>\u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0438 \u0432 300\u00a0\u0440\u0430\u0437!<\/strong><\/p>\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\/718092\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/718092\/<\/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<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u00a0\u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0443\u0447\u0435\u0442\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u0442\u0438\u043f\u0430 <a href=\"https:\/\/sbis.ru\/all_services\">\u043d\u0430\u0448\u0435\u0433\u043e \u0421\u0411\u0418\u0421<\/a>, \u0440\u0430\u043d\u043e \u0438\u043b\u0438\u00a0\u043f\u043e\u0437\u0434\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 <strong>\u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0440\u0435\u0435\u0441\u0442\u0440\u0430<\/strong>, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u00a0\u043f\u0440\u043e\u0441\u044c\u0431\u0430\u043c \u0431\u0438\u0437\u043d\u0435\u0441\u2011\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430\u043a\u0440\u0443\u0447\u0435\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u0441\u00a0\u043e\u0447\u0435\u043d\u044c \u0440\u0435\u0434\u043a\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u043e\u0439, \u043d\u0443 \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u00a0\u043b\u043e\u0436\u0430\u0449\u0438\u0445\u0441\u044f \u0432\u00a0\u0432\u0430\u0448\u0443 \u043a\u0440\u0430\u0441\u0438\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0440\u0435\u0435\u0441\u0442\u0440\u0430\u00a0\u2014 \u0447\u0442\u043e\u2011\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0438\u043f\u0430 \u00ab<em>\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u043e\u0434\u0430\u0436 \u043f\u043e\u043a\u0443\u043f\u0430\u0442\u0435\u043b\u044f\u043c, \u0447\u0435\u0439 \u0434\u0435\u043d\u044c \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 29\u00a0\u0444\u0435\u0432\u0440\u0430\u043b\u044f<\/em>\u00ab.<\/p>\n<p>\u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u00ab\u0445\u043e\u0440\u043e\u0448\u043e\u00bb \u0442\u0443\u0442 \u043d\u0435\u0442, \u043d\u043e\u00a0\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u0440\u043e\u00a0\u043c\u043e\u0434\u0435\u043b\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0430\u043c <strong>\u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e\u00a0\u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043e\u0442\u043a\u043b\u0438\u043a<\/strong>, \u043d\u043e\u00a0\u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u0432\u0435\u0441\u044c\u043c\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0441\u00a0\u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f PostgreSQL.<\/p>\n<hr\/>\n<p>\u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u043e, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439 \u043f\u043e\u00a0\u0440\u0435\u0435\u0441\u0442\u0440\u0443:<\/p>\n<ul>\n<li>\n<p>\u0432\u044b\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0435 (\u0437\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0434\u043b\u044f \u00ab\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e\u00bb \u043f\u0435\u0439\u0434\u0436\u0438\u043d\u0433\u0430);<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u00ab\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443\u00bb \u0441\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>OFFSET<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0447\u0438\u0442\u0430\u0442\u044c \u0431\u043b\u043e\u043a \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u00ab\u043f\u043e \u043a\u0443\u0440\u0441\u043e\u0440\u0443\u00bb.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0438\u0445 \u043e\u0431\u0445\u043e\u0434\u0430 \u044f \u0443\u0436\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b \u0432 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 <a href=\"\/ru\/post\/498740\/\">&#171;PostgreSQL Antipatterns: \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u043f\u043e \u0440\u0435\u0435\u0441\u0442\u0440\u0443&#187;<\/a> \u0438 <a href=\"\/ru\/post\/520294\/\">&#171;PostgreSQL 13: happy pagination WITH TIES&#187;<\/a>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u043c\u0438 \u0432\u0441\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0438 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 &#171;\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438&#187; \u0442\u0430\u043c \u0434\u0430\u0436\u0435 \u0431\u043b\u0438\u0437\u043a\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0442\u043e \u0441 \u0442\u0440\u0435\u0442\u044c\u0438\u043c \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<hr\/>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0442 \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0431\u0430\u0437\u044b:<\/p>\n<pre><code class=\"sql\">CREATE TABLE client(   client_id     serial       PRIMARY KEY , client_dt     date );  CREATE TABLE sale(   sale_id     serial       PRIMARY KEY , sale_dt     date , client_id     integer       REFERENCES client         ON DELETE RESTRICT );<\/code><\/pre>\n<p>\u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e\u00a0\u0442\u0443\u0442 \u043d\u0435\u00a0\u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0434\u043b\u044f\u00a0\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>foreign key(client_id)<\/code>, \u0438 \u0432\u00a0\u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u0431\u0435\u0437\u00a0\u043d\u0435\u0433\u043e <a href=\"\/ru\/post\/665118\/\">\u043c\u043e\u0436\u043d\u043e \u0445\u043b\u0435\u0431\u043d\u0443\u0442\u044c \u0433\u043e\u0440\u044f<\/a>, \u043d\u043e\u00a0\u0434\u043b\u044f\u00a0\u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u043d \u043d\u0435\u00a0\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0438 \u043c\u044b \u043d\u0435\u00a0\u0441\u0442\u0430\u043d\u0435\u043c \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0410\u00a0\u0432\u043e\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f\u00a0\u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0432\u00a0\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u0445\u0440\u043e\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043d\u0430\u043c \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"sql\">-- \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e \u0440\u0435\u0435\u0441\u0442\u0440\u0443 CREATE UNIQUE INDEX ON sale(sale_dt DESC, sale_id DESC);<\/code><\/pre>\n<p>\u041d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u043d\u0430\u0448\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438:<\/p>\n<pre><code class=\"sql\">INSERT INTO client(client_dt) SELECT (now() - random() * '10 year'::interval)::date FROM generate_series(1, 1e5); -- 100K \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432  INSERT INTO sale(client_id, sale_dt) SELECT (random() * (1e5 - 1))::integer + 1 ,(now() - random() * '10 year'::interval)::date FROM generate_series(1, 1e6); -- 1M \u043f\u0440\u043e\u0434\u0430\u0436 \u0437\u0430 10 \u043b\u0435\u0442<\/code><\/pre>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u043c \u0432\u044b\u0448\u0435 \u0441\u0442\u0430\u0442\u044c\u044f\u043c \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e\u00a0\u043d\u0430\u0447\u0438\u0442\u043a\u0430 \u0431\u043b\u043e\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u00a0\u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0441\u044c\u043c\u0430\u00a0\u0431\u044b\u0441\u0442\u0440\u043e\u0439:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   sale WHERE   (sale_dt, sale_id) &lt; ($1, $2) -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0448\u0430\u0433\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f ORDER BY   sale_dt DESC, sale_id DESC LIMIT 25;<\/code><\/pre>\n<h2>\u0418\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0435<\/h2>\n<p>\u0412\u043e\u00a0\u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043f\u0440\u0438 <a href=\"\/ru\/post\/491162\/\">\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435<\/a> \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a\u00a0\u043b\u044e\u0431\u0438\u043c\u043e\u043c\u0443 \u043c\u043d\u043e\u0433\u0438\u043c\u0438 ORM \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0443 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0438 \u043d\u0430\u0431\u043e\u0440\u0430 <code>id<\/code> \u043c\u0435\u0436\u0434\u0443 \u0431\u0430\u0437\u043e\u0439 \u0438 \u0431\u0438\u0437\u043d\u0435\u0441\u2011\u043b\u043e\u0433\u0438\u043a\u043e\u0439:<\/p>\n<pre><code class=\"javascript\">ids &lt;- `SELECT id FROM ... LIMIT 25` key &lt;- last(ids) res &lt;- `SELECT * FROM ... WHERE id IN (${ids.join(',')}) AND &lt;cond>`<\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043a\u0435\u0439\u0441 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432\u00a0\u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"\/ru\/post\/573214\/\">\u00abPostgreSQL Antipatterns: \u201e\u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0437\u043e\u043b\u043e\u0442\u0430\u201c\u00bb<\/a>, \u0438 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0430\u0441\u044c\u00a0\u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e\u00a0\u2014 \u0432\u043d\u0435\u0441\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u00a0\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0435\u0439 \u0432\u00a0\u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0435\u0441\u043b\u0438\u00a0\u0431\u044b \u043d\u0435\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u043b\u044f\u00a0\u0447\u0442\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 <strong>\u00ab\u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439\u00bb \u043a\u043b\u044e\u0447<\/strong>.<\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0440\u0435\u0435\u0441\u0442\u0440\u0430 \u0432\u00a0\u0442\u0430\u043a\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u043d\u0430\u00a0\u0411\u041b \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 \u0434\u043e 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 (\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043b\u0435\u0437\u0430\u0435\u0442 \u043d\u0430\u00a0\u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043a\u0440\u0430\u043d \u0431\u0435\u0437\u00a0\u0441\u043a\u0440\u043e\u043b\u043b\u0430) = ~<strong>30\u043c\u0441<\/strong>\u00a0\u2014 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442\u00a0\u043b\u0430\u0433\u043e\u0432 \u043d\u0430\u00a0\u0441\u0435\u0442\u0438 \u043e\u0442\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043e\u00a0\u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u0411\u041b \u0442\u0440\u0430\u0442\u0438\u0442 <strong>10\u043c\u0441<\/strong> \u043d\u0430\u00a0\u0432\u044b\u0447\u0438\u0442\u043a\u0443 \u0431\u043b\u043e\u043a\u0430 \u0438\u0437 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0435 \u00ab\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430\u00bb<\/p>\n<\/li>\n<li>\n<p>\u0411\u041b \u0442\u0440\u0430\u0442\u0438\u0442 \u0435\u0449\u0435 <strong>10\u043c\u0441<\/strong> \u043d\u0430\u00a0\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043f\u043e\u00a0\u0411\u0414 \u0434\u043b\u044f 25\u00a0\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0440\u0430\u043d\u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432<\/p>\n<\/li>\n<\/ol>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0441\u043a\u043e\u043c\u044b\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u00a0\u0447\u0430\u0441\u0442\u043e\u0442\u043e\u0439 1:1000, \u0442\u043e \u0434\u043b\u044f\u00a0\u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043a\u0440\u0430\u043d\u0430 \u043d\u0430 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0430 1000\u00a0\u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e 25\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u0432 \u043d\u0430\u00a0\u044d\u0442\u043e (30ms + 10ms + 10ms) x 1000\u00a0= <strong>50\u00a0\u0441\u0435\u043a\u0443\u043d\u0434<\/strong>.<\/p>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c\u00a0\u0431\u044b, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u00a0\u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u00a0\u0411\u041b \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043d\u0435\u00a0\u043f\u043e 25, \u0430\u00a0\u043f\u043e 100\u00a0\u0438\u043b\u0438\u00a0\u0434\u0430\u0436\u0435 1000\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439! \u041d\u043e\u00a0\u0432\u00a0\u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0440\u0438\u0441\u043a\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u2011\u0442\u0430\u043a\u0438 \u0432\u0441\u0435 \u044d\u0442\u0438 1000\u00a0\u0437\u0430\u043f\u0438\u0441\u0435\u0439 (\u0435\u0441\u043b\u0438 \u0444\u0438\u043b\u044c\u0442\u0440 \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043d\u0435\u00a0\u0442\u0430\u043a\u043e\u0439 \u0443\u0436 \u0440\u0435\u0434\u043a\u0438\u0439) \u0441\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0432\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435\u00a0\u2014 \u0430 <strong>\u044d\u0442\u043e \u0434\u043e\u043b\u0433\u043e<\/strong>!<\/p>\n<h2>\u0418\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u043f\u043e\u043d\u044f\u0442\u043d\u0430\u00a0\u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u00ab\u043f\u0440\u0438\u0437\u0435\u043c\u043b\u0438\u0442\u044c\u00bb \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0430\u043a\u00a0\u043c\u043e\u0436\u043d\u043e \u0431\u043b\u0438\u0436\u0435 \u043a\u00a0\u0434\u0430\u043d\u043d\u044b\u043c. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0430\u043c\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c\u0438 \u0432\u00a0\u0441\u0442\u0430\u0442\u044c\u044f\u0445 <a href=\"\/ru\/post\/486072\/\">\u00abSQL HowTo: \u043f\u0438\u0448\u0435\u043c while\u2011\u0446\u0438\u043a\u043b \u043f\u0440\u044f\u043c\u043e \u0432\u00a0\u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0438\u043b\u0438 \u201e\u042d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u0430\u044f \u0442\u0440\u0435\u0445\u0445\u043e\u0434\u043e\u0432\u043a\u0430\u201c\u00bb<\/a> \u0438 <a href=\"\/ru\/post\/676784\/\">\u00abSQL HowTo: \u043d\u0430\u043f\u0435\u0440\u0435\u0433\u043e\u043d\u043a\u0438 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c\u00bb<\/a>.<\/p>\n<p>\u0412\u00a0\u0446\u0435\u043b\u043e\u043c, \u0447\u0442\u043e\u00a0\u043c\u044b \u0445\u043e\u0442\u0438\u043c:<\/p>\n<ul>\n<li>\n<p>\u043a\u0430\u0436\u0434\u0430\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u00ab\u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f\u00bb \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430\u00a0\u0431\u044b\u0442\u044c <strong>\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430 \u043f\u043e\u00a0\u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/strong>\u00a0\u2014 \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u00a0\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c\u0441\u044f \u0434\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0441\u0438\u0445\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u0433\u043e\u0442\u043e\u0432 \u0436\u0434\u0430\u0442\u044c \u0434\u043e\u00a0\u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0435\u0441\u0442\u044c\u00a0\u2014 \u0441\u043a\u0430\u0436\u0435\u043c, 100\u043c\u0441<\/p>\n<\/li>\n<li>\n<p>\u0442\u0430\u043a\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <strong>\u043d\u0435\u00a0\u0441\u0438\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u043d\u043e\u0433\u043e\u00a0\u043b\u0438\u043c\u0438\u0442\u0430<\/strong> \u0437\u0430\u043f\u0438\u0441\u0435\u0439 (\u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0445\u043e\u0442\u044f\u00a0\u0431\u044b \u0432 2x)<\/p>\n<\/li>\n<li>\n<p>\u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c <strong>\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043f\u043e\u0438\u0441\u043a\u0430<\/strong> \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0434\u0430\u0442\u043e\u0439\u00a0\u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<h4>\u0412\u043e\u0437\u0432\u0440\u0430\u0442 &#171;\u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e&#187; \u043a\u043b\u044e\u0447\u0430<\/h4>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0440\u0430\u0437 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u00a0\u0431\u0430\u0437\u0435 \u043a\u0430\u043a\u0438\u0435\u2011\u0442\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043d\u043e\u00a0\u0432\u00a0SQL\u2011\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u044d\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432\u00a0\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443:<\/p>\n<pre><code class=\"sql\">WITH RECURSIVE R AS (   ... -- \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u0441 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 JSON \u0441 \u043a\u043b\u044e\u0447\u043e\u043c \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 UNION ALL   ... -- \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e \u0431\u0430\u0437\u0435 ) ( -- \u043f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 resultset - \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   ... ) UNION ALL ( -- \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 - \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442   ... );<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u0432 \u043d\u0430\u0448\u0435\u043c resultset \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u043b\u043e\u043a\u0430 &#171;\u0434\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438&#187;, \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 &#8212; \u0443\u0436\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u0435\u0433\u043c\u0435\u043d\u0442.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u043b\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 CTE:<\/p>\n<ul>\n<li>\n<p><code>i<\/code> &#8212; \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p><code>k<\/code> &#8212; \u043a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p><code>a<\/code> &#8212; \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439;<\/p>\n<\/li>\n<li>\n<p><code>s<\/code> &#8212; \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u043e\u0433\u0434\u0430 \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u0448\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432\u043e\u0442 \u0432 \u0442\u0430\u043a\u0443\u044e:<\/p>\n<pre><code class=\"sql\">WITH RECURSIVE R AS (   SELECT     -- \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     0 i     -- \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   , json_populate_record(       -- \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c       NULL::sale       -- \u0441\u044e\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c json-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c $1 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     , '{\"sale_dt\" : \"infinity\", \"sale_id\" : 0}'::json     ) k     -- \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , '{}'::sale[] a     -- \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , 0 s UNION ALL   ... ) ( -- \u043f\u0435\u0440\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0432 resultset - \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   SELECT     (k).* -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0435-\u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b   FROM     R   ORDER BY     i DESC -- \u043a\u043b\u044e\u0447 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   LIMIT 1 ) UNION ALL ( -- \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 - \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442   SELECT     (unnest(a)).* -- \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0430 \u0437\u0430\u0442\u0435\u043c - \u0437\u0430\u043f\u0438\u0441\u0438   FROM     R );<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0435, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0441\u0445\u0435\u043c\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0441\u0442\u0440\u043e\u0438\u043c:<\/p>\n<figure class=\"full-width\"><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/figcaption><\/figure>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0433 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438 \u0435\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0420\u0430\u0437 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u0430\u043a\u00a0\u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0437\u0430\u00a0\u043e\u0434\u043d\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u0443\u0436\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0442\u0430\u043a \u0438 <a href=\"\/ru\/post\/676784\/\">\u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/a>, \u043e\u0431\u0430 \u044d\u0442\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e:<\/p>\n<pre><code class=\"sql\">  WHERE     -- limit time     clock_timestamp() &lt; statement_timestamp() + '100 msec'::interval AND     -- limit resultset     R.s &lt; 25 -- pageLimit<\/code><\/pre>\n<p>\u0418, \u0432\u043e\u0442 \u043e\u043d, \u0441\u0430\u043c\u044b\u0439 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f &#171;\u043c\u0430\u0433\u0438\u044f&#187;:<\/p>\n<pre><code class=\"sql\">  SELECT     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439     R.i + 1     -- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u044e\u0447   , T.kn     -- \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0431\u043b\u043e\u043a \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , T.a     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e   , R.s + coalesce(array_length(T.a, 1), 0)   FROM     R   , LATERAL (       -- \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0438\u0437 pageLimit \u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       WITH Ts AS (         SELECT           T         FROM           sale T         WHERE           (sale_dt, sale_id) &lt; ((k).sale_dt, (k).sale_id) -- !!!         ORDER BY           sale_dt DESC, sale_id DESC -- !!! \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u0443         LIMIT 25 -- pageLimit       )       SELECT         (           -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 - \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u044e\u0447           TABLE Ts OFFSET 24 -- pageLimit - 1         ) kn         -- \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       , ARRAY(           SELECT             T           FROM             Ts           WHERE             -- \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432             (T).client_id = ANY(ARRAY(               SELECT                 client_id               FROM                 client               WHERE                 client_id = ANY(ARRAY( -- \u0441\u0432\u0435\u0440\u0442\u043a\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432                   SELECT                     (T).client_id                   FROM                     Ts                 )) AND                 client_dt::text ~ '-02-29$' -- \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438             ))         ) a     ) T<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">WITH RECURSIVE R AS (   SELECT     -- \u043d\u043e\u043c\u0435\u0440 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     0 i     -- \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438   , json_populate_record(       -- \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c       NULL::sale       -- \u0441\u044e\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c json-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c $1 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438     , '{\"sale_dt\" : \"infinity\", \"sale_id\" : 0}'::json     ) k     -- \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , '{}'::sale[] a     -- \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , 0 s UNION ALL   SELECT     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439     R.i + 1     -- \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u044e\u0447   , T.kn     -- \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0431\u043b\u043e\u043a \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439   , T.a     -- \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e   , R.s + coalesce(array_length(T.a, 1), 0)   FROM     R   , LATERAL (       -- \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0438\u0437 pageLimit \u043d\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       WITH Ts AS (         SELECT           T         FROM           sale T         WHERE           (sale_dt, sale_id) &lt; ((k).sale_dt, (k).sale_id) -- !!!         ORDER BY           sale_dt DESC, sale_id DESC -- !!!         LIMIT 25 -- pageLimit       )       SELECT         (           -- \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 - \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u044e\u0447           TABLE Ts OFFSET 24 -- pageLimit - 1         ) kn         -- \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u0430\u043f\u0438\u0441\u0435\u0439       , ARRAY(           SELECT             T           FROM             Ts           WHERE             -- \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043d\u0430\u0431\u043e\u0440\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432             (T).client_id = ANY(ARRAY(               SELECT<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-345749","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345749","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=345749"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345749\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}