Sql-server – Multiple-View Query Optimisation

optimizationsql serversql-server-2000view

We have a monster view that contains joins to many (many) tables in order to return the data that is required. In some cases, to get a column of information, we've have to join onto 4 tables, just to get to the value we need (the database structure isn't great, and we can't change it).

In order to improve the performance, I am extracting certain components and constructing them into their own View, in order to simplify the execution plan, and hopefully optimise the queries.

If I write a query that joins these views together, do I lose the benefit of the query optimisation for each view? i.e. do I end up back where I started by combining the views into a single select, or will it inherently perform better because they are acting atomically from one another?

Best Answer

Views are just stored queries: you don't execute views, but you SELECT data out of them. When you combine multiple views joining them, you are doing nothing different from inlining the view definition in the query. In fact, that's what the optimizer does: it's called view expansion.

If your views will never be used in isolation, but always joined together instead, I suggest that you stop tuning them individually, but you concentrate on the overall query that will use them. The optimizer will expand the views and generate a plan that will have nothing to do with the plan used when selecting from the individual views.

On the other hand, what makes your individual views faster (well-thought T-SQL code, indexes, statistics) won't probably hurt the performance of the overall query. Just don't expect the resulting plan to be a simple merge of the individual views' plans.