{"id":371703,"date":"2024-05-21T04:54:55","date_gmt":"2024-05-21T04:54:55","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=371703"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=371703","title":{"rendered":"<span>\u041a\u0430\u043a \u043f\u0438\u0448\u0443\u0442 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0433\u0443\u043c\u0430\u043d\u0438\u0442\u0430\u0440\u0438\u0438<\/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<blockquote>\n<p>&#8212; \u0418 \u0442\u043e\u0433\u0434\u0430 \u0441\u0430\u043c\u0435\u0446 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u043e\u0442\u0442\u0443\u0434\u0430 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442.<\/p>\n<p>&#8212; \u041f\u0440\u043e\u0441\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442?<\/p>\n<p>@ \u043a\/\u0444 \u00ab\u0414\u0435\u043d\u044c \u0440\u0430\u0434\u0438\u043e\u00bb<\/p>\n<\/blockquote>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0433\u043e\u0432\u043e\u0440\u044f\u0442 &#171;\u043c\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 N \u0440\u0430\u0437&#187; \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043f\u043b\u043e\u0445\u043e \u0430 \u043f\u043e\u0442\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u043a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a \u044f \u0434\u0443\u043c\u0430\u043b \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<hr\/>\n<p>\u041c\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u0430\u044f \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; \u043d\u0430\u0439\u0442\u0438 \u0432 \u0431\u0430\u0437\u0435 \u043f\u043e \u0418\u041d\u041d \u0438\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043d\u0443\u0436\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0438 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u043e \u043d\u0438\u043c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e<\/p>\n<ul>\n<li>\n<p>email<\/p>\n<\/li>\n<li>\n<p>\u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0430\u043a\u0430\u043d\u0441\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0445 \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432<\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0431\u0430\u0437\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7d8\/7f9\/fad\/7d87f9fad2a40f5c48237099933af1f0.png\" width=\"774\" height=\"520\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7d8\/7f9\/fad\/7d87f9fad2a40f5c48237099933af1f0.png\"\/><\/figure>\n<p>\u0411\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043d\u0438\u043a\u0443\u0434\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 \u0418\u041d\u041d \u0441\u043e\u0437\u0434\u0430\u043b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b:<\/p>\n<ul>\n<li>\n<p>\u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 \u0442\u0438\u043f\u0430 varchar &#8212; BTREE-\u0438\u043d\u0434\u0435\u043a\u0441\u044b,<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 jsonb[] -GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u043e\u0447\u043a\u0438 \u043e\u0442\u0441\u0447\u0435\u0442\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/p>\n<pre><code class=\"sql\">explain (FORMAT JSON, ANALYZE) select     inn, ogrn, name,     count(distinct email) as email_count,    count(distinct phone) as phone_count,    count(debtor_inn) as proverka_count,    count(company_inn) as vacancy_count from company  left join email on jsonb_build_object('inn', inn) &lt;@ any (email.company)  left join phone on jsonb_build_object('inn', inn) &lt;@ any (phone.company)  left join fssp on debtor_inn = inn left join vacancy on company_inn = inn where     inn = '7708503727' group by (inn, ogrn, name) <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043b\u0430\u043d\u0430<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f 5555ms<\/p>\n<\/li>\n<li>\n<p>GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0434\u043b\u044f jsonb[] \u043f\u043e\u043b\u0435\u0439 \u043f\u0440\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>85% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c (Nested Loop Left Join). \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0432 \u0446\u0438\u043a\u043b\u0435 &#8212; 239 \u0442\u044b\u0441.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 \u043f\u043e\u0438\u0441\u043a\u0435 GIN-\u0438\u043d\u0434\u0435\u043a\u0441.<br \/> \u041e\u0434\u043d\u0430 \u0438\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 GIN &#8212; <code>(jsonb @@ jsonpath)<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043b \u0442\u0430\u043a\u043e\u0439 \u043b\u0430\u0439\u0444\u0445\u0430\u043a &#8212; \u0441\u043e\u0437\u0434\u0430\u043b \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 jsonb \u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441 \u0432 \u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f jsonb-\u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u0431\u0435\u0440\u043d\u0443\u0432 \u0435\u0433\u043e \u0432 jsonb.<\/p>\n<pre><code class=\"json\">{ 'companies': jsonb[] } <\/code><\/pre>\n<p>\u0438 \u0441\u043e\u0437\u0434\u0430\u043b GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043d\u0430 \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/p>\n<pre><code class=\"sql\">CREATE INDEX IF NOT EXISTS company2_idx     ON tablename USING gin     (company2 jsonb_path_ops)     WHERE company2 IS NOT NULL; <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043b \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0418\u041d\u041d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <code>(jsonb @@ jsonpath)<\/code><\/p>\n<pre><code class=\"sql\">... left join view.phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath left join view.email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath ... <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 5 \u0440\u0430\u0437 &#8212; 960ms<\/p>\n<\/li>\n<li>\n<p>\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 2 \u0440\u0430\u0437\u0430 (- \u0434\u043e 123 \u0442\u044b\u0441) \u043d\u043e \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u043e\u043b\u0435\u0435 80% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u044f\u0437\u044b\u043a\u0430 SQL \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0442\u0440\u0435\u043c\u044f \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 (\u0438\u043c\u0435\u043d\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0443 \u043c\u0435\u043d\u044f). \u0421\u0430\u043c\u044b\u043c \u0443\u0434\u0430\u0447\u043d\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043b \u0442\u0430\u043a\u043e\u0439<\/p>\n<pre><code class=\"sql\">explain (FORMAT JSON, ANALYZE) select  b.*, count(company_inn) as vacancy_count from  (select  a.*, count(debtor_inn) as fssp_count from (select     inn, ogrn, name,     count(distinct email) as email_count,    count(distinct phone) as phone_count from company left join phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath left join email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath where inn = '7708503727' group by (inn, ogrn, name)) as a left join fssp on debtor_inn = inn group by (inn, full_name, email_count, phone_count)) as b left join vacancy on company_inn = inn group by (inn, full_name, email_count, phone_count, fssp_count) <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 &#8212; 176ms<\/p>\n<\/li>\n<li>\n<p>95% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0434 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u043e\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0438 \u043d\u0435 \u043f\u043b\u043e\u0445\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 &#8212; \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c 5 \u0440\u0430\u0437. \u0421\u043c\u0443\u0449\u0430\u043b\u0430 \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430. \u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u043b\u0438\u0441\u044c \u043d\u0430 \u0434\u0438\u0441\u043a, \u043e\u0442\u0441\u044e\u0434\u0430 \u0438 \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041f\u043e\u0431\u0435\u0434\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u0443\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c.  \u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>work_mem<\/code> \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>enable_sort<\/code> \u0434\u0430\u043b\u043e 3-\u0445 \u043a\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u044f \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043b \u044d\u0442\u043e &#171;\u043d\u0435\u0447\u0438\u0441\u0442\u044b\u043c&#187; \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043b\u0435\u0437 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0438 &#171;\u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b&#187; \u0442\u0430\u043a\u0438 4-\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"sql\">explain (FORMAT JSON, ANALYZE) select  b.*, vacancy_count, fssp_count from (select   inn, ogrn, name,   count(distinct email) as email_count,  count(distinct phone) as phone_count from company left join phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '616106158873' || '\"))')::jsonpath left join email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '616106158873' || '\"))')::jsonpath where inn = '616106158873' group by (inn, ogrn, name) ) as b left join ( select debtor_inn, count(debtor_inn) as fssp_count from fssp where debtor_inn = '616106158873' group by debtor_inn ) as c on debtor_inn = inn left join ( select company_inn, count(company_inn) as vacancy_count from vacancy where company_inn = '616106158873' group by company_inn ) as a on company_inn = inn <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 &#8212; 0.13ms \u0423\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u0435\u0435 100 \u0440\u0430\u0437 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0443!<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u0435\u0440\u043d\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 sql-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"sql\">select * from company_stat('7708503727') <\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 &#8212; 1.7ms<\/p>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e. \u041d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043f\u043e\u0438\u0441\u043a \u0418\u041d\u041d \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0435\u0435 \u043d\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b FTS (\u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0441\u0430\u043c\u044b\u0439 \u043c\u043e\u0439 \u043b\u044e\u0431\u0438\u043c\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0432 postgresql):<\/p>\n<ul>\n<li>\n<p>\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 company \u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u0442\u0438\u043f\u0430 ts_vector<\/p>\n<\/li>\n<li>\n<p>\u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043b\u044f \u043f\u043e\u043b\u044f GIN-\u0438\u043d\u0434\u0435\u043a\u0441 \u041e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u0431\u0435\u0440\u043d\u0443\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"sql\">create or replace function company_stat_by_name(X text) returns table(inn_ character varying(20), orgn character varying(25), company_name text, email_num bigint, phone_num bigint, fssp_num bigint, vacancy_num bigint) as $$   DECLARE   r record; BEGIN  FOR r IN  (select inn from company where fts_company_name @@ websearch_to_tsquery('config', X))  LOOP RETURN QUERY select  b.*, vacancy_count, fssp_count from (select  inn, ogrn, full_name,  count(distinct email) as email_count, count(distinct phone) as phone_count from company left join phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || r.inn || '\"))')::jsonpath left join email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || r.inn || '\"))')::jsonpath where inn = r.inn group by (inn, ogrn, full_name) ) as b left join ( select debtor_inn, count(debtor_inn) as fssp_count from fssp where debtor_inn = r.inn group by debtor_inn ) as c on debtor_inn = r.inn left join ( select company_inn, count(company_inn) as vacancy_count from vacancy where company_inn = r.inn group by company_inn ) as a on company_inn = r.inn; END LOOP; END; $$ language plpgsql  <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0439 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e, \u043e\u0446\u0435\u043d\u0438\u043b \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0440\u0430\u0439\u043d\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432:<\/p>\n<ul>\n<li>\n<p>3-4 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f 2.4ms<\/p>\n<\/li>\n<li>\n<p>> 200 \u0441\u0442\u0440\u043e\u043a \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f 34ms<br \/> \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u0435\u043d.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c &#8212; \u0441\u0440\u0430\u0437\u0443 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u0433\u0435\u043d\u0438\u0435\u0432. \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0435\u0441\u0442\u044c \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f.<\/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\/802645\/\"> https:\/\/habr.com\/ru\/articles\/802645\/<\/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<blockquote>\n<p>&#8212; \u0418 \u0442\u043e\u0433\u0434\u0430 \u0441\u0430\u043c\u0435\u0446 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u043e\u0442\u0442\u0443\u0434\u0430 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442.<\/p>\n<p>&#8212; \u041f\u0440\u043e\u0441\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442?<\/p>\n<p>@ \u043a\/\u0444 \u00ab\u0414\u0435\u043d\u044c \u0440\u0430\u0434\u0438\u043e\u00bb<\/p>\n<\/blockquote>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0433\u043e\u0432\u043e\u0440\u044f\u0442 &#171;\u043c\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 N \u0440\u0430\u0437&#187; \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043f\u043b\u043e\u0445\u043e \u0430 \u043f\u043e\u0442\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u043a\u0430\u043a \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a \u044f \u0434\u0443\u043c\u0430\u043b \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<hr\/>\n<p>\u041c\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u0430\u044f \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; \u043d\u0430\u0439\u0442\u0438 \u0432 \u0431\u0430\u0437\u0435 \u043f\u043e \u0418\u041d\u041d \u0438\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043d\u0443\u0436\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0438 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u043e \u043d\u0438\u043c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e<\/p>\n<ul>\n<li>\n<p>email<\/p>\n<\/li>\n<li>\n<p>\u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0430\u043a\u0430\u043d\u0441\u0438\u0439<\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0445 \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432<\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0431\u0430\u0437\u0435<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0411\u0435\u0437 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043d\u0438\u043a\u0443\u0434\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 \u0418\u041d\u041d \u0441\u043e\u0437\u0434\u0430\u043b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b:<\/p>\n<ul>\n<li>\n<p>\u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 \u0442\u0438\u043f\u0430 varchar &#8212; BTREE-\u0438\u043d\u0434\u0435\u043a\u0441\u044b,<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 jsonb[] -GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u043e\u0447\u043a\u0438 \u043e\u0442\u0441\u0447\u0435\u0442\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/p>\n<pre><code class=\"sql\">explain (FORMAT JSON, ANALYZE) select     inn, ogrn, name,     count(distinct email) as email_count,    count(distinct phone) as phone_count,    count(debtor_inn) as proverka_count,    count(company_inn) as vacancy_count from company  left join email on jsonb_build_object('inn', inn) &lt;@ any (email.company)  left join phone on jsonb_build_object('inn', inn) &lt;@ any (phone.company)  left join fssp on debtor_inn = inn left join vacancy on company_inn = inn where     inn = '7708503727' group by (inn, ogrn, name) <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043b\u0430\u043d\u0430<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f 5555ms<\/p>\n<\/li>\n<li>\n<p>GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0434\u043b\u044f jsonb[] \u043f\u043e\u043b\u0435\u0439 \u043f\u0440\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>85% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c (Nested Loop Left Join). \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0432 \u0446\u0438\u043a\u043b\u0435 &#8212; 239 \u0442\u044b\u0441.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 \u043f\u043e\u0438\u0441\u043a\u0435 GIN-\u0438\u043d\u0434\u0435\u043a\u0441.<br \/> \u041e\u0434\u043d\u0430 \u0438\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 GIN &#8212; <code>(jsonb @@ jsonpath)<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043b \u0442\u0430\u043a\u043e\u0439 \u043b\u0430\u0439\u0444\u0445\u0430\u043a &#8212; \u0441\u043e\u0437\u0434\u0430\u043b \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 jsonb \u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441 \u0432 \u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f jsonb-\u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u0431\u0435\u0440\u043d\u0443\u0432 \u0435\u0433\u043e \u0432 jsonb.<\/p>\n<pre><code class=\"json\">{ 'companies': jsonb[] } <\/code><\/pre>\n<p>\u0438 \u0441\u043e\u0437\u0434\u0430\u043b GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043d\u0430 \u043d\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/p>\n<pre><code class=\"sql\">CREATE INDEX IF NOT EXISTS company2_idx     ON tablename USING gin     (company2 jsonb_path_ops)     WHERE company2 IS NOT NULL; <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043b \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0418\u041d\u041d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <code>(jsonb @@ jsonpath)<\/code><\/p>\n<pre><code class=\"sql\">... left join view.phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath left join view.email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath ... <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 5 \u0440\u0430\u0437 &#8212; 960ms<\/p>\n<\/li>\n<li>\n<p>\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c GIN-\u0438\u043d\u0434\u0435\u043a\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 2 \u0440\u0430\u0437\u0430 (- \u0434\u043e 123 \u0442\u044b\u0441) \u043d\u043e \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u043e\u043b\u0435\u0435 80% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u044f\u0437\u044b\u043a\u0430 SQL \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0442\u0440\u0435\u043c\u044f \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 (\u0438\u043c\u0435\u043d\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0443 \u043c\u0435\u043d\u044f). \u0421\u0430\u043c\u044b\u043c \u0443\u0434\u0430\u0447\u043d\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043b \u0442\u0430\u043a\u043e\u0439<\/p>\n<pre><code class=\"sql\">explain (FORMAT JSON, ANALYZE) select  b.*, count(company_inn) as vacancy_count from  (select  a.*, count(debtor_inn) as fssp_count from (select     inn, ogrn, name,     count(distinct email) as email_count,    count(distinct phone) as phone_count from company left join phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath left join email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '7708503727' || '\"))')::jsonpath where inn = '7708503727' group by (inn, ogrn, name)) as a left join fssp on debtor_inn = inn group by (inn, full_name, email_count, phone_count)) as b left join vacancy on company_inn = inn group by (inn, full_name, email_count, phone_count, fssp_count) <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 &#8212; 176ms<\/p>\n<\/li>\n<li>\n<p>95% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0434 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u043e\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0438 \u043d\u0435 \u043f\u043b\u043e\u0445\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 &#8212; \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c 5 \u0440\u0430\u0437. \u0421\u043c\u0443\u0449\u0430\u043b\u0430 \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430. \u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u043b\u0438\u0441\u044c \u043d\u0430 \u0434\u0438\u0441\u043a, \u043e\u0442\u0441\u044e\u0434\u0430 \u0438 \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041f\u043e\u0431\u0435\u0434\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u0443\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c.  \u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>work_mem<\/code> \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>enable_sort<\/code> \u0434\u0430\u043b\u043e 3-\u0445 \u043a\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u044f \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043b \u044d\u0442\u043e &#171;\u043d\u0435\u0447\u0438\u0441\u0442\u044b\u043c&#187; \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043b\u0435\u0437 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0438 &#171;\u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b&#187; \u0442\u0430\u043a\u0438 4-\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"sql\">explain (FORMAT JSON, ANALYZE) select  b.*, vacancy_count, fssp_count from (select   inn, ogrn, name,   count(distinct email) as email_count,  count(distinct phone) as phone_count from company left join phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '616106158873' || '\"))')::jsonpath left join email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || '616106158873' || '\"))')::jsonpath where inn = '616106158873' group by (inn, ogrn, name) ) as b left join ( select debtor_inn, count(debtor_inn) as fssp_count from fssp where debtor_inn = '616106158873' group by debtor_inn ) as c on debtor_inn = inn left join ( select company_inn, count(company_inn) as vacancy_count from vacancy where company_inn = '616106158873' group by company_inn ) as a on company_inn = inn <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 &#8212; 0.13ms \u0423\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u0435\u0435 100 \u0440\u0430\u0437 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0443!<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u0435\u0440\u043d\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 sql-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"sql\">select * from company_stat('7708503727') <\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 &#8212; 1.7ms<\/p>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e. \u041d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043f\u043e\u0438\u0441\u043a \u0418\u041d\u041d \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0435\u0435 \u043d\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b FTS (\u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u0441\u0430\u043c\u044b\u0439 \u043c\u043e\u0439 \u043b\u044e\u0431\u0438\u043c\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0432 postgresql):<\/p>\n<ul>\n<li>\n<p>\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 company \u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u0442\u0438\u043f\u0430 ts_vector<\/p>\n<\/li>\n<li>\n<p>\u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043b\u044f \u043f\u043e\u043b\u044f GIN-\u0438\u043d\u0434\u0435\u043a\u0441 \u041e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u0431\u0435\u0440\u043d\u0443\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"sql\">create or replace function company_stat_by_name(X text) returns table(inn_ character varying(20), orgn character varying(25), company_name text, email_num bigint, phone_num bigint, fssp_num bigint, vacancy_num bigint) as $$   DECLARE   r record; BEGIN  FOR r IN  (select inn from company where fts_company_name @@ websearch_to_tsquery('config', X))  LOOP RETURN QUERY select  b.*, vacancy_count, fssp_count from (select  inn, ogrn, full_name,  count(distinct email) as email_count, count(distinct phone) as phone_count from company left join phone on phone.company2 @@ ('exists($.companies[*].inn ? (@==\"' || r.inn || '\"))')::jsonpath left join email on email.company2 @@ ('exists($.companies[*].inn ? (@==\"' || r.inn || '\"))')::jsonpath where inn = r.inn group by (inn, ogrn, full_name) ) as b left join ( select debtor_inn, count(debtor_inn) as fssp_count from fssp where debtor_inn = r.inn group by debtor_inn ) as c on debtor_inn = r.inn left join ( select company_inn, count(company_inn) as vacancy_count from vacancy where company_inn = r.inn group by company_inn ) as a on company_inn = r.inn; END LOOP; END; $$ language plpgsql  <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0439 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e, \u043e\u0446\u0435\u043d\u0438\u043b \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0440\u0430\u0439\u043d\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432:<\/p>\n<ul>\n<li>\n<p>3-4 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f 2.4ms<\/p>\n<\/li>\n<li>\n<p>> 200 \u0441\u0442\u0440\u043e\u043a \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 &#8212; \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f 34ms<br \/> \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u0435\u043d.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c &#8212; \u0441\u0440\u0430\u0437\u0443 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u0433\u0435\u043d\u0438\u0435\u0432. \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0435\u0441\u0442\u044c \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f.<\/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\/802645\/\"> https:\/\/habr.com\/ru\/articles\/802645\/<\/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-371703","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/371703","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=371703"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/371703\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=371703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=371703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=371703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}