Postgresql 10 - последняя версия Ubuntu LTS - 1CPU, 2 ГБ оперативной памяти - другой SW не установлен
Две таблицы, обе с индексами:
следует (22 записи)
советы (2,5 млн записей)
select users_id_to from follows where users_id_from =1
занимает 0,041 мсек
select tips.id
from tips
where tips.users_id in (2,3,4,5,6,8,79407,38463,42798,94150,76554,56777,71407,51788,4624,41079,13549,75920,18979,6078,26178,18316)
Bitmap Heap Scan on tips (cost=101.72..2122.76 rows=556 width=8) (actual time=0.267..1.120 rows=597 loops=1)
Recheck Cond: (users_id = ANY ('{2,3,4,5,6,8,79407,38463,42798,94150,76554,56777,71407,51788,4624,41079,13549,75920,18979,6078,26178,18316}'::bigint[]))
Heap Blocks: exact=594
-> Bitmap Index Scan on tips_idx_users_id01 (cost=0.00..101.58 rows=556 width=0) (actual time=0.188..0.188 rows=597 loops=1)
Index Cond: (users_id = ANY ('{2,3,4,5,6,8,79407,38463,42798,94150,76554,56777,71407,51788,4624,41079,13549,75920,18979,6078,26178,18316}'::bigint[]))
Planning time: 0.210 ms
Execution time: 1.193 ms
занимает 1,2 мсек (было 4,7 мсек при первом запуске)
select tips.id
from tips
where tips.users_id in (select users_id_to
from follows
where users_id_from = 1
)
Merge Semi Join (cost=2.29..22.07 rows=573 width=8) (actual time=0.540..10632.242 rows=597 loops=1)
Merge Cond: (tips.users_id = follows.users_id_to)
Buffers: shared hit=1095506 read=1264002
-> Index Scan using tips_idx_users_id01 on tips (cost=0.43..205139.43 rows=2500000 width=16) (actual time=0.021..10180.667 rows=2353909 loops=1)
Buffers: shared hit=1095505 read=1264002
-> Sort (cost=1.77..1.82 rows=22 width=8) (actual time=0.051..0.084 rows=22 loops=1)
Sort Key: follows.users_id_to
Sort Method: quicksort Memory: 26kB
Buffers: shared hit=1
-> Seq Scan on follows (cost=0.00..1.27 rows=22 width=8) (actual time=0.012..0.019 rows=22 loops=1)
Filter: (users_id_from = 1)
Buffers: shared hit=1
Planning time: 0.954 ms
Execution time: 10632.376 ms
занимает 10433 мсек
Определения:
CREATE TABLE public.follows (
id bigserial NOT NULL,
users_id_from bigint NOT NULL DEFAULT 0,
users_id_to bigint NOT NULL DEFAULT 0,
has_accepted boolean NOT NULL DEFAULT true,
created_on timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT followings_pkey
PRIMARY KEY (id)
)
CREATE TABLE public.tips (
id bigserial NOT NULL,
users_id bigint NOT NULL,
temp_id bigint NOT NULL,
first_seen numeric(12,2) NOT NULL DEFAULT 0,
created_on timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
expire_on_gmt timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
ip_from inet NOT NULL DEFAULT '0.0.0.0'::inet,
"type" smallint NOT NULL DEFAULT 0,
growth numeric(8,1) NOT NULL DEFAULT 0.0,
seen boolean DEFAULT false,
CONSTRAINT tips_pkey
PRIMARY KEY (id)
)
CREATE INDEX tips_idx_users_id01
ON public.tips
(users_id);
Я действительно не понимаю, почему эта низкая производительность, кажется, что сервер делает JOIN под капотом ...
Любая помощь приветствуется.
Спасибо
Перес
РЕДАКТИРОВАТЬ - 2018.10.9
Несмотря на принятый ответ, который немедленно решает проблему, благодаря более глубокому расследованию Павла Стеуле (см. Сообщения ниже),реальная проблема заключалась в неверной статистике таблицы , следующей за .ВАКУУМНЫЙ АНАЛИЗ решит проблему, оба запроса теперь выполняются быстро.