I have a requirement to create a view and the business scenario is explained below
Consider i am having table Products(all product information) and Settings(settings for a country/state/City)
Now i have to create a view which gives product information by considering settings, It might be possible to have cities/states/country have there own settings.
Design of the view
It means first i need to check
1. any city is having there custom settings then output those records
UNION ALL
2. any state is having there custom settings then output those records by excluding cities under this state in step 1
UNION ALL
3. any country is having there custom settings or not then output those records by excluding cities ans states records in step1 and step2
This is the design which i thought of, is there anything wrong in the design?
Performance improving
With this existing design its taking 5 minutes for a query to run without any indexes on view and base tables.
Now what is the best option for me to improve the performance.
Creating indexed views or create index on base tables? which one helps me to make the query run in seconds 🙂
Sample Data
Product Table

Settings table

Expected Output

I can’t work out why your (P2 – Blue) result is showing. I re-wrote your samples as SQL, and created what I thought you wanted (whilst waiting for your expected output), and mine only produces one row (P1 – Red)
And now the actual view:
What I did was to combine all applicable settings, and then assumed that we applied the most restrictive settings (so take the MAX MinPrice specified and MIN MaxPrice).
Using those rules, the (P2 – Blue) row is ruled out, since the only applicable setting is setting 1 – which has a Min price of 150.
If I reverse it, so that we try to be as inclusive as possible (MIN MinPrice and MAX MaxPrice), then that returns (P1 – Red) and (P3 – Green) – but still not (P2 – Blue)