Bobyblanco
Bobyblanco

Reputation: 121

Join a view with another view performance

I am working on a web application (PHP/MySQL) and I am looking for the best performance.

I am building a view, in this view I have to join multiple data but I was wondering, what is the best choice between joining a view with another view or with tables.

To be more clear, if i have:

View_1 = Table_A join Table_B

And i need in View_2 data from Table_A and Table_B, should I do

View_2 = Table_C join View_1 OR View_2 = Table_C join Table_A join Table_B

Thank you very much

Upvotes: 1

Views: 1943

Answers (2)

user1919238
user1919238

Reputation:

Look at the explain plan for your specific query.

Joining to a view rather than the underlying tables might have an impact on performance--because, as Atip Roy explains, the order in which you perform joins can be important for performance, and having some of the joins within a view could prevent you from optimally ordering the joins in the query.

But this depends entirely on your query--it might not make any difference.

And, there is a benefit to using a view, as well: it means the logic of that view is stored in only one place. You have given us a simplified example, but in real life, the code for view 1 is likely more complex. If you need the same logic in multiple places, it's a good thing to avoid code duplication where possible.

So, I would check whether performance actually matters (and how much it matters). I would prefer to use the view unless the performance of that option was not acceptable.

Upvotes: 3

ROY
ROY

Reputation: 73

It would always be a good idea to go ahead with the individual tables and if you are using any inner join which may reduce the row count, do it first. Reduced count of rows will always help you in terms of performance. In view to table joining you may not get the mentioned flexibility every time.

Upvotes: 2

Related Questions