{"id":457915,"date":"2025-04-29T09:18:46","date_gmt":"2025-04-29T09:18:46","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=457915"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=457915","title":{"rendered":"<span>\u041f\u043e\u0447\u0435\u043c\u0443 COUNT(*) \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c COUNT(col) \u2014 \u0438 \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a<\/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>\u0422\u0435\u043c\u0430 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e\u0439: \u0437\u0430\u0447\u0435\u043c <code>COUNT(col)<\/code>, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c <code>COUNT(*)<\/code>? \u041d\u043e \u0437\u0430 \u043d\u0435\u0439 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0443\u0447\u0430 \u043d\u044e\u0430\u043d\u0441\u043e\u0432: MVCC\u2011\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, visibility map, index\u2011only scan, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u2026 \u0418 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 PostgreSQL <code>COUNT(*)<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f \u043f\u043e\u2011\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0432\u0441\u0451 \u0434\u043e\u0441\u043a\u043e\u043d\u0430\u043b\u044c\u043d\u043e.<\/p>\n<h4>\u0421\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0430 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443 SQL<\/h4>\n<p><code><strong>COUNT(*)<\/strong><\/code> \u0441\u0447\u0438\u0442\u0430\u0435\u0442 <strong>\u0432\u0441\u0435<\/strong> \u0441\u0442\u0440\u043e\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043a\u043e\u043b\u043e\u043d\u043e\u043a.<\/p>\n<p><code><strong>COUNT(expression)<\/strong><\/code> \u0441\u0447\u0438\u0442\u0430\u0435\u0442 <strong>\u0442\u043e\u043b\u044c\u043a\u043e<\/strong> \u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0433\u0434\u0435 <code>expression IS NOT NULL<\/code>.<\/p>\n<pre><code class=\"sql\">SELECT COUNT(*) FROM users;       -- \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 SELECT COUNT(user_id) FROM users; -- \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435-NULL \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f user_id<\/code><\/pre>\n<h3>MVCC \u0438 visibility map<\/h3>\n<h4>MVCC\u2011\u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043f\u043e\u043b\u044f xmin\/xmax<\/h4>\n<p>PostgreSQL \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u0442\u0440\u043e\u043a  \u0432 \u043a\u0443\u0447\u0435. \u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442:<\/p>\n<ul>\n<li>\n<p><code>xmin<\/code>: XID \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0437\u0434\u0430\u043b\u0430 tuple;<\/p>\n<\/li>\n<li>\n<p><code>xmax<\/code>: XID \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u00ab\u0443\u0434\u0430\u043b\u0438\u043b\u0430\u00bb \u0438\u043b\u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u043b\u0430 \u0435\u0433\u043e (\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438).<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0447\u0442\u0435\u043d\u0438\u0438 \u0434\u0432\u0438\u0436\u043e\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0432\u0438\u0434\u043d\u0430 \u043b\u0438 \u044d\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u0410\u043a\u0442\u0438\u0432\u043d\u0430 \u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441 <code>xmin<\/code>?<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430 \u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441 <code>xmax<\/code>?<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438 \u043e\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u00ab\u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438\u00bb \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438?<\/p>\n<\/li>\n<\/ol>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u00ab\u0436\u0438\u0432\u0430 \u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u00bb, PostgreSQL \u0447\u0438\u0442\u0430\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a tuple \u0438 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439.<\/p>\n<h4>Visibility map<\/h4>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u2014 \u0434\u043e\u0440\u043e\u0433\u043e. \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u044f heap \u043f\u0440\u0438 index\u2011only scan, \u0435\u0441\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430:<\/p>\n<p><strong>Visibility map<\/strong>: \u0431\u0438\u0442\u043e\u0432\u0430\u044f \u043a\u0430\u0440\u0442\u0430, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0438\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 <strong>\u0446\u0435\u043b\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435<\/strong> (8\u00a0kB) \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e <strong>\u0432\u0441\u0435<\/strong> \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0432\u0438\u0434\u0438\u043c\u044b \u0412\u0421\u0415\u041c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c.<\/p>\n<p><strong>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435 <code>VACUUM<\/code> \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442\u0441\u044f all\u2011visible, \u0435\u0441\u043b\u0438 \u043d\u0430 \u043d\u0435\u0439 \u043d\u0435\u0442 \u00ab\u0433\u0440\u044f\u0437\u043d\u044b\u0445\u00bb \u0432\u0435\u0440\u0441\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041b\u044e\u0431\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\/\u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u044e\u0442 \u0431\u0438\u0442 \u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0439 <code>VACUUM<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0431\u0438\u0442.<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0431\u0438\u0442=1, \u0438\u043d\u0434\u0435\u043a\u0441\u043d\u044b\u0439 \u0441\u043a\u0430\u043d \u0434\u043e\u0432\u0435\u0440\u044f\u0435\u0442 visibility map \u0438 \u043d\u0435 \u0445\u043e\u0434\u0438\u0442 \u0432 heap.<\/p>\n<h3>\u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439: COUNT(*) \u2192 Seq Scan<\/h3>\n<p><strong>\u041d\u0435\u0442 \u00ab\u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438\u00bb<\/strong> \u043a \u043a\u0430\u043a\u043e\u0439\u2011\u043b\u0438\u0431\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0435, \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441. \u0414\u0430\u0436\u0435 \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043f\u043e <code>id<\/code> \u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e <code>xmin\/xmax<\/code>, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0447\u0435\u0440\u0435\u0437 heap \u2192 Seq Scan.<\/p>\n<pre><code class=\"sql\">-- \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439 A: \u0431\u0430\u0437\u043e\u0432\u044b\u0439 full table scan \u0434\u043b\u044f COUNT(*) EXPLAIN (ANALYZE, BUFFERS) SELECT COUNT(*) FROM t_events;<\/code><\/pre>\n<pre><code>Seq Scan on t_events  (cost=0.00..14923.00 rows=1000000 width=0)   (actual time=0.098..213.402 rows=1 loops=1)   Buffers: shared hit=8000<\/code><\/pre>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong>: ~213\u00a0ms. <strong>\u0421\u0442\u0440\u0430\u043d\u0438\u0446 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043d\u043e<\/strong>: 8000 (\u0432\u0441\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b).<\/p>\n<h4>\u041a\u043e\u0433\u0434\u0430 COUNT(col) \u043b\u0435\u0442\u0430\u0435\u0442: index\u2011only scan<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0435 <code>col<\/code> \u0438:<\/p>\n<ol>\n<li>\n<p><code>col NOT NULL<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0443 \u043a\u043e\u043b\u043e\u043d\u043a\u0443;<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b all\u2011visible \u0432 visibility map;<\/p>\n<\/li>\n<\/ol>\n<p>\u0442\u043e PostgreSQL \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c <strong>Index\u2011only Scan<\/strong>:<\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_event_time ON t_events (event_time); VACUUM ANALYZE t_events;  EXPLAIN (ANALYZE, BUFFERS) SELECT COUNT(event_time) FROM t_events;<\/code><\/pre>\n<pre><code>Index Only Scan using idx_event_time on t_events   Index Cond: (event_time IS NOT NULL)   Heap Fetches: 0   Buffers: shared hit=300   (actual time=0.050..4.572 rows=1 loops=1)<\/code><\/pre>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong>: ~4.5\u00a0ms. <strong>\u0421\u0442\u0440\u0430\u043d\u0438\u0446<\/strong> (\u0438\u043d\u0434\u0435\u043a\u0441): 300. <strong>Heap Fetches<\/strong>: 0<\/p>\n<p><strong>\u0412\u044b\u0438\u0433\u0440\u044b\u0448<\/strong>: \u224850\u00d7 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 full table scan.<\/p>\n<h3>Counter\u2011intuitive: COUNT(*) \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441<\/h3>\n<h4>Parallel Index\u2011only Scan (PostgreSQL\u00a012+)<\/h4>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 PostgreSQL\u00a012 \u0434\u0432\u0438\u0436\u043e\u043a \u0443\u043c\u0435\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u041f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c <strong>Parallel Index\u2011only Scan<\/strong> \u0434\u043b\u044f <code>COUNT(*)<\/code>,<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e <strong>NOT\u00a0NULL<\/strong> \u043f\u043e\u043b\u044e (\u0434\u0430\u0436\u0435 \u0431\u0435\u0437 INCLUDE),<\/p>\n<\/li>\n<li>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u0430\u0432\u043d\u043e \u043d\u0435 \u043c\u0435\u043d\u044f\u043b\u0430\u0441\u044c (visibility map \u0447\u0438\u0441\u0442\u0430),<\/p>\n<\/li>\n<li>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u0432\u043a\u043b\u044e\u0447\u0451\u043d (<code>max_parallel_workers_per_gather &gt; 0<\/code>).<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432 (\u0442\u0430\u0431\u043b\u0438\u0446\u0430 <code>tmp_test<\/code>):<\/p>\n<pre><code class=\"sql\">CREATE TABLE tmp_test (   num integer NULL,   val varchar );  -- \u041d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c 1\u00a0000\u00a0000 \u0441\u0442\u0440\u043e\u043a: INSERT INTO tmp_test (num, val) SELECT CASE WHEN i%2=0 THEN NULL ELSE 12345 END,        repeat(G.i::text, 256) FROM generate_series(1, 1000000) G(i);  -- \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e nullable-\u043f\u043e\u043b\u044e num: CREATE INDEX tmp_test_some_idx ON tmp_test(num);  -- \u041f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 VACUUM \u0434\u043b\u044f visibility map VACUUM tmp_test;  -- \u0421\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438 EXPLAIN (ANALYZE, BUFFERS) SELECT COUNT(*) FROM tmp_test;<\/code><\/pre>\n<pre><code>Finalize Aggregate  (cost=10966.90..10966.91 rows=1 width=8)   (actual time=29.826..32.236 rows=1 loops=1)   -&gt;  Gather         Workers Launched: 3         -&gt;  Partial Aggregate               -&gt;  Parallel Index Only Scan using tmp_test_some_idx                  Heap Fetches: 0                  (actual time=0.127..17.083 rows=250000 loops=4)<\/code><\/pre>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong>: ~32\u00a0ms (\u0432\u043c\u0435\u0441\u0442\u043e ~206\u00a0ms \u0431\u0435\u0437 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0438 index\u2011only).<strong>Heap Fetches<\/strong>: 0 \u2014 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0432\u0441\u0451, visibility map \u0447\u0438\u0441\u0442<\/p>\n<blockquote>\n<p><strong>\u0412\u044b\u0432\u043e\u0434<\/strong>: \u0434\u043b\u044f <code>COUNT(*)<\/code> \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e<\/p>\n<ol>\n<li>\n<p>\u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043f\u043e NOT\u00a0NULL-\u043f\u043e\u043b\u044e,<\/p>\n<\/li>\n<li>\n<p>\u0447\u0438\u0441\u0442\u043e\u0439 visibility map,<\/p>\n<\/li>\n<li>\n<p>\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u2014 \u0438 \u0434\u0432\u0438\u0436\u043e\u043a \u043f\u043e\u0439\u0434\u0451\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443.<\/p>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h4>\u0412\u0441\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430<\/h4>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0417\u0430\u043f\u0440\u043e\u0441<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041f\u043b\u0430\u043d<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0412\u0440\u0435\u043c\u044f<\/p>\n<\/th>\n<th>\n<p align=\"left\">Buffers \/ Heap Fetches<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(*)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">Seq Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~213\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=8000<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(event_time)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">Index\u2011only Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~4.5\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=300 \/ 0 fetches<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(user_id)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">Seq Scan (\u0431\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u0430)<\/p>\n<\/td>\n<td>\n<p align=\"left\">~230\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=8000<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(user_id)<\/code> (\u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c)<\/p>\n<\/td>\n<td>\n<p align=\"left\">Index\u2011only Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~~6\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit\u2248300 \/ 0 fetches<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(1)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0422\u043e \u0436\u0435, \u0447\u0442\u043e <code>COUNT(*)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">~213\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=8000<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(*)<\/code> (PG\u00a014+, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439)<\/p>\n<\/td>\n<td>\n<p align=\"left\">Parallel Index\u2011only Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~~30\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit\u2248300 \/ 0 fetches<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3>COUNT(DISTINCT) \u2014 \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0435 \u0434\u0435\u043b\u043e<\/h3>\n<p><code>COUNT(DISTINCT col)<\/code> \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c: \u0434\u0432\u0438\u0436\u043a\u0443 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f:<\/p>\n<ol>\n<li>\n<p><strong>\u0421\u043e\u0431\u0440\u0430\u0442\u044c<\/strong> \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>col<\/code> (\u0438\u043d\u0434\u0435\u043a\u0441\u043d\u044b\u0439 \u0438\u043b\u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043a\u0430\u043d);<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0441\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/strong> \u0438\u043b\u0438 <strong>\u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0445\u044d\u0448\u2011\u0442\u0430\u0431\u043b\u0438\u0446\u0443<\/strong> \u0432 \u043f\u0430\u043c\u044f\u0442\u0438;<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043e\u0431\u044a\u0451\u043c\u0435 \u2014 spill \u0432 temp\u2011\u0444\u0430\u0439\u043b\u044b, merge\u2011\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"sql\">EXPLAIN ANALYZE SELECT COUNT(DISTINCT user_id) FROM t_events;<\/code><\/pre>\n<p>\u041f\u043b\u0430\u043d \u043e\u0431\u044b\u0447\u043d\u043e:<\/p>\n<pre><code>Aggregate   -&gt;  Index Only Scan using idx_user_id on t_events<\/code><\/pre>\n<p>\u041d\u043e \u0432 <strong>Aggregate<\/strong> \u043b\u0435\u0436\u0438\u0442 \u0441\u0430\u043c <code>DISTINCT<\/code>, \u0438 \u043e\u043d \u0434\u043e \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u0435\u0440\u0436\u0438\u0442 \u0445\u044d\u0448\/\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u2014 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 CPU\/RAM\/Disk.<\/p>\n<p><strong>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430<\/strong>:<\/p>\n<ul>\n<li>\n<p><code>approx_count_distinct(col)<\/code> \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <code>hyperloglog<\/code>,<\/p>\n<\/li>\n<li>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b (PostGIS\u2011Tiger\u2011Geocoder \u0442\u043e\u0436\u0435 \u0434\u0430\u0451\u0442 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b),<\/p>\n<\/li>\n<li>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439, \u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0414\u043b\u044f OLTP\u2011\u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0432\u044b\u0431\u0438\u0440\u0430\u0439\u0442\u0435 <code>COUNT(col)<\/code> \u043f\u043e \u043f\u043e\u043b\u044e \u0441 <code>NOT\u00a0NULL<\/code> \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0438 \u0441\u0432\u0435\u0436\u0438\u043c <code>VACUUM<\/code>. \u0415\u0441\u043b\u0438 \u0432\u0441\u0451\u2011\u0442\u0430\u043a\u0438 \u043d\u0443\u0436\u0435\u043d <code>COUNT(*)<\/code>, \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e \u043b\u044e\u0431\u043e\u043c\u0443 <code>NOT\u00a0NULL<\/code> \u043f\u043e\u043b\u044e \u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c, \u0447\u0442\u043e\u0431\u044b \u0434\u0432\u0438\u0436\u043e\u043a \u043c\u043e\u0433 \u043f\u043e\u0439\u0442\u0438 \u0432 Index\u2011only Scan.<\/p>\n<p>\u0412 OLAP\u2011\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430\u0445 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 <code>COUNT(*)<\/code> \u0447\u0435\u0440\u0435\u0437 Seq\u00a0Scan \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0440\u043a\u0435\u0440\u044b \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0434\u0430\u0442\u0435 <code>date\u2010partitioning<\/code> \u2014 \u0442\u0430\u043a \u0432\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u0435 \u043e\u0431\u044a\u0451\u043c \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u043a\u0443\u0447\u0438. \u0410 \u0434\u043b\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e <code>COUNT(DISTINCT)<\/code> \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u043d\u0430 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0451\u043d\u043d\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0442\u0438\u043f\u0430 HyperLogLog <code>approx_count_distinct()<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u044b\u0432\u0430\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432 temp\u2011\u0444\u0430\u0439\u043b\u044b \u0438 \u043d\u0435 \u0431\u0438\u0442\u044c \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044e:  \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u0442\u0435 <code>autovacuum<\/code> \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>vacuum_scale_factor<\/code>, <code>vacuum_freeze_table_age<\/code> \u0438 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u044c\u0442\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 visibility map \u0432 <code>pg_visibility<\/code> \u2014 \u044d\u0442\u043e \u043a\u043b\u044e\u0447 \u043a \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u0430\u0448\u0438\u0445 <code>index\u2010only<\/code> \u043f\u043b\u0430\u043d\u043e\u0432.<\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0431\u043b\u0438\u0437\u043a\u0438 \u0442\u0435\u043c\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u044b\u0436\u0430\u0442\u044c \u0438\u0437 \u0431\u0430\u0437\u044b \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u2014 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a <strong>22 \u0430\u043f\u0440\u0435\u043b\u044f<\/strong>, \u0433\u0434\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u043a\u0430\u043a \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0443\u0442\u0438\u043d\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440 \u0432 PostgreSQL \u0438 MS SQL Server. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043f\u043e\u043a\u0430\u0436\u0435\u043c, \u043a\u0430\u043a \u043e\u0444\u043e\u0440\u043c\u043b\u044f\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438, \u043f\u043e\u0432\u044b\u0448\u0430\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u2014 \u0432\u0441\u0451, \u0447\u0442\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u043d\u0435\u0440\u0432\u044b \u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/ifgM\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 \u00abSQL \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u043e\u0432\u00bb.<\/a><\/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\/901922\/\"> https:\/\/habr.com\/ru\/articles\/901922\/<\/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>\u0422\u0435\u043c\u0430 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e\u0439: \u0437\u0430\u0447\u0435\u043c <code>COUNT(col)<\/code>, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c <code>COUNT(*)<\/code>? \u041d\u043e \u0437\u0430 \u043d\u0435\u0439 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0443\u0447\u0430 \u043d\u044e\u0430\u043d\u0441\u043e\u0432: MVCC\u2011\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, visibility map, index\u2011only scan, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u2026 \u0418 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 PostgreSQL <code>COUNT(*)<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f \u043f\u043e\u2011\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0432\u0441\u0451 \u0434\u043e\u0441\u043a\u043e\u043d\u0430\u043b\u044c\u043d\u043e.<\/p>\n<h4>\u0421\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0430 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443 SQL<\/h4>\n<p><code><strong>COUNT(*)<\/strong><\/code> \u0441\u0447\u0438\u0442\u0430\u0435\u0442 <strong>\u0432\u0441\u0435<\/strong> \u0441\u0442\u0440\u043e\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043a\u043e\u043b\u043e\u043d\u043e\u043a.<\/p>\n<p><code><strong>COUNT(expression)<\/strong><\/code> \u0441\u0447\u0438\u0442\u0430\u0435\u0442 <strong>\u0442\u043e\u043b\u044c\u043a\u043e<\/strong> \u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u0433\u0434\u0435 <code>expression IS NOT NULL<\/code>.<\/p>\n<pre><code class=\"sql\">SELECT COUNT(*) FROM users;       -- \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 SELECT COUNT(user_id) FROM users; -- \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435-NULL \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f user_id<\/code><\/pre>\n<h3>MVCC \u0438 visibility map<\/h3>\n<h4>MVCC\u2011\u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043f\u043e\u043b\u044f xmin\/xmax<\/h4>\n<p>PostgreSQL \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u0442\u0440\u043e\u043a  \u0432 \u043a\u0443\u0447\u0435. \u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442:<\/p>\n<ul>\n<li>\n<p><code>xmin<\/code>: XID \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0437\u0434\u0430\u043b\u0430 tuple;<\/p>\n<\/li>\n<li>\n<p><code>xmax<\/code>: XID \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u00ab\u0443\u0434\u0430\u043b\u0438\u043b\u0430\u00bb \u0438\u043b\u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u043b\u0430 \u0435\u0433\u043e (\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438).<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0447\u0442\u0435\u043d\u0438\u0438 \u0434\u0432\u0438\u0436\u043e\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0432\u0438\u0434\u043d\u0430 \u043b\u0438 \u044d\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u0410\u043a\u0442\u0438\u0432\u043d\u0430 \u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441 <code>xmin<\/code>?<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430 \u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441 <code>xmax<\/code>?<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438 \u043e\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u00ab\u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438\u00bb \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438?<\/p>\n<\/li>\n<\/ol>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u00ab\u0436\u0438\u0432\u0430 \u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u00bb, PostgreSQL \u0447\u0438\u0442\u0430\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a tuple \u0438 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439.<\/p>\n<h4>Visibility map<\/h4>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u2014 \u0434\u043e\u0440\u043e\u0433\u043e. \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u044f heap \u043f\u0440\u0438 index\u2011only scan, \u0435\u0441\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430:<\/p>\n<p><strong>Visibility map<\/strong>: \u0431\u0438\u0442\u043e\u0432\u0430\u044f \u043a\u0430\u0440\u0442\u0430, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0438\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 <strong>\u0446\u0435\u043b\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435<\/strong> (8\u00a0kB) \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e <strong>\u0432\u0441\u0435<\/strong> \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0432\u0438\u0434\u0438\u043c\u044b \u0412\u0421\u0415\u041c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c.<\/p>\n<p><strong>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/strong>:<\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435 <code>VACUUM<\/code> \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442\u0441\u044f all\u2011visible, \u0435\u0441\u043b\u0438 \u043d\u0430 \u043d\u0435\u0439 \u043d\u0435\u0442 \u00ab\u0433\u0440\u044f\u0437\u043d\u044b\u0445\u00bb \u0432\u0435\u0440\u0441\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041b\u044e\u0431\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\/\u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u044e\u0442 \u0431\u0438\u0442 \u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0439 <code>VACUUM<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0431\u0438\u0442.<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0431\u0438\u0442=1, \u0438\u043d\u0434\u0435\u043a\u0441\u043d\u044b\u0439 \u0441\u043a\u0430\u043d \u0434\u043e\u0432\u0435\u0440\u044f\u0435\u0442 visibility map \u0438 \u043d\u0435 \u0445\u043e\u0434\u0438\u0442 \u0432 heap.<\/p>\n<h3>\u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439: COUNT(*) \u2192 Seq Scan<\/h3>\n<p><strong>\u041d\u0435\u0442 \u00ab\u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438\u00bb<\/strong> \u043a \u043a\u0430\u043a\u043e\u0439\u2011\u043b\u0438\u0431\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0435, \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441. \u0414\u0430\u0436\u0435 \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043f\u043e <code>id<\/code> \u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e <code>xmin\/xmax<\/code>, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0447\u0435\u0440\u0435\u0437 heap \u2192 Seq Scan.<\/p>\n<pre><code class=\"sql\">-- \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439 A: \u0431\u0430\u0437\u043e\u0432\u044b\u0439 full table scan \u0434\u043b\u044f COUNT(*) EXPLAIN (ANALYZE, BUFFERS) SELECT COUNT(*) FROM t_events;<\/code><\/pre>\n<pre><code>Seq Scan on t_events  (cost=0.00..14923.00 rows=1000000 width=0)   (actual time=0.098..213.402 rows=1 loops=1)   Buffers: shared hit=8000<\/code><\/pre>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong>: ~213\u00a0ms. <strong>\u0421\u0442\u0440\u0430\u043d\u0438\u0446 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043d\u043e<\/strong>: 8000 (\u0432\u0441\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b).<\/p>\n<h4>\u041a\u043e\u0433\u0434\u0430 COUNT(col) \u043b\u0435\u0442\u0430\u0435\u0442: index\u2011only scan<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0435 <code>col<\/code> \u0438:<\/p>\n<ol>\n<li>\n<p><code>col NOT NULL<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0418\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0443 \u043a\u043e\u043b\u043e\u043d\u043a\u0443;<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b all\u2011visible \u0432 visibility map;<\/p>\n<\/li>\n<\/ol>\n<p>\u0442\u043e PostgreSQL \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c <strong>Index\u2011only Scan<\/strong>:<\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_event_time ON t_events (event_time); VACUUM ANALYZE t_events;  EXPLAIN (ANALYZE, BUFFERS) SELECT COUNT(event_time) FROM t_events;<\/code><\/pre>\n<pre><code>Index Only Scan using idx_event_time on t_events   Index Cond: (event_time IS NOT NULL)   Heap Fetches: 0   Buffers: shared hit=300   (actual time=0.050..4.572 rows=1 loops=1)<\/code><\/pre>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong>: ~4.5\u00a0ms. <strong>\u0421\u0442\u0440\u0430\u043d\u0438\u0446<\/strong> (\u0438\u043d\u0434\u0435\u043a\u0441): 300. <strong>Heap Fetches<\/strong>: 0<\/p>\n<p><strong>\u0412\u044b\u0438\u0433\u0440\u044b\u0448<\/strong>: \u224850\u00d7 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 full table scan.<\/p>\n<h3>Counter\u2011intuitive: COUNT(*) \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441<\/h3>\n<h4>Parallel Index\u2011only Scan (PostgreSQL\u00a012+)<\/h4>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 PostgreSQL\u00a012 \u0434\u0432\u0438\u0436\u043e\u043a \u0443\u043c\u0435\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u041f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c <strong>Parallel Index\u2011only Scan<\/strong> \u0434\u043b\u044f <code>COUNT(*)<\/code>,<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e <strong>NOT\u00a0NULL<\/strong> \u043f\u043e\u043b\u044e (\u0434\u0430\u0436\u0435 \u0431\u0435\u0437 INCLUDE),<\/p>\n<\/li>\n<li>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u0430\u0432\u043d\u043e \u043d\u0435 \u043c\u0435\u043d\u044f\u043b\u0430\u0441\u044c (visibility map \u0447\u0438\u0441\u0442\u0430),<\/p>\n<\/li>\n<li>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u0432\u043a\u043b\u044e\u0447\u0451\u043d (<code>max_parallel_workers_per_gather &gt; 0<\/code>).<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432 (\u0442\u0430\u0431\u043b\u0438\u0446\u0430 <code>tmp_test<\/code>):<\/p>\n<pre><code class=\"sql\">CREATE TABLE tmp_test (   num integer NULL,   val varchar );  -- \u041d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c 1\u00a0000\u00a0000 \u0441\u0442\u0440\u043e\u043a: INSERT INTO tmp_test (num, val) SELECT CASE WHEN i%2=0 THEN NULL ELSE 12345 END,        repeat(G.i::text, 256) FROM generate_series(1, 1000000) G(i);  -- \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e nullable-\u043f\u043e\u043b\u044e num: CREATE INDEX tmp_test_some_idx ON tmp_test(num);  -- \u041f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 VACUUM \u0434\u043b\u044f visibility map VACUUM tmp_test;  -- \u0421\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438 EXPLAIN (ANALYZE, BUFFERS) SELECT COUNT(*) FROM tmp_test;<\/code><\/pre>\n<pre><code>Finalize Aggregate  (cost=10966.90..10966.91 rows=1 width=8)   (actual time=29.826..32.236 rows=1 loops=1)   -&gt;  Gather         Workers Launched: 3         -&gt;  Partial Aggregate               -&gt;  Parallel Index Only Scan using tmp_test_some_idx                  Heap Fetches: 0                  (actual time=0.127..17.083 rows=250000 loops=4)<\/code><\/pre>\n<p><strong>\u0412\u0440\u0435\u043c\u044f<\/strong>: ~32\u00a0ms (\u0432\u043c\u0435\u0441\u0442\u043e ~206\u00a0ms \u0431\u0435\u0437 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0438 index\u2011only).<strong>Heap Fetches<\/strong>: 0 \u2014 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0432\u0441\u0451, visibility map \u0447\u0438\u0441\u0442<\/p>\n<blockquote>\n<p><strong>\u0412\u044b\u0432\u043e\u0434<\/strong>: \u0434\u043b\u044f <code>COUNT(*)<\/code> \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e<\/p>\n<ol>\n<li>\n<p>\u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043f\u043e NOT\u00a0NULL-\u043f\u043e\u043b\u044e,<\/p>\n<\/li>\n<li>\n<p>\u0447\u0438\u0441\u0442\u043e\u0439 visibility map,<\/p>\n<\/li>\n<li>\n<p>\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u2014 \u0438 \u0434\u0432\u0438\u0436\u043e\u043a \u043f\u043e\u0439\u0434\u0451\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443.<\/p>\n<\/li>\n<\/ol>\n<\/blockquote>\n<h4>\u0412\u0441\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430<\/h4>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0417\u0430\u043f\u0440\u043e\u0441<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041f\u043b\u0430\u043d<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0412\u0440\u0435\u043c\u044f<\/p>\n<\/th>\n<th>\n<p align=\"left\">Buffers \/ Heap Fetches<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(*)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">Seq Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~213\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=8000<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(event_time)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">Index\u2011only Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~4.5\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=300 \/ 0 fetches<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(user_id)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">Seq Scan (\u0431\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u0430)<\/p>\n<\/td>\n<td>\n<p align=\"left\">~230\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=8000<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(user_id)<\/code> (\u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c)<\/p>\n<\/td>\n<td>\n<p align=\"left\">Index\u2011only Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~~6\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit\u2248300 \/ 0 fetches<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(1)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0422\u043e \u0436\u0435, \u0447\u0442\u043e <code>COUNT(*)<\/code><\/p>\n<\/td>\n<td>\n<p align=\"left\">~213\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit=8000<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\"><code>SELECT COUNT(*)<\/code> (PG\u00a014+, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439)<\/p>\n<\/td>\n<td>\n<p align=\"left\">Parallel Index\u2011only Scan<\/p>\n<\/td>\n<td>\n<p align=\"left\">~~30\u00a0ms<\/p>\n<\/td>\n<td>\n<p align=\"left\">shared hit\u2248300 \/ 0 fetches<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3>COUNT(DISTINCT) \u2014 \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0435 \u0434\u0435\u043b\u043e<\/h3>\n<p><code>COUNT(DISTINCT col)<\/code> \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c: \u0434\u0432\u0438\u0436\u043a\u0443 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f:<\/p>\n<ol>\n<li>\n<p><strong>\u0421\u043e\u0431\u0440\u0430\u0442\u044c<\/strong> \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>col<\/code> (\u0438\u043d\u0434\u0435\u043a\u0441\u043d\u044b\u0439 \u0438\u043b\u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043a\u0430\u043d);<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0441\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/strong> \u0438\u043b\u0438 <strong>\u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0445\u044d\u0448\u2011\u0442\u0430\u0431\u043b\u0438\u0446\u0443<\/strong> \u0432 \u043f\u0430\u043c\u044f\u0442\u0438;<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043e\u0431\u044a\u0451\u043c\u0435 \u2014 spill \u0432 temp\u2011\u0444\u0430\u0439\u043b\u044b, merge\u2011\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"sql\">EXPLAIN ANALYZE SELECT COUNT(DISTINCT user_id) FROM t_events;<\/code><\/pre>\n<p>\u041f\u043b\u0430\u043d \u043e\u0431\u044b\u0447\u043d\u043e:<\/p>\n<pre><code>Aggregate   -&gt;  Index Only Scan using idx_user_id on t_events<\/code><\/pre>\n<p>\u041d\u043e \u0432 <strong>Aggregate<\/strong> \u043b\u0435\u0436\u0438\u0442 \u0441\u0430\u043c <code>DISTINCT<\/code>, \u0438 \u043e\u043d \u0434\u043e \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u0435\u0440\u0436\u0438\u0442 \u0445\u044d\u0448\/\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u2014 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 CPU\/RAM\/Disk.<\/p>\n<p><strong>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430<\/strong>:<\/p>\n<ul>\n<li>\n<p><code>approx_count_distinct(col)<\/code> \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <code>hyperloglog<\/code>,<\/p>\n<\/li>\n<li>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b (PostGIS\u2011Tiger\u2011Geocoder \u0442\u043e\u0436\u0435 \u0434\u0430\u0451\u0442 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b),<\/p>\n<\/li>\n<li>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439, \u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0414\u043b\u044f OLTP\u2011\u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0432\u044b\u0431\u0438\u0440\u0430\u0439\u0442\u0435 <code>COUNT(col)<\/code> \u043f\u043e \u043f\u043e\u043b\u044e \u0441 <code>NOT\u00a0NULL<\/code> \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0438 \u0441\u0432\u0435\u0436\u0438\u043c <code>VACUUM<\/code>. \u0415\u0441\u043b\u0438 \u0432\u0441\u0451\u2011\u0442\u0430\u043a\u0438 \u043d\u0443\u0436\u0435\u043d <code>COUNT(*)<\/code>, \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e \u043b\u044e\u0431\u043e\u043c\u0443 <code>NOT\u00a0NULL<\/code> \u043f\u043e\u043b\u044e \u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c, \u0447\u0442\u043e\u0431\u044b \u0434\u0432\u0438\u0436\u043e\u043a \u043c\u043e\u0433 \u043f\u043e\u0439\u0442\u0438 \u0432 Index\u2011only Scan.<\/p>\n<p>\u0412 OLAP\u2011\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430\u0445 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 <code>COUNT(*)<\/code> \u0447\u0435\u0440\u0435\u0437 Seq\u00a0Scan \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0440\u043a\u0435\u0440\u044b \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0434\u0430\u0442\u0435 <code>date\u2010partitioning<\/code> \u2014 \u0442\u0430\u043a \u0432\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u0435 \u043e\u0431\u044a\u0451\u043c \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u043a\u0443\u0447\u0438. \u0410 \u0434\u043b\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e <code>COUNT(DISTINCT)<\/code> \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u043d\u0430 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0451\u043d\u043d\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0442\u0438\u043f\u0430 HyperLogLog <code>approx_count_distinct()<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u044b\u0432\u0430\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0432 temp\u2011\u0444\u0430\u0439\u043b\u044b \u0438 \u043d\u0435 \u0431\u0438\u0442\u044c \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044e:  \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u0442\u0435 <code>autovacuum<\/code> \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>vacuum_scale_factor<\/code>, <code>vacuum_freeze_table_age<\/code> \u0438 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u044c\u0442\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 visibility map \u0432 <code>pg_visibility<\/code> \u2014 \u044d\u0442\u043e \u043a\u043b\u044e\u0447 \u043a \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u0430\u0448\u0438\u0445 <code>index\u2010only<\/code> \u043f\u043b\u0430\u043d\u043e\u0432.<\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0431\u043b\u0438\u0437\u043a\u0438 \u0442\u0435\u043c\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u044b\u0436\u0430\u0442\u044c \u0438\u0437 \u0431\u0430\u0437\u044b \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u2014 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a <strong>22 \u0430\u043f\u0440\u0435\u043b\u044f<\/strong>, \u0433\u0434\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u043a\u0430\u043a \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0443\u0442\u0438\u043d\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440 \u0432 PostgreSQL \u0438 MS SQL Server. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043f\u043e\u043a\u0430\u0436\u0435\u043c, \u043a\u0430\u043a \u043e\u0444\u043e\u0440\u043c\u043b\u044f\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438, \u043f\u043e\u0432\u044b\u0448\u0430\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u2014 \u0432\u0441\u0451, \u0447\u0442\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u043d\u0435\u0440\u0432\u044b \u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/ifgM\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 \u00abSQL \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u043e\u0432\u00bb.<\/a><\/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\/901922\/\"> https:\/\/habr.com\/ru\/articles\/901922\/<\/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-457915","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/457915","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=457915"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/457915\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=457915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=457915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=457915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}