{"id":282729,"date":"2016-12-20T06:15:04","date_gmt":"2016-12-20T03:15:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=282729"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=282729","title":{"rendered":"\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 GROUP BY \u0432 PostgreSQL"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/454\/70a\/08a\/45470a08af92d4c1a7ca64b6f252de46.png\" alt=\"image\"\/><\/p>\n<p>  \u0421\u0440\u0430\u0437\u0443 \u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043d\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0432\u0435\u0442\u0430 \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0441\u043b\u0443\u0447\u0430\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043b\u0438\u0448\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0442\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0442\u044c\u0441\u044f \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u0421\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443<\/h3>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0430\u043a\u0443\u044e \u0441\u0445\u0435\u043c\u0443. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0438  <\/p>\n<ul>\n<li><code>content<\/code> \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b.<\/li>\n<li><code>content_keyword_ref<\/code> \u2014 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0441\u043b\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0435.<\/li>\n<\/ul>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/f83\/c94\/5c4\/f83c945c46b04a29e5fb12e50eac6c95.png\" alt=\"image\"\/><\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">CREATE TABLE<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">CREATE TABLE content (   id integer NOT NULL DEFAULT nextval('content_id_seq'::regclass),   some_data character varying(1000) NOT NULL,   CONSTRAINT content_pkey PRIMARY KEY (id), );  CREATE TABLE content_keyword_ref (   keyword_id integer NOT NULL,   content_id integer NOT NULL,   CONSTRAINT content_keyword_ref_pkey PRIMARY KEY (keyword_id, content_id),   CONSTRAINT content_keyword_ref_content_id_foreign FOREIGN KEY (content_id)       REFERENCES content (id) MATCH SIMPLE       ON UPDATE NO ACTION ON DELETE CASCADE,   CONSTRAINT content_keyword_ref_keyword_id_foreign FOREIGN KEY (keyword_id)       REFERENCES keywords (id) MATCH SIMPLE       ON UPDATE NO ACTION ON DELETE CASCADE ); CREATE INDEX content_keyword_ref_content_id_index   ON content_keyword_ref   USING btree   (content_id); CREATE INDEX content_keyword_ref_keyword_id_index   ON content_keyword_ref   USING btree   (keyword_id);<\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0443 \u043c\u0435\u043d\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0432 \u0431\u0430\u0437\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e 2 \u043c\u043b\u043d, \u0430 \u0441\u0432\u044f\u0437\u0435\u0439 \u0441 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e 15 \u043c\u043b\u043d.<\/p>\n<p>  \u0412\u044b\u0431\u0435\u0440\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u043e\u0434\u043d\u043e \u0438\u0437 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435<\/h3>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 (\u044f \u0441\u0440\u0430\u0437\u0443 \u0431\u0443\u0434\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c <code>EXPLAIN ANALYZE<\/code> \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u043b\u0430\u043d):<\/p>\n<pre><code class=\"sql\">EXPLAIN ANALYSE SELECT c.id  FROM content c JOIN content_keyword_ref r ON r.content_id = c.id     AND r.keyword_id IN (4713, 5951) GROUP BY c.id LIMIT 1000 <\/code><\/pre>\n<p>  GROUP BY \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u0432\u044b\u0432\u043e\u0434\u0435 \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430. \u0427\u0442\u043e \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0432 \u043f\u043b\u0430\u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre>Limit  (cost=21454.94..34933.16 rows=1000 width=4) (actual time=6.777..199.735 rows=1000 loops=1)   -&gt;  Group  (cost=21454.94..100235.11 rows=5845 width=4) (actual time=6.775..199.641 rows=1000 loops=1)         Group Key: c.id         -&gt;  Merge Join  (cost=21454.94..100220.49 rows=5845 width=4) (actual time=6.774..199.389 rows=1141 loops=1)               Merge Cond: (c.id = r.content_id)               -&gt;  Index Only Scan using content_pkey on content c  (cost=0.43..73221.47 rows=2182736 width=4) (actual time=0.013..131.942 rows=1339506 loops=1)                     Heap Fetches: 0               -&gt;  Sort  (cost=21454.51..21469.13 rows=5845 width=4) (actual time=6.662..6.792 rows=1141 loops=1)                     Sort Key: r.content_id                     Sort Method: quicksort  Memory: 143kB                     -&gt;  Bitmap Heap Scan on content_keyword_ref r  (cost=118.16..21088.82 rows=5845 width=4) (actual time=0.470..6.273 rows=2007 loops=1)                           Recheck Cond: (keyword_id = ANY ('{4713,5951}'::integer[]))                           Heap Blocks: exact=1781                           -&gt;  Bitmap Index Scan on content_keyword_ref_keyword_content_idx  (cost=0.00..116.70 rows=5845 width=0) (actual time=0.239..0.239 rows=2007 loops=1)                                 Index Cond: (keyword_id = ANY ('{4713,5951}'::integer[])) Planning time: 0.277 ms Execution time: 199.805 ms <\/pre>\n<p>  \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0431\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0432 DISTINCT \u0432\u043c\u0435\u0441\u0442\u043e GROUP BY:<\/p>\n<pre><code class=\"sql\">EXPLAIN ANALYSE SELECT DISTINCT c.id  FROM content c JOIN content_keyword_ref r ON r.content_id = c.id     AND r.keyword_id IN (4713, 5951) LIMIT 1000<\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c:<\/p>\n<pre>Limit  (cost=21454.94..34933.16 rows=1000 width=4) (actual time=2.824..187.619 rows=1000 loops=1)   -&gt;  Unique  (cost=21454.94..100235.11 rows=5845 width=4) (actual time=2.824..187.519 rows=1000 loops=1)         -&gt;  Merge Join  (cost=21454.94..100220.49 rows=5845 width=4) (actual time=2.823..187.351 rows=1141 loops=1)               Merge Cond: (c.id = r.content_id)               -&gt;  Index Only Scan using content_pkey on content c  (cost=0.43..73221.47 rows=2182736 width=4) (actual time=0.011..120.481 rows=1339506 loops=1)                     Heap Fetches: 0               -&gt;  Sort  (cost=21454.51..21469.13 rows=5845 width=4) (actual time=2.693..2.805 rows=1141 loops=1)                     Sort Key: r.content_id                     Sort Method: quicksort  Memory: 143kB                     -&gt;  Bitmap Heap Scan on content_keyword_ref r  (cost=118.16..21088.82 rows=5845 width=4) (actual time=0.463..2.321 rows=2007 loops=1)                           Recheck Cond: (keyword_id = ANY ('{4713,5951}'::integer[]))                           Heap Blocks: exact=1781                           -&gt;  Bitmap Index Scan on content_keyword_ref_keyword_content_idx  (cost=0.00..116.70 rows=5845 width=0) (actual time=0.235..0.235 rows=2007 loops=1)                                 Index Cond: (keyword_id = ANY ('{4713,5951}'::integer[])) Planning time: 0.264 ms Execution time: 187.727 ms <\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\u043c \u0438 \u0434\u0440\u0443\u0433\u0438\u043c \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u043c \u0440\u0430\u0441\u0445\u043e\u0434\u0430\u043c. \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0435\u043a\u0443\u043d\u0434!<\/p>\n<p>  \u041a\u0430\u043a \u0431\u044b\u0442\u044c?<\/p>\n<h3>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u041c\u043e\u0438 \u0438\u0434\u0435\u0438, \u043a\u0430\u043a \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u0440\u0438 \u0438\u043c\u0435\u044e\u0449\u0435\u0439\u0441\u044f \u0441\u0445\u0435\u043c\u0435, \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438\u0441\u044c. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0445\u0435\u043c\u0443. \u0422\u0430\u0431\u043b\u0438\u0447\u043a\u0443 <code>content<\/code> \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c. \u0410 \u0432\u043e\u0442 \u0441\u0432\u044f\u0437\u0438 \u0441 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435. \u0427\u0442\u043e\u0431\u044b \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0430\u043a\u0436\u0435 GiST \u0438\u043d\u0434\u0435\u043a\u0441. \u041e \u0442\u043e\u043c, \u043a\u0430\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/www.postgresql.org\/docs\/current\/static\/functions-array.html#ARRAY-OPERATORS-TABLE\">\u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 PostgreSQL<\/a>.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/a15\/3b6\/5e4\/a153b65e4c9b7565046e8f0085953a7f.png\" alt=\"image\"\/><\/p>\n<pre><code class=\"sql\">CREATE TABLE document (   content_id integer NOT NULL,   -- \u041d\u0430\u0448 \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432, \u0432\u0437\u0430\u043c\u0435\u043d \u0442\u0430\u0431\u043b\u0438\u0446\u044b content_keyword_ref   keyword_ids integer[] NOT NULL );  -- \u041d\u0430\u0448 GiST \u0438\u043d\u0434\u0435\u043a\u0441 CREATE INDEX document_keyword_ids_index ON document USING GiST(keyword_ids  gist__intbig_ops); <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0418 \u043c\u0435\u043d\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\"> CREATE INDEX document_content_id_index   ON public.document   USING btree   (content_id);  -- \u041a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0438\u043c \u0438\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 INSERT INTO document (content_id, keyword_ids) SELECT c.id, ARRAY(   SELECT r.keyword_id   FROM content_keyword_ref r    WHERE r.content_id = c.id ) FROM content c GROUP BY c.id;<\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u0430\u043a \u0438 \u0432 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445 \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"sql\">EXPLAIN ANALYZE SELECT c.id   FROM content c   JOIN document d ON d.content_id = c.id      AND d.keyword_ids && ARRAY[4713, 5951] limit 1000 <\/code><\/pre>\n<p>  \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043b\u0430\u043d:<\/p>\n<pre>Limit  (cost=387.80..7540.27 rows=1000 width=4) (actual time=8.799..12.935 rows=1000 loops=1)   -&gt;  Nested Loop  (cost=387.80..14177.77 rows=1928 width=4) (actual time=8.799..12.880 rows=1000 loops=1)         -&gt;  Bitmap Heap Scan on document d  (cost=387.37..6246.79 rows=1930 width=4) (actual time=8.786..10.599 rows=1000 loops=1)               Recheck Cond: (keyword_ids && '{4713,5951}'::integer[])               Rows Removed by Index Recheck: 107               Heap Blocks: exact=1008               -&gt;  Bitmap Index Scan on document_keyword_ids_index  (cost=0.00..386.89 rows=1930 width=0) (actual time=8.560..8.560 rows=1977 loops=1)                     Index Cond: (keyword_ids && '{4713,5951}'::integer[])         -&gt;  Index Only Scan using content_pkey on content c  (cost=0.43..4.10 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=1000)               Index Cond: (id = d.content_id)               Heap Fetches: 0 Planning time: 0.184 ms Execution time: 12.994 ms <\/pre>\n<p>  \u0412\u044b\u0433\u043e\u0434\u0430 \u0435\u0441\u0442\u044c, \u043f\u0440\u0438\u0447\u0435\u043c \u0437\u0430\u043c\u0435\u0442\u043d\u0430\u044f. \u041d\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 14 \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0422\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u0435\u0449\u0435 \u0432\u044b\u0433\u043e\u0434\u044b \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438.<\/p>\n<h3>\u0411\u043e\u043d\u0443\u0441<\/h3>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u0434\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0441 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0435\u0439. \u041a\u0430\u043a \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0432 \u00ab\u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u00bb \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435? \u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432:<\/p>\n<p>  \u0421\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0441 <code>GROUP BY:<\/code>  <\/p>\n<pre><code class=\"sql\">SELECT COUNT(1) FROM (     SELECT c.id      FROM content c     JOIN content_keyword_ref r ON r.content_id = c.id         AND r.keyword_id IN (4713, 5951)     GROUP BY c.id ) t;<\/code><\/pre>\n<p>  \u0421\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0441 <code>DISTINCT<\/code>:  <\/p>\n<pre><code class=\"sql\">SELECT COUNT(1) FROM (     SELECT DISTINCT(c.id)     FROM content c     JOIN content_keyword_ref r ON r.content_id = c.id         AND r.keyword_id IN (4713, 5951) ) t;<\/code><\/pre>\n<p>  \u0421\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0431\u0435\u0437 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>COUNT (DISTINCT columns)<\/code>:  <\/p>\n<pre><code class=\"sql\">SELECT COUNT(DISTINCT c.id) FROM content c JOIN content_keyword_ref r ON r.content_id = c.id     AND r.keyword_id IN (4713, 5951)<\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0434\u0430\u0436\u0435 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"sql\">SELECT COUNT(1) OVER() FROM content c JOIN content_keyword_ref r ON r.content_id = c.id     AND r.keyword_id IN (4713, 5951) GROUP BY c.id LIMIT 1<\/code><\/pre>\n<p>  \u0412\u043e \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445 \u043c\u0438\u043d\u0443\u0441 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0411\u0443\u0434\u0435\u0442 \u043b\u0438 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432? Laravel, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/github.com\/laravel\/framework\/blob\/53f97a014da380dc85fb4b0d826475e562d78dcc\/src\/Illuminate\/Database\/Query\/Builder.php#L1716\">\u043d\u0435\u0442<\/a>. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u043d \u0432\u044b\u0431\u0435\u0440\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>count()<\/code> \u0443\u0436\u0435 \u043d\u0430 PHP. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u0435 \u0432\u044b\u0447\u0438\u0442\u044b\u0432\u0430\u043b\u0430\u0441\u044c \u0438\u0437 \u0431\u0430\u0437\u044b \u0432\u0441\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430.<\/p>\n<p>  \u041a\u0430\u043a \u043c\u044b \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"sql\">SELECT COUNT(1) FROM document d  WHERE d.keyword_ids && ARRAY[4713, 5951]<\/code><\/pre>\n<p>  \u0413\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u0435\u0435 \u0438 \u043d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043f\u0430\u0433\u0438\u043d\u0430\u0442\u043e\u0440\u043e\u043c.<\/p>\n<h3>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u0431\u043e\u043d\u0443\u0441<\/h3>\n<p>  \u041c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u043b\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u043e \u0438\u0437 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u043e\u0432. \u0427\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u0432\u0441\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0441\u043b\u043e\u0432\u0430? \u0412 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">SELECT c.id  FROM content c JOIN content_keyword_ref r1 ON r1.content_id = c.id     AND r1.keyword_id = 5388 JOIN content_keyword_ref r2 ON r2.content_id = c.id     AND r2.keyword_id = 5951 LIMIT 1000<\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432 \u0438\u0449\u0435\u043c, \u0441\u0442\u043e\u043b\u044c\u043a\u043e JOIN-\u043e\u0432 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c. \u0415\u0441\u043b\u0438 \u043c\u044b \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0442\u043e \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>@&gt;<\/code>. \u0422\u043e\u0433\u0434\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e:<\/p>\n<pre><code class=\"sql\">SELECT c.id FROM content c JOIN document d ON d.content_id = c.id      AND d.keyword_ids @&gt; ARRAY[5388, 5951] LIMIT 1000<\/code><\/pre>\n<p>  \u0414\u0430 \u0438 \u043f\u043b\u0430\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0443 \u043d\u0435\u0433\u043e \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0435.<\/p>\n<p>  \u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u044f \u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0432\u044b\u0448\u0435, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438.<\/p>\n<h3>\u0420\u0435\u0437\u044e\u043c\u0435<\/h3>\n<p>  \u042f \u043f\u043e\u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0434\u0430\u0435\u0442 \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0432 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043e\u0442 2 \u0434\u043e 10 \u0440\u0430\u0437. \u041d\u043e \u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c-\u0442\u0430\u043a\u0438 \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u0432\u044b\u0434\u0430\u0447\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 1.5-2 \u0440\u0430\u0437\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u00ab\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e\u00bb \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430.<\/p>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u0432 \u0446\u0435\u043b\u043e\u043c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u0443\u0434\u0430\u0447\u043d\u044b\u043c. \u041d\u043e \u0435\u0441\u043b\u0438 \u0440\u0435\u0448\u0438\u0442\u0435\u0441\u044c \u043d\u0430 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u0438, \u0442\u043e \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d \u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0432\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<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:\/\/habrahabr.ru\/post\/317980\/\"> https:\/\/habrahabr.ru\/post\/317980\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/454\/70a\/08a\/45470a08af92d4c1a7ca64b6f252de46.png\" alt=\"image\"\/><\/p>\n<p>  \u0421\u0440\u0430\u0437\u0443 \u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043d\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0432\u0435\u0442\u0430 \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0441\u043b\u0443\u0447\u0430\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043b\u0438\u0448\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0442\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0442\u044c\u0441\u044f \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/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-282729","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/282729","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=282729"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/282729\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=282729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=282729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=282729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}