sql - Choosing the view query at runtime (postgres database) -
i want create view choose between 2 possible selects based on session variable (set_config) on runtime.
today "union all" between 2 selects in following manner:
create view my_view ( select * x cast(current_setting('first_select') int)=1 , ...; union select * y cast(current_setting('first_select') int)=0 , ...; )
the problem postgres optimizer takes bad decisions when target union. when run example:
select * my_view id in (select id z field='value')
it decides full scan on table x although has index on "id".
there way define such view without using "union" clause?
just or them where-clause. optimiser find invariant conditions.
create view my_view ( select * x ( cast(current_setting('first_select') int)=1 , <condition1> ) or ( cast(current_setting('first_select') int)=0 , <condition2> ) ... ) ;
Comments
Post a Comment