{"id":316189,"date":"2021-01-11T15:01:14","date_gmt":"2021-01-11T15:01:14","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=316189"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=316189","title":{"rendered":"SQL HowTo: \u0440\u0435\u0439\u0442\u0438\u043d\u0433-\u0437\u0430-\u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u044b\u0445 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439-&#171;\u0445\u043e\u0442\u0435\u043b\u043e\u043a&#187; \u0431\u0438\u0437\u043d\u0435\u0441\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0432\u0441\u044f\u043a\u0438\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0435\u0439\u0442\u0438\u043d\u0433\u043e\u0432 &#8212;&nbsp;<strong>&#171;\u0441\u0430\u043c\u044b\u0435 \u043e\u0431\u043e\u0440\u043e\u0442\u0438\u0441\u0442\u044b\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u044b&#187;, &#171;\u0441\u0430\u043c\u044b\u0435 \u043f\u0440\u043e\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438&#187;, &#171;\u0441\u0430\u043c\u044b\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438&#187;<\/strong>, \u2026 &#8212; \u043b\u044e\u0431\u0438\u043c\u0430\u044f \u0442\u0435\u043c\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0434\u0430\u0448\u0431\u043e\u0440\u0434\u043e\u0432.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 <a href=\"https:\/\/sbis.ru\/presto\">\u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0435\u0441\u0442\u043e\u0440\u0430\u043d\u043e\u0432 \u0438 \u043a\u0430\u0444\u0435 <strong>Presto<\/strong><\/a> \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u0435\u043d \u0442\u0430\u043a\u043e\u0439:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/940\/ab2\/448\/940ab24489d927be116b3264742a79b8.png\" alt=\"\" title=\"\" width=\"505\" height=\"377\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u043e \u043f\u0440\u043e\u0441\u0442\u043e &#171;\u0441\u0430\u043c\u044b\u0435&#187; \u0437\u0430 \u0432\u0435\u0441\u044c \u0434\u043e\u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0435\u0440\u0438\u043e\u0434 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b &#8212; \u043f\u0440\u043e\u0434\u0430\u043b \u0442\u044b 3 \u0433\u043e\u0434\u0430 \u043d\u0430\u0437\u0430\u0434 \u0432\u0430\u0433\u043e\u043d \u0432\u0430\u043b\u0435\u043d\u043e\u043a, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u0443 \u0442\u0435\u0431\u044f \u0432 &#171;\u0441\u0430\u043c\u044b\u0445&#187; \u043f\u0440\u043e\u0434\u0430\u0436\u0430\u0445 \u0432\u0435\u0447\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0438\u0434\u0435\u0442\u044c&nbsp;<strong>&#171;\u0442\u043e\u043f&#187; \u043d\u0430 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435<\/strong>&nbsp;&#8212; \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, &#171;\u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0433\u043e\u0434&#187; (\u0442\u043e\u0447\u043d\u0435\u0435, \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 12 \u043a\u0430\u043b\u0435\u043d\u0434\u0430\u0440\u043d\u044b\u0445 \u043c\u0435\u0441\u044f\u0446\u0435\u0432).<\/p>\n<p>\u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u043e, \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435: \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044e \u043f\u043e &#171;\u0441\u044b\u0440\u044b\u043c&#187; \u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f. \u0418 \u0435\u0441\u043b\u0438 &#171;\u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c&#187; \u0442\u0430\u043a\u043e\u0439 \u043e\u0442\u0447\u0435\u0442 \u043f\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043a\u0435 &#8212; \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0435 \u0434\u043b\u044f SQL-\u043d\u043e\u0432\u0438\u0447\u043a\u0430, \u043d\u043e \u043e\u0447\u0435\u043d\u044c &#171;\u0442\u044f\u0436\u0435\u043b\u043e\u0435&#187; \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0421\u0423\u0411\u0414, \u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u043e\u0432 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u0431\u0438\u0437\u043d\u0435\u0441\u043e\u0432, \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0432 <a href=\"https:\/\/sbis.ru\/all_services\">\u0421\u0411\u0418\u0421<\/a>, <strong>\u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e 1-\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043c\u0435\u0441\u044f\u0446\u0430<\/strong> \u0441\u0443\u0434\u043e\u0440\u043e\u0436\u043d\u043e \u043f\u043e \u0432\u0441\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c &#8212; \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<h2>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0439\u043c\u0435\u043c, \u0447\u0442\u043e &#171;\u0431\u044b\u0441\u0442\u0440\u043e&#187; \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e&nbsp;<strong>\u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e &#171;\u0432\u0435\u0440\u0445\u0443\u0448\u043a\u0435&#187; \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430<\/strong>&nbsp;\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0441\u043a\u043e\u043c\u044b\u0435 TOP-10 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 &#8212; \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u0438\u0445 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439 \u0438 \u043f\u0435\u0440\u0435\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043e\u043a.<\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c (\u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e \u0441\u0443\u043c\u043c\u0435, \u0434\u043b\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e):<\/p>\n<pre><code class=\"sql\">CREATE TABLE item_stat(   item -- \u0442\u043e\u0432\u0430\u0440     integer , sum     numeric(32,2) ); CREATE INDEX ON item_stat(sum DESC);<\/code><\/pre>\n<p>\u041d\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0435 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e &#8212;&nbsp;<strong>\u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0435<\/strong>&nbsp;\u043f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0434\u0430\u0436\u0438. \u041d\u043e \u043a\u0430\u043a \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 &#171;\u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435&#187; \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u043c\u0435\u0441\u044f\u0446\u0430?..<\/p>\n<h4>&#171;\u041d\u0443\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043e\u043b\u043e\u0442\u0430&#187;<\/h4>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u044b\u0441\u0442\u0440\u043e \u0447\u0442\u043e-\u0442\u043e \u0432\u044b\u0447\u0435\u0441\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0437\u0430 12-\u0439 \u043c\u0435\u0441\u044f\u0446 &#171;\u043d\u0430\u0437\u0430\u0434&#187; \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0438 \u0433\u0440\u0430\u043d\u0438\u0446\u044b. \u0422\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b \u0438\u044e\u043d\u044c &#8212; \u0438\u0437 \u043e\u0431\u0449\u0438\u0445 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0447\u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u0438\u044e\u043d\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u0410 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0445 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 &#171;\u0433\u043e\u0434\u0438\u0447\u043d\u044b\u0445&#187;, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:<\/p>\n<pre><code class=\"sql\">CREATE TABLE item_stat(   interval_id -- 0 - \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438, 202001 - \u044f\u043d\u0432\u0430\u0440\u044c 2020, 202002 - \u0444\u0435\u0432\u0440\u0430\u043b\u044c, ...     integer , item     integer , sum     numeric(32,2) , UNIQUE(interval_id, item) ); CREATE INDEX ON item_stat(interval_id, sum DESC);<\/code><\/pre>\n<h2>\u041c\u043e\u043c\u0435\u043d\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0432\u043e\u0442 \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0434\u043e &#171;\u0432\u044b\u0447\u0435\u0441\u0442\u044c&#187; \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u0435\u0441\u044f\u0446, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c&nbsp;\u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c <strong>\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c<\/strong>&nbsp;\u0442\u0438\u043f\u0430&nbsp;&#171;\u043c\u0435\u0441\u044f\u0446 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0435\u0439\u0442\u0438\u043d\u0433\u0430 \u043f\u0440\u043e\u0434\u0430\u0436&#187;. \u0425\u0440\u0430\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u0432 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u043f\u043e\u043c\u0435\u0448\u0430\u0435\u0442 Foreign Key, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0430 <code>item<\/code>):<\/p>\n<pre><code class=\"sql\">INSERT INTO item_stat(   interval_id , item , sum ) VALUES   (0, 0, 202012) -- \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 (0, 0), \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 - 2020'12 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0443\u043c\u043c\u044b ON CONFLICT(interval_id, item)   DO UPDATE SET     sum = EXCLUDED.sum; -- \u0432\u0441\u0435\u0433\u0434\u0430 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043f\u0440\u043e\u0434\u0430\u0436\u0435\u0439 (\u043e\u0442\u0433\u0440\u0443\u0437\u043a\u0430\/\u0430\u043d\u043d\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435) \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c, \u043c\u043e\u0436\u043d\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\/\u0434\u0435\u043a\u0440\u0435\u043c\u0435\u043d\u0442 \u0441\u0440\u0430\u0437\u0443&nbsp;<strong>\u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 &#8212; &#171;\u0433\u043e\u0434\u0438\u0447\u043d\u043e\u0439&#187; \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043c\u0435\u0441\u044f\u0446\u0430<\/strong>:<\/p>\n<pre><code class=\"sql\">INSERT INTO item_stat(   interval_id , item , sum ) VALUES   (202001, 1, 100) -- + \u0432 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 \u0437\u0430 \u044f\u043d\u0432\u0430\u0440\u044c 2020 , (     0, 1, 100) -- + \u0432 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 ON CONFLICT(interval_id, item)   DO UPDATE SET     sum = item_stat.sum + EXCLUDED.sum; -- \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0441\u0443\u043c\u043c\u0443<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/838\/8f4\/584\/8388f4584286b7cfc991e59efdd9651f.png\" width=\"484\" height=\"178\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439&nbsp;<strong>\u043c\u0435\u0441\u044f\u0446 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0430\u0437\u043e\u0448\u0435\u043b\u0441\u044f \u0441 \u043c\u0435\u0441\u044f\u0446\u0435\u043c \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430,&nbsp;\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e<\/strong>&nbsp;\u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 &#171;\u0433\u043e\u0434\u043e\u0432\u044b\u0445&#187; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0432\u044b\u0447\u0438\u0442\u0430\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0437\u0430 \u0441\u0442\u0430\u0432\u0448\u0438\u0435 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u044b\u043c\u0438 \u043c\u0435\u0441\u044f\u0446\u044b, \u0438 \u043f\u0435\u0440\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430:<\/p>\n<pre><code class=\"sql\">-- \"\u043d\u043e\u0432\u044b\u0439\" \u043c\u0435\u0441\u044f\u0446 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 WITH next AS (   SELECT 202101 ) -- \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043c\u0435\u0441\u044f\u0446 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 , prev AS (   SELECT     sum::integer   FROM     item_stat   WHERE     (interval_id, item) = (0, 0) ) -- \u0432\u0441\u0435 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0437\u0430 \u043f\u0435\u0440\u0438\u043e\u0434, \u0441\u0442\u0430\u0432\u0448\u0438\u0439 \u043d\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c, \u0432 \u0440\u0430\u0437\u0440\u0435\u0437\u0435 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 , diff AS (   SELECT     item   , sum(sum) sum   FROM     item_stat   WHERE     interval_id BETWEEN (TABLE prev) - 100 AND (TABLE next) - 100   GROUP BY     1 ) UPDATE   item_stat dst SET   sum = dst.sum - diff.sum FROM   diff WHERE   (dst.interval_id, dst.item) = (0, diff.item);  UPDATE   item_stat SET   sum = 202101 WHERE   (interval_id, item) = (0, 0);<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/a85\/fb8\/af7\/a85fb8af735cf7b965f65ad59a400102.png\" width=\"578\" height=\"190\"><figcaption><\/figcaption><\/figure>\n<h2>\u041f\u0440\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u043e\u0442\u0447\u0435\u0442\u0430<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u0435\u0441\u044f\u0446 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u043c\u0435\u0441\u044f\u0446\u0435\u043c \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u0442\u043e \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 &#171;\u0433\u043e\u0434\u0438\u0447\u043d\u043e\u043c&#187; \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0442\u043e\u043f \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   item_stat WHERE   interval_id = 0 -- \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \"\u0433\u043e\u0434\u0438\u0447\u043d\u044b\u0439\" \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b ORDER BY   sum DESC LIMIT 10;<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 (\u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b \u043d\u043e\u0432\u044b\u0439 \u043c\u0435\u0441\u044f\u0446, \u043d\u043e \u043f\u0440\u043e\u0434\u0430\u0436 \u0435\u0449\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e) &#8212;&nbsp;<strong>\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e<\/strong>&nbsp;\u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u0431\u044b\u043b\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432\u044b\u0448\u0435 (\u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u0443\u043f\u0438\u0442, \u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0437\u0430 \u043c\u0435\u0441\u044f\u0446) \u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432\u044b\u0448\u0435.<\/p>\n<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/536696\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/536696\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u044b\u0445 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439-&#171;\u0445\u043e\u0442\u0435\u043b\u043e\u043a&#187; \u0431\u0438\u0437\u043d\u0435\u0441\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0432\u0441\u044f\u043a\u0438\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0435\u0439\u0442\u0438\u043d\u0433\u043e\u0432 &#8212;&nbsp;<strong>&#171;\u0441\u0430\u043c\u044b\u0435 \u043e\u0431\u043e\u0440\u043e\u0442\u0438\u0441\u0442\u044b\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u044b&#187;, &#171;\u0441\u0430\u043c\u044b\u0435 \u043f\u0440\u043e\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438&#187;, &#171;\u0441\u0430\u043c\u044b\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438&#187;<\/strong>, \u2026 &#8212; \u043b\u044e\u0431\u0438\u043c\u0430\u044f \u0442\u0435\u043c\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0434\u0430\u0448\u0431\u043e\u0440\u0434\u043e\u0432.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 <a href=\"https:\/\/sbis.ru\/presto\">\u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0435\u0441\u0442\u043e\u0440\u0430\u043d\u043e\u0432 \u0438 \u043a\u0430\u0444\u0435 <strong>Presto<\/strong><\/a> \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u0435\u043d \u0442\u0430\u043a\u043e\u0439:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u043e \u043f\u0440\u043e\u0441\u0442\u043e &#171;\u0441\u0430\u043c\u044b\u0435&#187; \u0437\u0430 \u0432\u0435\u0441\u044c \u0434\u043e\u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0435\u0440\u0438\u043e\u0434 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b &#8212; \u043f\u0440\u043e\u0434\u0430\u043b \u0442\u044b 3 \u0433\u043e\u0434\u0430 \u043d\u0430\u0437\u0430\u0434 \u0432\u0430\u0433\u043e\u043d \u0432\u0430\u043b\u0435\u043d\u043e\u043a, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u0443 \u0442\u0435\u0431\u044f \u0432 &#171;\u0441\u0430\u043c\u044b\u0445&#187; \u043f\u0440\u043e\u0434\u0430\u0436\u0430\u0445 \u0432\u0435\u0447\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0438\u0434\u0435\u0442\u044c&nbsp;<strong>&#171;\u0442\u043e\u043f&#187; \u043d\u0430 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435<\/strong>&nbsp;&#8212; \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, &#171;\u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0433\u043e\u0434&#187; (\u0442\u043e\u0447\u043d\u0435\u0435, \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 12 \u043a\u0430\u043b\u0435\u043d\u0434\u0430\u0440\u043d\u044b\u0445 \u043c\u0435\u0441\u044f\u0446\u0435\u0432).<\/p>\n<p>\u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u043e, \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435: \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044e \u043f\u043e &#171;\u0441\u044b\u0440\u044b\u043c&#187; \u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f. \u0418 \u0435\u0441\u043b\u0438 &#171;\u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c&#187; \u0442\u0430\u043a\u043e\u0439 \u043e\u0442\u0447\u0435\u0442 \u043f\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043a\u0435 &#8212; \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0435 \u0434\u043b\u044f SQL-\u043d\u043e\u0432\u0438\u0447\u043a\u0430, \u043d\u043e \u043e\u0447\u0435\u043d\u044c &#171;\u0442\u044f\u0436\u0435\u043b\u043e\u0435&#187; \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0421\u0423\u0411\u0414, \u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u043e\u0432 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u0431\u0438\u0437\u043d\u0435\u0441\u043e\u0432, \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0432 <a href=\"https:\/\/sbis.ru\/all_services\">\u0421\u0411\u0418\u0421<\/a>, <strong>\u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e 1-\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043c\u0435\u0441\u044f\u0446\u0430<\/strong> \u0441\u0443\u0434\u043e\u0440\u043e\u0436\u043d\u043e \u043f\u043e \u0432\u0441\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c &#8212; \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<h2>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0439\u043c\u0435\u043c, \u0447\u0442\u043e &#171;\u0431\u044b\u0441\u0442\u0440\u043e&#187; \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e&nbsp;<strong>\u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e &#171;\u0432\u0435\u0440\u0445\u0443\u0448\u043a\u0435&#187; \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430<\/strong>&nbsp;\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0441\u043a\u043e\u043c\u044b\u0435 TOP-10 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 &#8212; \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u0438\u0445 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439 \u0438 \u043f\u0435\u0440\u0435\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043e\u043a.<\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c (\u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e \u0441\u0443\u043c\u043c\u0435, \u0434\u043b\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e):<\/p>\n<pre><code class=\"sql\">CREATE TABLE item_stat(   item -- \u0442\u043e\u0432\u0430\u0440     integer , sum     numeric(32,2) ); CREATE INDEX ON item_stat(sum DESC);<\/code><\/pre>\n<p>\u041d\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0435 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e &#8212;&nbsp;<strong>\u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0435<\/strong>&nbsp;\u043f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0434\u0430\u0436\u0438. \u041d\u043e \u043a\u0430\u043a \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 &#171;\u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435&#187; \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u043c\u0435\u0441\u044f\u0446\u0430?..<\/p>\n<h4>&#171;\u041d\u0443\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043e\u043b\u043e\u0442\u0430&#187;<\/h4>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u044b\u0441\u0442\u0440\u043e \u0447\u0442\u043e-\u0442\u043e \u0432\u044b\u0447\u0435\u0441\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0437\u0430 12-\u0439 \u043c\u0435\u0441\u044f\u0446 &#171;\u043d\u0430\u0437\u0430\u0434&#187; \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0438 \u0433\u0440\u0430\u043d\u0438\u0446\u044b. \u0422\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b \u0438\u044e\u043d\u044c &#8212; \u0438\u0437 \u043e\u0431\u0449\u0438\u0445 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u0432 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0447\u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u0438\u044e\u043d\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u0410 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0445 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 &#171;\u0433\u043e\u0434\u0438\u0447\u043d\u044b\u0445&#187;, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:<\/p>\n<pre><code class=\"sql\">CREATE TABLE item_stat(   interval_id -- 0 - \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438, 202001 - \u044f\u043d\u0432\u0430\u0440\u044c 2020, 202002 - \u0444\u0435\u0432\u0440\u0430\u043b\u044c, ...     integer , item     integer , sum     numeric(32,2) , UNIQUE(interval_id, item) ); CREATE INDEX ON item_stat(interval_id, sum DESC);<\/code><\/pre>\n<h2>\u041c\u043e\u043c\u0435\u043d\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0432\u043e\u0442 \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0434\u043e &#171;\u0432\u044b\u0447\u0435\u0441\u0442\u044c&#187; \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u0435\u0441\u044f\u0446, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c&nbsp;\u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c <strong>\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c<\/strong>&nbsp;\u0442\u0438\u043f\u0430&nbsp;&#171;\u043c\u0435\u0441\u044f\u0446 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0435\u0439\u0442\u0438\u043d\u0433\u0430 \u043f\u0440\u043e\u0434\u0430\u0436&#187;. \u0425\u0440\u0430\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u0432 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u043f\u043e\u043c\u0435\u0448\u0430\u0435\u0442 Foreign Key, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0430 <code>item<\/code>):<\/p>\n<pre><code class=\"sql\">INSERT INTO item_stat(   interval_id , item , sum ) VALUES   (0, 0, 202012) -- \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 (0, 0), \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 - 2020'12 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0443\u043c\u043c\u044b ON CONFLICT(interval_id, item)   DO UPDATE SET     sum = EXCLUDED.sum; -- \u0432\u0441\u0435\u0433\u0434\u0430 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043f\u0440\u043e\u0434\u0430\u0436\u0435\u0439 (\u043e\u0442\u0433\u0440\u0443\u0437\u043a\u0430\/\u0430\u043d\u043d\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435) \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c, \u043c\u043e\u0436\u043d\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\/\u0434\u0435\u043a\u0440\u0435\u043c\u0435\u043d\u0442 \u0441\u0440\u0430\u0437\u0443&nbsp;<strong>\u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 &#8212; &#171;\u0433\u043e\u0434\u0438\u0447\u043d\u043e\u0439&#187; \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043c\u0435\u0441\u044f\u0446\u0430<\/strong>:<\/p>\n<pre><code class=\"sql\">INSERT INTO item_stat(   interval_id , item , sum ) VALUES   (202001, 1, 100) -- + \u0432 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 \u0437\u0430 \u044f\u043d\u0432\u0430\u0440\u044c 2020 , (     0, 1, 100) -- + \u0432 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 ON CONFLICT(interval_id, item)   DO UPDATE SET     sum = item_stat.sum + EXCLUDED.sum; -- \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0441\u0443\u043c\u043c\u0443<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439&nbsp;<strong>\u043c\u0435\u0441\u044f\u0446 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0430\u0437\u043e\u0448\u0435\u043b\u0441\u044f \u0441 \u043c\u0435\u0441\u044f\u0446\u0435\u043c \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430,&nbsp;\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e<\/strong>&nbsp;\u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 &#171;\u0433\u043e\u0434\u043e\u0432\u044b\u0445&#187; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0432\u044b\u0447\u0438\u0442\u0430\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0437\u0430 \u0441\u0442\u0430\u0432\u0448\u0438\u0435 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u044b\u043c\u0438 \u043c\u0435\u0441\u044f\u0446\u044b, \u0438 \u043f\u0435\u0440\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430:<\/p>\n<pre><code class=\"sql\">-- \"\u043d\u043e\u0432\u044b\u0439\" \u043c\u0435\u0441\u044f\u0446 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 WITH next AS (   SELECT 202101 ) -- \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043c\u0435\u0441\u044f\u0446 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 , prev AS (   SELECT     sum::integer   FROM     item_stat   WHERE     (interval_id, item) = (0, 0) ) -- \u0432\u0441\u0435 \u043f\u0440\u043e\u0434\u0430\u0436\u0438 \u0437\u0430 \u043f\u0435\u0440\u0438\u043e\u0434, \u0441\u0442\u0430\u0432\u0448\u0438\u0439 \u043d\u0435\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c, \u0432 \u0440\u0430\u0437\u0440\u0435\u0437\u0435 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 , diff AS (   SELECT     item   , sum(sum) sum   FROM     item_stat   WHERE     interval_id BETWEEN (TABLE prev) - 100 AND (TABLE next) - 100   GROUP BY     1 ) UPDATE   item_stat dst SET   sum = dst.sum - diff.sum FROM   diff WHERE   (dst.interval_id, dst.item) = (0, diff.item);  UPDATE   item_stat SET   sum = 202101 WHERE   (interval_id, item) = (0, 0);<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h2>\u041f\u0440\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u043e\u0442\u0447\u0435\u0442\u0430<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u0435\u0441\u044f\u0446 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u043c\u0435\u0441\u044f\u0446\u0435\u043c \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u0442\u043e \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 &#171;\u0433\u043e\u0434\u0438\u0447\u043d\u043e\u043c&#187; \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0442\u043e\u043f \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443:<\/p>\n<pre><code class=\"sql\">SELECT   * FROM   item_stat WHERE   interval_id = 0 -- \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \"\u0433\u043e\u0434\u0438\u0447\u043d\u044b\u0439\" \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b ORDER BY   sum DESC LIMIT 10;<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 (\u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b \u043d\u043e\u0432\u044b\u0439 \u043c\u0435\u0441\u044f\u0446, \u043d\u043e \u043f\u0440\u043e\u0434\u0430\u0436 \u0435\u0449\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e) &#8212;&nbsp;<strong>\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e<\/strong>&nbsp;\u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u0431\u044b\u043b\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432\u044b\u0448\u0435 (\u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u0443\u043f\u0438\u0442, \u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0437\u0430 \u043c\u0435\u0441\u044f\u0446) \u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432\u044b\u0448\u0435.<\/p>\n<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/536696\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/536696\/<\/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-316189","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/316189","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=316189"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/316189\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}