{"id":405850,"date":"2024-06-29T18:34:06","date_gmt":"2024-06-29T18:34:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=405850"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=405850","title":{"rendered":"<span>1C (\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e) c PostgreSQL<\/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<h2>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0423\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441\u00a0\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 1\u0421 \u043d\u0430\u00a0PostgreSQL \u0432\u00a0\u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u00a0MS SQL \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u00a0\u0431\u044b\u0441\u0442\u0440\u043e. \u0422\u043e\u0433\u0434\u0430\u00a0\u0436\u0435 \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e\u00a0\u0432 99% \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0442\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u00a0\u043e\u043d\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u0430\u0436\u0435\u00a0\u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043d\u0430\u00a0MS SQL, \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0432\u0441\u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432\u043e\u00a0\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0422\u043e\u0433\u0434\u0430\u00a0\u0436\u0435\u00a0\u0431\u044b\u043b\u043e \u044f\u0441\u043d\u043e, \u0447\u0442\u043e\u00a0\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0442\u0438\u043f\u043e\u0432\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u00a0\u0445\u043e\u0447\u0435\u0442\u0441\u044f. \u041b\u0430\u0434\u043d\u043e \u0435\u0449\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446, \u043d\u043e\u00a0\u0434\u043b\u044f\u00a0\u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432\u043e\u00a0\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 1\u0421 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0434. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u043e\u00a0\u0431\u044b\u043b\u043e \u043d\u0435\u00a0\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0432\u043e\u043e\u0431\u0449\u0435, \u0430\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430\u00a0\u043b\u0435\u0442\u0443 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 \u0441\u0430\u043c\u043e\u0433\u043e PostgreSQL. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438 \u0432\u00a0\u043d\u0435\u043c \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u0430\u043a\u00a0CREATE EVENT TRIGGER. \u0412\u00a0\u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0432\u044b\u0437\u043e\u0432 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e\u0433\u043e \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 \u0441\u0440\u0430\u0437\u0443\u00a0\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u00a0\u0441\u043e\u0431\u044b\u0442\u0438\u044e ddl_command_end.<\/p>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u00a0\u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u041f\u0443\u0441\u0442\u044c \u0443\u00a0\u043d\u0430\u0441 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 tmp_tmp \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437\u00a0\u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e id, \u0438 \u0435\u0449\u0435 \u043a\u0430\u043a\u0438\u0445\u2011\u0442\u043e \u043f\u043e\u043b\u0435\u0439. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043ac \u043f\u043e\u00a0id. \u041f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u043d\u0435\u00a0\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e\u00a0\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043c\u043e\u0436\u0435\u0442 \u0442\u0440\u0435\u043c\u044f \u043f\u0443\u0442\u044f\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>CREATE TEMP TABLE&#8230;<\/p>\n<\/li>\n<li>\n<p>CREATE TEMP TABLE AS&#8230;<\/p>\n<\/li>\n<li>\n<p>SELECT&#8230; INTO TEMP TABLE&#8230;<\/p>\n<\/li>\n<\/ul>\n<p>\u0423\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u044d\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0430\u043a\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f\u00a0\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0430:<\/p>\n<pre><code class=\"pgsql\">CREATE OR REPLACE FUNCTION build_index_on_tmp_tmp() RETURNS event_trigger LANGUAGE plpgsql AS $$ BEGIN   IF EXISTS (       SELECT 1       FROM pg_event_trigger_ddl_commands() E       WHERE E.object_identity='pg_temp.tmp_tmp'         AND tg_tag IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')) THEN     CREATE UNIQUE INDEX IF NOT EXISTS tmp_tmp_idx ON tmp_tmp(Id);   END IF; END; $$;<\/code><\/pre>\n<p>\u0410 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u0436\u0435 \u0438\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e\u0433\u043e \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430:<\/p>\n<pre><code class=\"pgsql\">CREATE EVENT TRIGGER build_index_on_tmp_tmp_tr ON ddl_command_end   EXECUTE FUNCTION build_index_on_tmp_tmp();<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b tmp_tmp \u043b\u044e\u0431\u044b\u043c \u0438\u0437 \u0442\u0440\u0435\u0445 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439, \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430:<\/p>\n<pre><code class=\"pgsql\">DROP TABLE IF EXISTS tmp_tmp; CREATE TEMP TABLE tmp_tmp (Id int, some_text text NULL); INSERT INTO tmp_tmp(Id) SELECT generate_series(1,1000000);  DROP TABLE IF EXISTS tmp_tmp; CREATE TEMP TABLE tmp_tmp AS SELECT generate_series(1,1000000) AS Id, NULL::text AS some_text;  DROP TABLE IF EXISTS tmp_tmp; SELECT generate_series(1,1000000) AS Id, NULL::text AS some_text INTO TEMP TABLE tmp_tmp;<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0431\u0435\u0437 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 \u0438 \u0441 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/p>\n<pre><code class=\"sql\">SELECT * FROM tmp_tmp WHERE Id=500000;<\/code><\/pre>\n<p>\u0421 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c<\/p>\n<pre><code>Index Scan using tmp_tmp_idx on tmp_tmp  (cost=0.42..2.64 rows=1 width=36) (actual time=0.030..0.031 rows=1 loops=1)   Index Cond: (id = 500000) Planning Time: 0.169 ms Execution Time: 0.043 ms<\/code><\/pre>\n<p>\u0411\u0435\u0437 \u043d\u0435\u0433\u043e<\/p>\n<pre><code>Seq Scan on tmp_tmp  (cost=0.00..11449.69 rows=2810 width=36) (actual time=39.425..78.025 rows=1 loops=1)   Filter: (id = 500000)   Rows Removed by Filter: 999999 Planning Time: 0.112 ms Execution Time: 78.045 ms<\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043b\u0430\u0439\u0444\u2011\u0445\u0430\u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u043a 1C, \u043d\u043e\u00a0\u0438 \u043a\u00a0\u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u0432\u00a0\u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u0442\u00a0\u0436\u0435\u043b\u0430\u043d\u0438\u044f \u0438\u043b\u0438\u00a0\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043b\u0435\u0437\u0442\u044c. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0434\u043e\u0432\u0435\u0441\u043a\u043e\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u00a0PostgreSQL \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0432\u044b\u043d\u0443\u0434\u0438\u0432 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u0443. \u0418\u043d\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0435\u0449\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438, \u043d\u043e\u00a0\u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u0443\u0436\u0435 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u00a0\u0440\u0430\u043c\u043a\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/719416\/\"> https:\/\/habr.com\/ru\/articles\/719416\/<\/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<h2>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0423\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441\u00a0\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 1\u0421 \u043d\u0430\u00a0PostgreSQL \u0432\u00a0\u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u00a0MS SQL \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u00a0\u0431\u044b\u0441\u0442\u0440\u043e. \u0422\u043e\u0433\u0434\u0430\u00a0\u0436\u0435 \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e\u00a0\u0432 99% \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0442\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u00a0\u043e\u043d\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u0430\u0436\u0435\u00a0\u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043d\u0430\u00a0MS SQL, \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0432\u0441\u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432\u043e\u00a0\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0422\u043e\u0433\u0434\u0430\u00a0\u0436\u0435\u00a0\u0431\u044b\u043b\u043e \u044f\u0441\u043d\u043e, \u0447\u0442\u043e\u00a0\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0442\u0438\u043f\u043e\u0432\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u00a0\u0445\u043e\u0447\u0435\u0442\u0441\u044f. \u041b\u0430\u0434\u043d\u043e \u0435\u0449\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446, \u043d\u043e\u00a0\u0434\u043b\u044f\u00a0\u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432\u043e\u00a0\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 1\u0421 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0434. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u043e\u00a0\u0431\u044b\u043b\u043e \u043d\u0435\u00a0\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0432\u043e\u043e\u0431\u0449\u0435, \u0430\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430\u00a0\u043b\u0435\u0442\u0443 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 \u0441\u0430\u043c\u043e\u0433\u043e PostgreSQL. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438 \u0432\u00a0\u043d\u0435\u043c \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u0430\u043a\u00a0CREATE EVENT TRIGGER. \u0412\u00a0\u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0432\u044b\u0437\u043e\u0432 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e\u0433\u043e \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 \u0441\u0440\u0430\u0437\u0443\u00a0\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u00a0\u0441\u043e\u0431\u044b\u0442\u0438\u044e ddl_command_end.<\/p>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u00a0\u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u041f\u0443\u0441\u0442\u044c \u0443\u00a0\u043d\u0430\u0441 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 tmp_tmp \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437\u00a0\u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e id, \u0438 \u0435\u0449\u0435 \u043a\u0430\u043a\u0438\u0445\u2011\u0442\u043e \u043f\u043e\u043b\u0435\u0439. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043ac \u043f\u043e\u00a0id. \u041f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u043d\u0435\u00a0\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e\u00a0\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043c\u043e\u0436\u0435\u0442 \u0442\u0440\u0435\u043c\u044f \u043f\u0443\u0442\u044f\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>CREATE TEMP TABLE&#8230;<\/p>\n<\/li>\n<li>\n<p>CREATE TEMP TABLE AS&#8230;<\/p>\n<\/li>\n<li>\n<p>SELECT&#8230; INTO TEMP TABLE&#8230;<\/p>\n<\/li>\n<\/ul>\n<p>\u0423\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u044d\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0430\u043a\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f\u00a0\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0430:<\/p>\n<pre><code class=\"pgsql\">CREATE OR REPLACE FUNCTION build_index_on_tmp_tmp() RETURNS event_trigger LANGUAGE plpgsql AS $$ BEGIN   IF EXISTS (       SELECT 1       FROM pg_event_trigger_ddl_commands() E       WHERE E.object_identity='pg_temp.tmp_tmp'         AND tg_tag IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')) THEN     CREATE UNIQUE INDEX IF NOT EXISTS tmp_tmp_idx ON tmp_tmp(Id);   END IF; END; $$;<\/code><\/pre>\n<p>\u0410 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u0436\u0435 \u0438\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e\u0433\u043e \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430:<\/p>\n<pre><code class=\"pgsql\">CREATE EVENT TRIGGER build_index_on_tmp_tmp_tr ON ddl_command_end   EXECUTE FUNCTION build_index_on_tmp_tmp();<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b tmp_tmp \u043b\u044e\u0431\u044b\u043c \u0438\u0437 \u0442\u0440\u0435\u0445 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439, \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430:<\/p>\n<pre><code class=\"pgsql\">DROP TABLE IF EXISTS tmp_tmp; CREATE TEMP TABLE tmp_tmp (Id int, some_text text NULL); INSERT INTO tmp_tmp(Id) SELECT generate_series(1,1000000);  DROP TABLE IF EXISTS tmp_tmp; CREATE TEMP TABLE tmp_tmp AS SELECT generate_series(1,1000000) AS Id, NULL::text AS some_text;  DROP TABLE IF EXISTS tmp_tmp; SELECT generate_series(1,1000000) AS Id, NULL::text AS some_text INTO TEMP TABLE tmp_tmp;<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0431\u0435\u0437 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 \u0438 \u0441 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/p>\n<pre><code class=\"sql\">SELECT * FROM tmp_tmp WHERE Id=500000;<\/code><\/pre>\n<p>\u0421 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c<\/p>\n<pre><code>Index Scan using tmp_tmp_idx on tmp_tmp  (cost=0.42..2.64 rows=1 width=36) (actual time=0.030..0.031 rows=1 loops=1)   Index Cond: (id = 500000) Planning Time: 0.169 ms Execution Time: 0.043 ms<\/code><\/pre>\n<p>\u0411\u0435\u0437 \u043d\u0435\u0433\u043e<\/p>\n<pre><code>Seq Scan on tmp_tmp  (cost=0.00..11449.69 rows=2810 width=36) (actual time=39.425..78.025 rows=1 loops=1)   Filter: (id = 500000)   Rows Removed by Filter: 999999 Planning Time: 0.112 ms Execution Time: 78.045 ms<\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043b\u0430\u0439\u0444\u2011\u0445\u0430\u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u043a 1C, \u043d\u043e\u00a0\u0438 \u043a\u00a0\u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u0432\u00a0\u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u0442\u00a0\u0436\u0435\u043b\u0430\u043d\u0438\u044f \u0438\u043b\u0438\u00a0\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043b\u0435\u0437\u0442\u044c. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0434\u043e\u0432\u0435\u0441\u043a\u043e\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u00a0PostgreSQL \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0432\u044b\u043d\u0443\u0434\u0438\u0432 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u00a0\u0438\u043d\u0434\u0435\u043a\u0441\u0443. \u0418\u043d\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0435\u0449\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438, \u043d\u043e\u00a0\u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u0443\u0436\u0435 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u00a0\u0440\u0430\u043c\u043a\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/719416\/\"> https:\/\/habr.com\/ru\/articles\/719416\/<\/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-405850","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/405850","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=405850"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/405850\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=405850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=405850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=405850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}