... FROM sales
INNER JOIN (SELECT product,
price,
date_start,
LEAD(date_start) OVER(PARTITION BY product ORDER BY date_start) next_date
FROM prices) prices_extra
ON sales.product = prices_extra.product ...
WHERE prices_extra.date_start <= sales.date AND sales.date < COALESCE(prices_extra.next_date, to_date('20990101', 'YYYYMMDD'))
CREATE INDEX location_index ON youtube ("CITY", "AREA", "REGION");
SET enable_seqscan TO false;
SELECT "CITY", "AREA", "REGION"
FROM youtube
GROUP BY "CITY", "AREA", "REGION";
select
tw.worker_id
,tw2.worker_id as partner
,t.dt_from
,t.dt_to
from as_team_worker tw
inner join team t on t.team_id = tw.team_id
inner join as_team_worker tw2 on tw2.team_id = tw.team_id
and tw2.worker_id != tw.worker_id
where tw.worker_id = 5
Необходимые условия:
- структура бд разработана не мазохистом
- вы не пытаетесь выбрать всю простыню товаров (тогда зачем вам скорость)