I have a query that I would like to save as a view:
WITH subquery AS ( SELECT aaa_id, ... FROM table_aaa ... )
SELECT subquery.aaa_id, ... FROM table_bbb JOIN subquery USING ( ... )
;
[NOTE: In real-life, my query is much more complicated than this, with several WITH
sub-queries, many performing JOINS
, all being JOIN
ed together. But I'm looking for general guidance that I can use to work into my problem.]
The execution for this query include full table scans. This makes sense since there are no criteria included in a WHERE
clause. However, I can eliminate most of the full table scans by including such a clause:
WITH subquery AS ( SELECT aaa_id, ... FROM table_aaa ... WHERE aaa_id = :id)
SELECT subquery.aaa_id, ... FROM table_bbb JOIN subquery USING ( ... )
;
However, it doesn't seem that I have the option of putting the WHERE
condition in the right place when I create the view:
CREATE OR REPLACE VIEW vw_my_view AS
WITH subquery AS ( SELECT aaa_id, ... FROM table_aaa ... )
SELECT subquery.aaa_id, ... FROM table_bbb JOIN subquery USING ( ... )
;
SELECT ... FROM vw_my_view WHERE aaa_id = :id
;
In this case, the execute plan still contains the full table scans. Is there a way for me to hint that the WHERE
clause can actually be inserted into the WITH
sub-query?