{"id":464360,"date":"2025-06-24T21:04:39","date_gmt":"2025-06-24T21:04:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=464360"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=464360","title":{"rendered":"<span>\u041f\u043b\u043e\u0445\u0438\u0435 JOIN\u2019\u044b: \u043f\u0440\u0438\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 (\u043d\u0435\u0447\u0430\u044f\u043d\u043d\u043e) \u043a\u043b\u0430\u0434\u0443\u0442 \u043f\u0440\u043e\u0434<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u0438\u0437\u00a0\u0441\u0430\u043c\u044b\u0445 \u043a\u043e\u0432\u0430\u0440\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0443\u0431\u0438\u0442\u044c \u0431\u0430\u0437\u0443\u00a0\u2014 \u043f\u043b\u043e\u0445\u0438\u0435 <code>JOIN<\/code>&#8216;\u044b. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c\u00a0\u0431\u044b, \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0434\u0435\u043b\u043e: \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u00a0\u2014 \u0438 \u0432\u043f\u0435\u0440\u0451\u0434. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 \u0432 <code>ON<\/code> \u0437\u0430\u0441\u0443\u043d\u0443\u0442\u044c <code>LOWER(email)<\/code>, \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u0440\u043e\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438\u043b\u0438\u00a0\u043f\u0435\u0440\u0435\u043f\u0443\u0442\u0430\u0442\u044c <code>LEFT JOIN<\/code> \u0441 <code>INNER<\/code>\u00a0\u2014 \u0441\u0435\u0440\u0432\u0435\u0440 \u043c\u0438\u0433\u043e\u043c \u043d\u0430\u0447\u043d\u0435\u0442 \u0434\u044b\u0448\u0430\u0442\u044c \u043d\u0430\u00a0\u043b\u0430\u0434\u0430\u043d. <\/p>\n<p>\u0412\u00a0\u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0435\u043a\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p><strong>\u041f<\/strong>\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044c\u0435\u0442 \u043f\u043e\u00a0\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u0427<\/strong>\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a<\/strong>\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433 + \u0438\u043d\u0434\u0435\u043a\u0441\u044b + \u0447\u0442\u043e\u00a0\u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a<\/strong>\u043e\u0433\u0434\u0430 \u0432\u0441\u0435\u2011\u0442\u0430\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0440\u0443\u0448\u0430\u0442\u044c \u0438 \u043d\u0435\u00a0\u0441\u0442\u044b\u0434\u0438\u0442\u044c\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b\u00a0\u2014 PostgreSQL, \u043d\u043e\u00a0\u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0432\u044b\u0432\u043e\u0434\u043e\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u0432\u0430\u043b\u0438\u0442 MySQL \u0438 SQL Server.<\/p>\n<h3>Cartesian product \u0431\u0435\u0437 ON: \u00abSELECT * FROM users, payments\u00bb<\/h3>\n<pre><code class=\"sql\">SELECT  u.id, p.amount FROM    users u, payments p;      -- \u0443\u043f\u0441<\/code><\/pre>\n<p>\u041d\u0430\u00a0\u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0435: users\u00a0\u2014 1\u00a0\u043c\u043b\u043d \u0441\u0442\u0440\u043e\u043a, payments\u00a0\u2014 2\u00a0\u043c\u043b\u043d. \u0418\u0442\u043e\u0433: <strong>2\u00a0000\u00a0000\u00a0000\u00a0rows<\/strong> \u0432\u00a0hash join\u00bb\u0435, 9\u00a0GB temp \u043d\u0430\u00a0\u0434\u0438\u0441\u043a\u0435. \u0411\u0440\u0443\u2011\u0442\u0430\u043b\u044c\u2011\u043d\u043e.<\/p>\n<p>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u043d\u0435\u00a0\u043d\u0430\u0448\u0451\u043b \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0438 \u0441\u0434\u0435\u043b\u0430\u043b \u0447\u0438\u0441\u0442\u044b\u0439 <strong>CROSS JOIN<\/strong>, \u0430\u00a0\u043c\u044b \u0434\u0430\u0436\u0435 \u043d\u0435\u00a0\u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438: \u0432\u00a0\u0441\u0442\u0430\u0440\u043e\u043c ANSI-89\u00a0\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 \u0437\u0430\u043f\u044f\u0442\u0430\u044f\u00a0\u2014 \u044d\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0441\u0442\u0451\u0442 \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e. <\/p>\n<p>\u0412\u0441\u0435\u0433\u0434\u0430 \u043f\u0438\u0448\u0435\u043c \u044f\u0432\u043d\u044b\u0439 <code>JOIN \u2026 ON<\/code>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u00ab\u0438 \u0442\u0430\u043a \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u00bb. \u0412\u00a0PostgreSQL \u2265 15\u00a0\u043c\u043e\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c <code>standard_conforming_strings<\/code> \u0438 <code>FROM \u2026 JOIN<\/code>, \u0447\u0442\u043e\u0431\u044b linters \u043b\u043e\u0432\u0438\u043b\u0438 \u0437\u0430\u043f\u044f\u0442\u0443\u044e\u2011JOIN. \u0415\u0441\u043b\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u0435\u043d \u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u00a0\u2014 \u043f\u0438\u0448\u0438\u0442\u0435 <code>CROSS JOIN<\/code> \u0438 \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 <strong>\u043f\u043e\u0447\u0435\u043c\u0443<\/strong>.<\/p>\n<h3>JOIN \u043f\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (LOWER(email))<\/h3>\n<pre><code class=\"sql\">SELECT  u.id, s.id FROM    users u JOIN    subscriptions s         ON LOWER(u.email) = LOWER(s.email); <\/code><\/pre>\n<p>\u041f\u043e\u00a0\u043f\u043b\u0430\u043d\u0443\u00a0\u2014 <code>Seq Scan<\/code> \u043d\u0430\u00a0\u043e\u0431\u0435\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445, 12\u00a0\u0441\u00a0\u0432\u043c\u0435\u0441\u0442\u043e 40\u00a0\u043c\u0441.<\/p>\n<p>\u041b\u044e\u0431\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 <code>ON<\/code> \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u0437\u0430\u00a0\u0447\u0435\u0440\u043d\u044b\u043c \u044f\u0449\u0438\u043a\u043e\u043c\u00a0\u2014 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0438 \u0438\u0434\u0451\u0442 \u0432\u00a0full scan.<\/p>\n<p>\u0417\u0430\u0432\u043e\u0434\u0438\u043c <strong>functional index<\/strong>:<\/p>\n<pre><code class=\"pgsql\">CREATE INDEX CONCURRENTLY idx_users_email_lower ON users (LOWER(email));<\/code><\/pre>\n<p>\u0438 \u0442\u0430\u043a\u043e\u0439\u00a0\u0436\u0435 \u043d\u0430 <code>subscriptions<\/code>. \u0415\u0441\u043b\u0438 \u0431\u0430\u0437\u0430 \u043d\u0435\u00a0\u0443\u043c\u0435\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b (Hello, \u0441\u0442\u0430\u0440\u044b\u0439 MySQL)\u00a0\u2014 \u0434\u0435\u0440\u0436\u0438\u043c \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 <code>email_lower<\/code> \u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u043d\u0430\u00a0\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435. \u0412\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 (email \u2192 lowercase) \u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u0441\u044f \u0433\u043e\u043b\u044b\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c\u0438.<\/p>\n<h3>LEFT JOIN + WHERE column IS NOT NULL: \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0432 INNER JOIN<\/h3>\n<pre><code class=\"sql\">SELECT  o.id, r.id FROM    orders o LEFT JOIN refunds r ON r.order_id = o.id WHERE   r.id IS NOT NULL;         -- \u041c\u043e\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u043e \u00ab\u0441\u044a\u0435\u0434\u0430\u0435\u0442\u00bb \u0432\u0441\u0435 NULL'\u044b<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0442\u0435\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0431\u0435\u0437\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430\u00a0\u2014 \u0443\u0432\u044b, \u043e\u043d\u0438 \u0438\u0441\u0447\u0435\u0437\u043b\u0438; \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 <code>orders<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u0435\u043d, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0444\u0438\u043b\u044c\u0442\u0440 \u0438\u0434\u0451\u0442 \u041f\u041e\u0421\u041b\u0415 join\u00bb\u0430.<\/p>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440 <code>WHERE r.id IS NOT NULL<\/code> \u0443\u0431\u0438\u0440\u0430\u0435\u0442 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0433\u0434\u0435 <code>refunds<\/code> \u043d\u0435\u00a0\u043d\u0430\u0448\u0435\u043b\u0441\u044f\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>INNER JOIN<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u00a0\u043b\u0438\u0448\u043d\u0438\u043c \u0448\u0430\u0433\u043e\u043c. \u041f\u043b\u0430\u043d \u0440\u0430\u0437\u0434\u0443\u0442, \u0432\u0440\u0435\u043c\u044f\u00a0\u2014 \u0432\u0434\u0432\u043e\u0435 \u0445\u0443\u0436\u0435. <\/p>\n<p>\u041f\u0438\u0448\u0438\u0442\u0435 \u043f\u0440\u044f\u043c\u043e <code>INNER JOIN<\/code> (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u043e\u0442\u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u0438\u0442). \u0415\u0441\u043b\u0438 \u043f\u0440\u0430\u0432\u0434\u0430 \u043d\u0443\u0436\u0435\u043d <code>LEFT JOIN<\/code>, \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440 \u0432 <code>ON<\/code>:<\/p>\n<pre><code class=\"pgsql\">LEFT JOIN refunds r         ON r.order_id = o.id       AND r.processed = TRUE;<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u00ab\u043b\u0435\u0432\u0443\u044e\u00bb \u0447\u0430\u0441\u0442\u044c.<\/p>\n<h3>\u041d\u0435\u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (INT vs TEXT)<\/h3>\n<pre><code class=\"sql\">SELECT  c.id, o.id FROM    customers  c         -- id VARCHAR JOIN    orders     o         -- customer_id INT         ON c.id = o.customer_id; <\/code><\/pre>\n<p>20\u00a0\u043c\u043b\u043d \u0441\u0442\u0440\u043e\u043a, \u043d\u043e\u00a0\u043f\u043b\u0430\u043d \u0443\u043f\u043e\u0440\u043d\u043e \u0447\u0438\u0442\u0430\u0435\u0442 <code>orders<\/code> \u043f\u043e\u00a0PK, \u0430 <code>customers<\/code>\u00a0\u2014 seq scan: \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0435\u00a0\u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u0441\u044f.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0421\u0423\u0411\u0414 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043c\u043b\u0430\u0434\u0448\u0438\u0439 \u0442\u0438\u043f \u043a\u00a0\u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443 \u043f\u043e <strong>precedence<\/strong>; \u0437\u0434\u0435\u0441\u044c\u00a0\u2014 <code>orders.customer_id::text<\/code>. \u0418\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 <code>orders.customer_id<\/code> \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0412\u00a0MS SQL \u044d\u0442\u0430\u00a0\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u00abimplicit conversion\u00bb warning. <\/p>\n<p>\u041f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043e\u0431\u0430 \u043f\u043e\u043b\u044f \u043a\u00a0\u043e\u0434\u043d\u043e\u043c\u0443 \u0442\u0438\u043f\u0443 <strong>\u0432\u00a0\u0441\u0445\u0435\u043c\u0435<\/strong>, \u0430\u00a0\u043d\u0435\u00a0\u0432\u00a0\u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u0412\u00a0\u043a\u0440\u0430\u0439\u043d\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u043c computed\/virtual column \u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0435\u0451. CI\u2011\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430: simple script, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0449\u0435\u0442 <code>::<\/code> \u0438\u043b\u0438 <code>CAST(<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>JOIN \u2026 ON<\/code>.<\/p>\n<h3>OR \u0432 ON-\u0443\u0441\u043b\u043e\u0432\u0438\u0438: \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0440\u0430\u0437\u0432\u043e\u0434\u0438\u0442 \u0440\u0443\u043a\u0430\u043c\u0438<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM    payments p JOIN    invoices i       ON (i.id = p.invoice_id OR i.external_id = p.invoice_external_id);<\/code><\/pre>\n<p>\u041f\u043b\u0430\u043d \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0434\u0432\u0430 seq scan + nested loop, \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0445.<\/p>\n<p><code>OR<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043d\u0435\u043a\u043e\u043d\u044a\u044e\u043d\u043a\u0442\u0438\u0432\u043d\u044b\u043c; \u043e\u0446\u0435\u043d\u043a\u0430 \u0441\u0435\u043b\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u0430\u0434\u0430\u0435\u0442, \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0447\u0430\u0441\u0442\u043e \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f. <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043c \u043d\u0430 <strong>UNION ALL<\/strong> \u0434\u0432\u0443\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u00a0\u2014 \u0432\u00a0\u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c JOIN. \u0418\u043b\u0438\u00a0\u0441\u0442\u0430\u0432\u0438\u043c <strong>partial indexes<\/strong> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>UNION<\/code>. \u0412\u00a0PostgreSQL \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 <code>ENABLE_SEQSCAN = off<\/code> \u0434\u043b\u044f\u00a0\u0442\u0435\u0441\u0442\u0430\u00a0\u2014 \u0435\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 10\u0445 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435, \u0437\u043d\u0430\u0435\u0442\u0435, \u0433\u0434\u0435 \u043a\u043e\u043f\u0430\u0442\u044c.<\/p>\n<h3>\u041d\u0435-\u0441\u0430\u0440\u0433\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f (DATE(created_at) = \u2026)<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM    logs l JOIN    users u ON u.id = l.user_id WHERE   DATE(l.created_at) = CURRENT_DATE;   -- \u0430\u0445 \u0434\u0430, \u043d\u0430\u0434\u043e \u0436\u0435 \u00ab\u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f\u00bb <\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 <code>DATE()<\/code>\u200a\u2011\u200a\u0442\u043e\u0433\u043e\u00a0\u0436\u0435 \u043f\u043e\u0440\u044f\u0434\u043a\u0430, \u0447\u0442\u043e\u00a0FULL SCAN: 6\u00a0\u0441\u0435\u043a \u0432\u043c\u0435\u0441\u0442\u043e 50\u00a0\u043c\u0441.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u0434 \u043a\u043e\u043b\u043e\u043d\u043a\u043e\u0439\u00a0\u2014 \u0442\u043e\u0442\u00a0\u0436\u0435 \u0447\u0451\u0440\u043d\u044b\u0439 \u044f\u0449\u0438\u043a, \u0447\u0442\u043e\u00a0\u0438 \u0432\u00a0\u043a\u0435\u0439\u0441\u0435 #2: \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u0435\u0440\u044f\u0435\u0442\u0441\u044f, plan\u00a0\u2014 seq scan \u043d\u0430 14\u00a0\u0413\u0411.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u043e\u043c<\/p>\n<pre><code class=\"pgsql\">WHERE l.created_at &gt;= CURRENT_DATE   AND l.created_at &lt;  CURRENT_DATE + INTERVAL '1 day';<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c <strong>expression index<\/strong> <code>CREATE INDEX \u2026 (DATE(created_at))<\/code>\u00a0\u2014 \u043d\u043e\u00a0\u0442\u043e\u0433\u0434\u0430 \u0443\u0441\u043f\u043e\u043a\u0430\u0438\u0432\u0430\u0435\u043c DBA \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043d\u0430\u0434\u043e.<\/p>\n<h3>Join \u0431\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432: \u00ab\u0441\u043b\u0435\u043f\u043e\u0439 merge-join\u00bb<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM      big_a a JOIN      big_b b ON b.a_id = a.id;<\/code><\/pre>\n<p>\u0421\u00a0\u043e\u0431\u0435\u0438\u0445 \u0441\u0442\u043e\u0440\u043e\u043d full scan + sort + merge, \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0441\u0442\u0451\u0442 O(n log n). <\/p>\n<p>\u0411\u0435\u0437\u00a0b\u2011tree \u043f\u043e\u0434\u00a0\u043a\u043b\u044e\u0447\u043e\u043c <code>a_id<\/code> \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u0412\u00a0MySQL <code>EXPLAIN<\/code> \u043f\u043e\u043a\u0430\u0436\u0435\u0442 <code>type: ALL<\/code> = full scan. <\/p>\n<p>\u0424\u0438\u043a\u0441\u0438\u043c:<\/p>\n<pre><code class=\"sql\">CREATE INDEX CONCURRENTLY idx_big_b_a_id ON big_b (a_id);<\/code><\/pre>\n<p><em>\u041d\u0435\u00a0\u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e <\/em><code><em>analyze<\/em><\/code>, \u0438\u043d\u0430\u0447\u0435 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 \u0441\u0442\u0430\u0440\u0430\u044f \u0438 PG \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442 seq scan \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0434\u0443\u043c\u0430\u0435\u0442, \u0447\u0442\u043e\u00a0\u044d\u0442\u043e \u0434\u0451\u0448\u0435\u0432\u043e.<\/p>\n<h3>CROSS APPLY \/ LATERAL \u043a\u0430\u043a \u0446\u0438\u043a\u043b \u00abfor each row\u00bb<\/h3>\n<pre><code class=\"sql\">SELECT  u.id,         l.last_login FROM    users u CROSS APPLY (    SELECT  last_login    FROM    logins    WHERE   user_id = u.id    ORDER BY created_at DESC    LIMIT 1 ) l;<\/code><\/pre>\n<p>\u0414\u043b\u044f 500\u00a0k \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439\u00a0\u2014 500\u00a0k * (\u0438\u043d\u0434\u0435\u043a\u0441\u2011lookup + sort), latency\u00a0\u2014 \u043c\u0438\u043d\u0443\u0442\u044b.<\/p>\n<p><code>CROSS APPLY<\/code> (SQL Server) \u0438\u043b\u0438 <code>LATERAL<\/code> (Postgres) \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\u0434\u043b\u044f\u00a0\u043a\u0430\u0436\u0434\u043e\u0439<\/strong> \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0415\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\/\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c N \u0440\u0430\u0437. <\/p>\n<p>\u0420\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u00a0\u043e\u043a\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"pgsql\">SELECT DISTINCT ON (u.id)        u.id,        l.last_login FROM   users u JOIN   logins l ON l.user_id = u.id ORDER  BY u.id, l.created_at DESC;<\/code><\/pre>\n<p>\u0418\u043b\u0438\u00a0\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f + join:<\/p>\n<pre><code class=\"pgsql\">WITH last AS (     SELECT user_id, MAX(created_at) AS last_login     FROM   logins     GROUP  BY user_id ) SELECT u.id, last.last_login FROM   users u LEFT JOIN last ON last.user_id = u.id;<\/code><\/pre>\n<h3>JOIN \u043a \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443-\u0447\u0443\u0434\u043e\u0432\u0438\u0449\u0443<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM    orders o JOIN    sales_report_monthly m           ON m.customer_id = o.customer_id; <\/code><\/pre>\n<p><code>sales_report_monthly<\/code>\u00a0\u2014 \u044d\u0442\u043e <code>SELECT \u2026 GROUP BY month<\/code>, 150\u00a0GB, no indexes.<\/p>\n<p>\u0427\u0430\u0441\u0442\u043e \u0432\u044b\u043d\u043e\u0441\u044f\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u00a0view \u0438\u043b\u0438\u00a0CTE, \u0430\u00a0\u043f\u043e\u0442\u043e\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442 \u0431\u0435\u0437\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 view <strong>\u043a\u0430\u043a\u00a0\u0435\u0441\u0442\u044c<\/strong>, \u0438 join \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0432\u0435\u0440\u0445 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e, \u0443\u0436\u0435 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430.<\/p>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (Materialized View) + \u0438\u043d\u0434\u0435\u043a\u0441. \u0418\u043b\u0438\u00a0\u0438\u043d\u043b\u0430\u0439\u043d\u2011\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f: \u0441\u0447\u0438\u0442\u0430\u0435\u043c <code>GROUP BY<\/code> \u043d\u0430\u00a0\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435, \u043f\u043e\u0442\u043e\u043c join. \u041d\u0435\u00a0\u0431\u043e\u0438\u043c\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446, \u043e\u043d\u0438 \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u0448\u0435\u0432\u043b\u0435 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0441\u0447\u0451\u0442\u0430.<\/p>\n<h3>Fan-out join: many-to-many + \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 DISTINCT<\/h3>\n<pre><code class=\"sql\">SELECT  p.id, t.tag FROM    products p JOIN    products_tags pt ON pt.product_id = p.id JOIN    tags t           ON t.id = pt.tag_id;<\/code><\/pre>\n<p>\u0412\u00a0\u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0435 \u044d\u0442\u043e \u043e\u043a, \u043d\u043e\u00a0\u0444\u0440\u043e\u043d\u0442 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <strong>\u043f\u043e\u00a0\u043e\u0434\u043d\u043e\u043c\u0443<\/strong> \u0442\u0435\u0433\u0443, \u0430\u00a0\u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e\u00a0\u2014 \u0442\u044b\u0441\u044f\u0447\u0438 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432, JSON\u2011\u043e\u0442\u0432\u0435\u0442 \u043f\u0443\u0445\u043d\u0435\u0442.<\/p>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 join \u043c\u043d\u043e\u0436\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u0438; \u0431\u0435\u0437 <code>\"UNIQUE\"<\/code> \u0432 <code>products_tags<\/code> \u0438\u043b\u0438 <code>DISTINCT<\/code> \u043f\u043e\u0441\u043b\u0435 join \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043b\u0430\u0432\u0438\u043d\u0443. \u041f\u043b\u044e\u0441\u00a0\u2014 order by \u043f\u043e\u0441\u043b\u0435 join \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0443\u0436\u0435 \u0440\u0430\u0437\u0434\u0443\u0442\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443.<\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c <code>UNIQUE (product_id, tag_id)<\/code>\u00a0\u2014 \u043f\u043b\u044e\u0441 DB \u0441\u0430\u043c\u0430 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c. \u041d\u0430\u00a0\u0441\u0442\u043e\u0440\u043e\u043d\u0435 SQL\u00a0\u2014 <code>SELECT <\/code><a href=\"http:\/\/p.id\" rel=\"noopener noreferrer nofollow\"><code>p.id<\/code><\/a><code>, ARRAY_AGG(t.tag) \u2026 GROUP BY p.id<\/code> (Postgres) \u0438\u043b\u0438 <code>JSON_ARRAYAGG<\/code> \u0432\u00a0MySQL 8.<\/p>\n<p>\u0412\u00a0\u0442\u044f\u0436\u0435\u043b\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445\u00a0\u2014 \u0434\u0435\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u00abtags jsonb\u00bb + \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u043d\u0430\u00a0\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<hr\/>\n<h3>\u0412\u044b\u0432\u043e\u0434<\/h3>\n<p><strong>JOIN\u00a0\u2014 \u044d\u0442\u043e \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u00ab\u0441\u043a\u043b\u0435\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0438\u00bb<\/strong>. \u042d\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043c\u0435\u0436\u0434\u0443 \u0432\u0430\u043c\u0438 \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u043c. \u041d\u0430\u0440\u0443\u0448\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u00a0\u2014 \u043f\u043b\u0430\u0442\u0438\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438.<\/p>\n<p>\u0411\u0435\u0440\u0435\u0433\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b, \u043d\u0435\u00a0\u043f\u0440\u044f\u0447\u044c\u0442\u0435 \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0442\u0438\u043f\u044b \u0438 \u043d\u0435\u00a0\u0431\u043e\u0439\u0442\u0435\u0441\u044c <code>EXPLAIN ANALYZE<\/code>. <\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u0441\u00a0\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438\u0437\u2011\u0437\u0430 \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 JOIN\u2011\u043e\u0432, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430\u00a0\u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a <a href=\"https:\/\/otus.pw\/u4x5\/\" rel=\"noopener noreferrer nofollow\">\u00abSQL: \u041e\u043a\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0\u2014 \u043a\u043e\u0433\u0434\u0430 GROUP BY \u0443\u0436\u0435 \u043d\u0435\u00a0\u0445\u0432\u0430\u0442\u0430\u0435\u0442\u00bb<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>2\u00a0\u0438\u044e\u043b\u044f \u0432 20:00<\/strong>. \u0423\u0437\u043d\u0430\u0439\u0442\u0435, \u043a\u0430\u043a\u00a0\u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u00ab\u0441\u043b\u043e\u043c\u0430\u0442\u044c\u00bb \u0432\u0430\u0448 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d.<\/p>\n<p><em>\u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 <\/em><a href=\"https:\/\/otus.pw\/F0t3\/\" rel=\"noopener noreferrer nofollow\"><em>\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/em><\/a><em>, \u0447\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 SQL \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u0433\u0430\u0439\u0434.<\/em><\/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\/articles\/920004\/\"> https:\/\/habr.com\/ru\/articles\/920004\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u0438\u0437\u00a0\u0441\u0430\u043c\u044b\u0445 \u043a\u043e\u0432\u0430\u0440\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0443\u0431\u0438\u0442\u044c \u0431\u0430\u0437\u0443\u00a0\u2014 \u043f\u043b\u043e\u0445\u0438\u0435 <code>JOIN<\/code>&#8216;\u044b. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c\u00a0\u0431\u044b, \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0434\u0435\u043b\u043e: \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u00a0\u2014 \u0438 \u0432\u043f\u0435\u0440\u0451\u0434. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 \u0432 <code>ON<\/code> \u0437\u0430\u0441\u0443\u043d\u0443\u0442\u044c <code>LOWER(email)<\/code>, \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u0440\u043e\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438\u043b\u0438\u00a0\u043f\u0435\u0440\u0435\u043f\u0443\u0442\u0430\u0442\u044c <code>LEFT JOIN<\/code> \u0441 <code>INNER<\/code>\u00a0\u2014 \u0441\u0435\u0440\u0432\u0435\u0440 \u043c\u0438\u0433\u043e\u043c \u043d\u0430\u0447\u043d\u0435\u0442 \u0434\u044b\u0448\u0430\u0442\u044c \u043d\u0430\u00a0\u043b\u0430\u0434\u0430\u043d. <\/p>\n<p>\u0412\u00a0\u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0435\u043a\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p><strong>\u041f<\/strong>\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044c\u0435\u0442 \u043f\u043e\u00a0\u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u0427<\/strong>\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a<\/strong>\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433 + \u0438\u043d\u0434\u0435\u043a\u0441\u044b + \u0447\u0442\u043e\u00a0\u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a<\/strong>\u043e\u0433\u0434\u0430 \u0432\u0441\u0435\u2011\u0442\u0430\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0440\u0443\u0448\u0430\u0442\u044c \u0438 \u043d\u0435\u00a0\u0441\u0442\u044b\u0434\u0438\u0442\u044c\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b\u00a0\u2014 PostgreSQL, \u043d\u043e\u00a0\u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0432\u044b\u0432\u043e\u0434\u043e\u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u0432\u0430\u043b\u0438\u0442 MySQL \u0438 SQL Server.<\/p>\n<h3>Cartesian product \u0431\u0435\u0437 ON: \u00abSELECT * FROM users, payments\u00bb<\/h3>\n<pre><code class=\"sql\">SELECT  u.id, p.amount FROM    users u, payments p;      -- \u0443\u043f\u0441<\/code><\/pre>\n<p>\u041d\u0430\u00a0\u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0435: users\u00a0\u2014 1\u00a0\u043c\u043b\u043d \u0441\u0442\u0440\u043e\u043a, payments\u00a0\u2014 2\u00a0\u043c\u043b\u043d. \u0418\u0442\u043e\u0433: <strong>2\u00a0000\u00a0000\u00a0000\u00a0rows<\/strong> \u0432\u00a0hash join\u00bb\u0435, 9\u00a0GB temp \u043d\u0430\u00a0\u0434\u0438\u0441\u043a\u0435. \u0411\u0440\u0443\u2011\u0442\u0430\u043b\u044c\u2011\u043d\u043e.<\/p>\n<p>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u043d\u0435\u00a0\u043d\u0430\u0448\u0451\u043b \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0438 \u0441\u0434\u0435\u043b\u0430\u043b \u0447\u0438\u0441\u0442\u044b\u0439 <strong>CROSS JOIN<\/strong>, \u0430\u00a0\u043c\u044b \u0434\u0430\u0436\u0435 \u043d\u0435\u00a0\u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438: \u0432\u00a0\u0441\u0442\u0430\u0440\u043e\u043c ANSI-89\u00a0\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 \u0437\u0430\u043f\u044f\u0442\u0430\u044f\u00a0\u2014 \u044d\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0441\u0442\u0451\u0442 \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e. <\/p>\n<p>\u0412\u0441\u0435\u0433\u0434\u0430 \u043f\u0438\u0448\u0435\u043c \u044f\u0432\u043d\u044b\u0439 <code>JOIN \u2026 ON<\/code>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u00ab\u0438 \u0442\u0430\u043a \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u00bb. \u0412\u00a0PostgreSQL \u2265 15\u00a0\u043c\u043e\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c <code>standard_conforming_strings<\/code> \u0438 <code>FROM \u2026 JOIN<\/code>, \u0447\u0442\u043e\u0431\u044b linters \u043b\u043e\u0432\u0438\u043b\u0438 \u0437\u0430\u043f\u044f\u0442\u0443\u044e\u2011JOIN. \u0415\u0441\u043b\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u0435\u043d \u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u00a0\u2014 \u043f\u0438\u0448\u0438\u0442\u0435 <code>CROSS JOIN<\/code> \u0438 \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 <strong>\u043f\u043e\u0447\u0435\u043c\u0443<\/strong>.<\/p>\n<h3>JOIN \u043f\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (LOWER(email))<\/h3>\n<pre><code class=\"sql\">SELECT  u.id, s.id FROM    users u JOIN    subscriptions s         ON LOWER(u.email) = LOWER(s.email); <\/code><\/pre>\n<p>\u041f\u043e\u00a0\u043f\u043b\u0430\u043d\u0443\u00a0\u2014 <code>Seq Scan<\/code> \u043d\u0430\u00a0\u043e\u0431\u0435\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445, 12\u00a0\u0441\u00a0\u0432\u043c\u0435\u0441\u0442\u043e 40\u00a0\u043c\u0441.<\/p>\n<p>\u041b\u044e\u0431\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 <code>ON<\/code> \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u0437\u0430\u00a0\u0447\u0435\u0440\u043d\u044b\u043c \u044f\u0449\u0438\u043a\u043e\u043c\u00a0\u2014 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0438 \u0438\u0434\u0451\u0442 \u0432\u00a0full scan.<\/p>\n<p>\u0417\u0430\u0432\u043e\u0434\u0438\u043c <strong>functional index<\/strong>:<\/p>\n<pre><code class=\"pgsql\">CREATE INDEX CONCURRENTLY idx_users_email_lower ON users (LOWER(email));<\/code><\/pre>\n<p>\u0438 \u0442\u0430\u043a\u043e\u0439\u00a0\u0436\u0435 \u043d\u0430 <code>subscriptions<\/code>. \u0415\u0441\u043b\u0438 \u0431\u0430\u0437\u0430 \u043d\u0435\u00a0\u0443\u043c\u0435\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b (Hello, \u0441\u0442\u0430\u0440\u044b\u0439 MySQL)\u00a0\u2014 \u0434\u0435\u0440\u0436\u0438\u043c \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 <code>email_lower<\/code> \u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u043d\u0430\u00a0\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435. \u0412\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 (email \u2192 lowercase) \u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u0441\u044f \u0433\u043e\u043b\u044b\u043c\u0438 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c\u0438.<\/p>\n<h3>LEFT JOIN + WHERE column IS NOT NULL: \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0432 INNER JOIN<\/h3>\n<pre><code class=\"sql\">SELECT  o.id, r.id FROM    orders o LEFT JOIN refunds r ON r.order_id = o.id WHERE   r.id IS NOT NULL;         -- \u041c\u043e\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u043e \u00ab\u0441\u044a\u0435\u0434\u0430\u0435\u0442\u00bb \u0432\u0441\u0435 NULL'\u044b<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0442\u0435\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0431\u0435\u0437\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430\u00a0\u2014 \u0443\u0432\u044b, \u043e\u043d\u0438 \u0438\u0441\u0447\u0435\u0437\u043b\u0438; \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 <code>orders<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u0435\u043d, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0444\u0438\u043b\u044c\u0442\u0440 \u0438\u0434\u0451\u0442 \u041f\u041e\u0421\u041b\u0415 join\u00bb\u0430.<\/p>\n<p>\u0424\u0438\u043b\u044c\u0442\u0440 <code>WHERE r.id IS NOT NULL<\/code> \u0443\u0431\u0438\u0440\u0430\u0435\u0442 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0433\u0434\u0435 <code>refunds<\/code> \u043d\u0435\u00a0\u043d\u0430\u0448\u0435\u043b\u0441\u044f\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0439 <code>INNER JOIN<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u00a0\u043b\u0438\u0448\u043d\u0438\u043c \u0448\u0430\u0433\u043e\u043c. \u041f\u043b\u0430\u043d \u0440\u0430\u0437\u0434\u0443\u0442, \u0432\u0440\u0435\u043c\u044f\u00a0\u2014 \u0432\u0434\u0432\u043e\u0435 \u0445\u0443\u0436\u0435. <\/p>\n<p>\u041f\u0438\u0448\u0438\u0442\u0435 \u043f\u0440\u044f\u043c\u043e <code>INNER JOIN<\/code> (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u043e\u0442\u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u0438\u0442). \u0415\u0441\u043b\u0438 \u043f\u0440\u0430\u0432\u0434\u0430 \u043d\u0443\u0436\u0435\u043d <code>LEFT JOIN<\/code>, \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440 \u0432 <code>ON<\/code>:<\/p>\n<pre><code class=\"pgsql\">LEFT JOIN refunds r         ON r.order_id = o.id       AND r.processed = TRUE;<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u00ab\u043b\u0435\u0432\u0443\u044e\u00bb \u0447\u0430\u0441\u0442\u044c.<\/p>\n<h3>\u041d\u0435\u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (INT vs TEXT)<\/h3>\n<pre><code class=\"sql\">SELECT  c.id, o.id FROM    customers  c         -- id VARCHAR JOIN    orders     o         -- customer_id INT         ON c.id = o.customer_id; <\/code><\/pre>\n<p>20\u00a0\u043c\u043b\u043d \u0441\u0442\u0440\u043e\u043a, \u043d\u043e\u00a0\u043f\u043b\u0430\u043d \u0443\u043f\u043e\u0440\u043d\u043e \u0447\u0438\u0442\u0430\u0435\u0442 <code>orders<\/code> \u043f\u043e\u00a0PK, \u0430 <code>customers<\/code>\u00a0\u2014 seq scan: \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0435\u00a0\u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u0441\u044f.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0421\u0423\u0411\u0414 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043c\u043b\u0430\u0434\u0448\u0438\u0439 \u0442\u0438\u043f \u043a\u00a0\u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443 \u043f\u043e <strong>precedence<\/strong>; \u0437\u0434\u0435\u0441\u044c\u00a0\u2014 <code>orders.customer_id::text<\/code>. \u0418\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 <code>orders.customer_id<\/code> \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0412\u00a0MS SQL \u044d\u0442\u0430\u00a0\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u00abimplicit conversion\u00bb warning. <\/p>\n<p>\u041f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043e\u0431\u0430 \u043f\u043e\u043b\u044f \u043a\u00a0\u043e\u0434\u043d\u043e\u043c\u0443 \u0442\u0438\u043f\u0443 <strong>\u0432\u00a0\u0441\u0445\u0435\u043c\u0435<\/strong>, \u0430\u00a0\u043d\u0435\u00a0\u0432\u00a0\u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u0412\u00a0\u043a\u0440\u0430\u0439\u043d\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u043c computed\/virtual column \u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0435\u0451. CI\u2011\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430: simple script, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0449\u0435\u0442 <code>::<\/code> \u0438\u043b\u0438 <code>CAST(<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>JOIN \u2026 ON<\/code>.<\/p>\n<h3>OR \u0432 ON-\u0443\u0441\u043b\u043e\u0432\u0438\u0438: \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0440\u0430\u0437\u0432\u043e\u0434\u0438\u0442 \u0440\u0443\u043a\u0430\u043c\u0438<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM    payments p JOIN    invoices i       ON (i.id = p.invoice_id OR i.external_id = p.invoice_external_id);<\/code><\/pre>\n<p>\u041f\u043b\u0430\u043d \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0434\u0432\u0430 seq scan + nested loop, \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0445.<\/p>\n<p><code>OR<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043d\u0435\u043a\u043e\u043d\u044a\u044e\u043d\u043a\u0442\u0438\u0432\u043d\u044b\u043c; \u043e\u0446\u0435\u043d\u043a\u0430 \u0441\u0435\u043b\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u0430\u0434\u0430\u0435\u0442, \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0447\u0430\u0441\u0442\u043e \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f. <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043c \u043d\u0430 <strong>UNION ALL<\/strong> \u0434\u0432\u0443\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u00a0\u2014 \u0432\u00a0\u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c JOIN. \u0418\u043b\u0438\u00a0\u0441\u0442\u0430\u0432\u0438\u043c <strong>partial indexes<\/strong> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>UNION<\/code>. \u0412\u00a0PostgreSQL \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 <code>ENABLE_SEQSCAN = off<\/code> \u0434\u043b\u044f\u00a0\u0442\u0435\u0441\u0442\u0430\u00a0\u2014 \u0435\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 10\u0445 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435, \u0437\u043d\u0430\u0435\u0442\u0435, \u0433\u0434\u0435 \u043a\u043e\u043f\u0430\u0442\u044c.<\/p>\n<h3>\u041d\u0435-\u0441\u0430\u0440\u0433\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f (DATE(created_at) = \u2026)<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM    logs l JOIN    users u ON u.id = l.user_id WHERE   DATE(l.created_at) = CURRENT_DATE;   -- \u0430\u0445 \u0434\u0430, \u043d\u0430\u0434\u043e \u0436\u0435 \u00ab\u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f\u00bb <\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 <code>DATE()<\/code>\u200a\u2011\u200a\u0442\u043e\u0433\u043e\u00a0\u0436\u0435 \u043f\u043e\u0440\u044f\u0434\u043a\u0430, \u0447\u0442\u043e\u00a0FULL SCAN: 6\u00a0\u0441\u0435\u043a \u0432\u043c\u0435\u0441\u0442\u043e 50\u00a0\u043c\u0441.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u0434 \u043a\u043e\u043b\u043e\u043d\u043a\u043e\u0439\u00a0\u2014 \u0442\u043e\u0442\u00a0\u0436\u0435 \u0447\u0451\u0440\u043d\u044b\u0439 \u044f\u0449\u0438\u043a, \u0447\u0442\u043e\u00a0\u0438 \u0432\u00a0\u043a\u0435\u0439\u0441\u0435 #2: \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u0435\u0440\u044f\u0435\u0442\u0441\u044f, plan\u00a0\u2014 seq scan \u043d\u0430 14\u00a0\u0413\u0411.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u043e\u043c<\/p>\n<pre><code class=\"pgsql\">WHERE l.created_at &gt;= CURRENT_DATE   AND l.created_at &lt;  CURRENT_DATE + INTERVAL '1 day';<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c <strong>expression index<\/strong> <code>CREATE INDEX \u2026 (DATE(created_at))<\/code>\u00a0\u2014 \u043d\u043e\u00a0\u0442\u043e\u0433\u0434\u0430 \u0443\u0441\u043f\u043e\u043a\u0430\u0438\u0432\u0430\u0435\u043c DBA \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043d\u0430\u0434\u043e.<\/p>\n<h3>Join \u0431\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432: \u00ab\u0441\u043b\u0435\u043f\u043e\u0439 merge-join\u00bb<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM      big_a a JOIN      big_b b ON b.a_id = a.id;<\/code><\/pre>\n<p>\u0421\u00a0\u043e\u0431\u0435\u0438\u0445 \u0441\u0442\u043e\u0440\u043e\u043d full scan + sort + merge, \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0441\u0442\u0451\u0442 O(n log n). <\/p>\n<p>\u0411\u0435\u0437\u00a0b\u2011tree \u043f\u043e\u0434\u00a0\u043a\u043b\u044e\u0447\u043e\u043c <code>a_id<\/code> \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u0412\u00a0MySQL <code>EXPLAIN<\/code> \u043f\u043e\u043a\u0430\u0436\u0435\u0442 <code>type: ALL<\/code> = full scan. <\/p>\n<p>\u0424\u0438\u043a\u0441\u0438\u043c:<\/p>\n<pre><code class=\"sql\">CREATE INDEX CONCURRENTLY idx_big_b_a_id ON big_b (a_id);<\/code><\/pre>\n<p><em>\u041d\u0435\u00a0\u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e <\/em><code><em>analyze<\/em><\/code>, \u0438\u043d\u0430\u0447\u0435 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 \u0441\u0442\u0430\u0440\u0430\u044f \u0438 PG \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442 seq scan \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0434\u0443\u043c\u0430\u0435\u0442, \u0447\u0442\u043e\u00a0\u044d\u0442\u043e \u0434\u0451\u0448\u0435\u0432\u043e.<\/p>\n<h3>CROSS APPLY \/ LATERAL \u043a\u0430\u043a \u0446\u0438\u043a\u043b \u00abfor each row\u00bb<\/h3>\n<pre><code class=\"sql\">SELECT  u.id,         l.last_login FROM    users u CROSS APPLY (    SELECT  last_login    FROM    logins    WHERE   user_id = u.id    ORDER BY created_at DESC    LIMIT 1 ) l;<\/code><\/pre>\n<p>\u0414\u043b\u044f 500\u00a0k \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439\u00a0\u2014 500\u00a0k * (\u0438\u043d\u0434\u0435\u043a\u0441\u2011lookup + sort), latency\u00a0\u2014 \u043c\u0438\u043d\u0443\u0442\u044b.<\/p>\n<p><code>CROSS APPLY<\/code> (SQL Server) \u0438\u043b\u0438 <code>LATERAL<\/code> (Postgres) \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\u0434\u043b\u044f\u00a0\u043a\u0430\u0436\u0434\u043e\u0439<\/strong> \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0415\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\/\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f\u00a0\u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c N \u0440\u0430\u0437. <\/p>\n<p>\u0420\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u00a0\u043e\u043a\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"pgsql\">SELECT DISTINCT ON (u.id)        u.id,        l.last_login FROM   users u JOIN   logins l ON l.user_id = u.id ORDER  BY u.id, l.created_at DESC;<\/code><\/pre>\n<p>\u0418\u043b\u0438\u00a0\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f + join:<\/p>\n<pre><code class=\"pgsql\">WITH last AS (     SELECT user_id, MAX(created_at) AS last_login     FROM   logins     GROUP  BY user_id ) SELECT u.id, last.last_login FROM   users u LEFT JOIN last ON last.user_id = u.id;<\/code><\/pre>\n<h3>JOIN \u043a \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443-\u0447\u0443\u0434\u043e\u0432\u0438\u0449\u0443<\/h3>\n<pre><code class=\"sql\">SELECT  * FROM    orders o JOIN    sales_report_monthly m           ON m.customer_id = o.customer_id; <\/code><\/pre>\n<p><code>sales_report_monthly<\/code>\u00a0\u2014 \u044d\u0442\u043e <code>SELECT \u2026 GROUP BY month<\/code>, 150\u00a0GB, no indexes.<\/p>\n<p>\u0427\u0430\u0441\u0442\u043e \u0432\u044b\u043d\u043e\u0441\u044f\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u00a0view \u0438\u043b\u0438\u00a0CTE, \u0430\u00a0\u043f\u043e\u0442\u043e\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442 \u0431\u0435\u0437\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 view <strong>\u043a\u0430\u043a\u00a0\u0435\u0441\u0442\u044c<\/strong>, \u0438 join \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0432\u0435\u0440\u0445 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e, \u0443\u0436\u0435 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430.<\/p>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (Materialized View) + \u0438\u043d\u0434\u0435\u043a\u0441. \u0418\u043b\u0438\u00a0\u0438\u043d\u043b\u0430\u0439\u043d\u2011\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f: \u0441\u0447\u0438\u0442\u0430\u0435\u043c <code>GROUP BY<\/code> \u043d\u0430\u00a0\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435, \u043f\u043e\u0442\u043e\u043c join. \u041d\u0435\u00a0\u0431\u043e\u0438\u043c\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446, \u043e\u043d\u0438 \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u0448\u0435\u0432\u043b\u0435 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0441\u0447\u0451\u0442\u0430.<\/p>\n<h3>Fan-out join: many-to-many + \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 DISTINCT<\/h3>\n<pre><code class=\"sql\">SELECT  p.id, t.tag FROM    products p JOIN    products_tags pt ON pt.product_id = p.id JOIN    tags t           ON t.id = pt.tag_id;<\/code><\/pre>\n<p>\u0412\u00a0\u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0435 \u044d\u0442\u043e \u043e\u043a, \u043d\u043e\u00a0\u0444\u0440\u043e\u043d\u0442 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <strong>\u043f\u043e\u00a0\u043e\u0434\u043d\u043e\u043c\u0443<\/strong> \u0442\u0435\u0433\u0443, \u0430\u00a0\u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e\u00a0\u2014 \u0442\u044b\u0441\u044f\u0447\u0438 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432, JSON\u2011\u043e\u0442\u0432\u0435\u0442 \u043f\u0443\u0445\u043d\u0435\u0442.<\/p>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 join \u043c\u043d\u043e\u0436\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u0438; \u0431\u0435\u0437 <code>\"UNIQUE\"<\/code> \u0432 <code>products_tags<\/code> \u0438\u043b\u0438 <code>DISTINCT<\/code> \u043f\u043e\u0441\u043b\u0435 join \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043b\u0430\u0432\u0438\u043d\u0443. \u041f\u043b\u044e\u0441\u00a0\u2014 order by \u043f\u043e\u0441\u043b\u0435 join \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0443\u0436\u0435 \u0440\u0430\u0437\u0434\u0443\u0442\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443.<\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u043c <code>UNIQUE (product_id, tag_id)<\/code>\u00a0\u2014 \u043f\u043b\u044e\u0441 DB \u0441\u0430\u043c\u0430 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c. \u041d\u0430\u00a0\u0441\u0442\u043e\u0440\u043e\u043d\u0435 SQL\u00a0\u2014 <code>SELECT <\/code><a href=\"http:\/\/p.id\" rel=\"noopener noreferrer nofollow\"><code>p.id<\/code><\/a><code>, ARRAY_AGG(t.tag) \u2026 GROUP BY p.id<\/code> (Postgres) \u0438\u043b\u0438 <code>JSON_ARRAYAGG<\/code> \u0432\u00a0MySQL 8.<\/p>\n<p>\u0412\u00a0\u0442\u044f\u0436\u0435\u043b\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445\u00a0\u2014 \u0434\u0435\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u00abtags jsonb\u00bb + \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u043d\u0430\u00a0\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<hr\/>\n<h3>\u0412\u044b\u0432\u043e\u0434<\/h3>\n<p><strong>JOIN\u00a0\u2014 \u044d\u0442\u043e \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u00ab\u0441\u043a\u043b\u0435\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0438\u00bb<\/strong>. \u042d\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043c\u0435\u0436\u0434\u0443 \u0432\u0430\u043c\u0438 \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u043c. \u041d\u0430\u0440\u0443\u0448\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u00a0\u2014 \u043f\u043b\u0430\u0442\u0438\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438.<\/p>\n<p>\u0411\u0435\u0440\u0435\u0433\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b, \u043d\u0435\u00a0\u043f\u0440\u044f\u0447\u044c\u0442\u0435 \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0442\u0438\u043f\u044b \u0438 \u043d\u0435\u00a0\u0431\u043e\u0439\u0442\u0435\u0441\u044c <code>EXPLAIN ANALYZE<\/code>. <\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u0441\u00a0\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438\u0437\u2011\u0437\u0430 \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 JOIN\u2011\u043e\u0432, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430\u00a0\u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a <a href=\"https:\/\/otus.pw\/u4x5\/\" rel=\"noopener noreferrer nofollow\">\u00abSQL: \u041e\u043a\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0\u2014 \u043a\u043e\u0433\u0434\u0430 GROUP BY \u0443\u0436\u0435 \u043d\u0435\u00a0\u0445\u0432\u0430\u0442\u0430\u0435\u0442\u00bb<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>2\u00a0\u0438\u044e\u043b\u044f \u0432 20:00<\/strong>. \u0423\u0437\u043d\u0430\u0439\u0442\u0435, \u043a\u0430\u043a\u00a0\u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u00ab\u0441\u043b\u043e\u043c\u0430\u0442\u044c\u00bb \u0432\u0430\u0448 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d.<\/p>\n<p><em>\u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 <\/em><a href=\"https:\/\/otus.pw\/F0t3\/\" rel=\"noopener noreferrer nofollow\"><em>\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/em><\/a><em>, \u0447\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 SQL \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u0433\u0430\u0439\u0434.<\/em><\/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\/articles\/920004\/\"> https:\/\/habr.com\/ru\/articles\/920004\/<\/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-464360","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/464360","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=464360"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/464360\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}