Is creating a view of multiple joining table faster then gettin that data directly using the join in a query?
Following is the query which is hit every table has over 100,000 records.
SELECT b.login as userEmail, imgateway_instance_id as img, u.id as userId
FROM bu开发者_开发百科ddy b
INNER JOIN `user` u ON b.username = u.login
INNER JOIN bot_to_buddy btb ON b.id = btb.buddy_id
INNER JOIN bot ON btb.bot_id = bot.id
WHERE u.id IN 14242
Using joins with tables that have as large an amount of records as yours are often very slow. This is so because joins will go over every record in a table which makes the query take a lot of time.
As a personally experienced solution I would suggest that you try and cut down the results of your query by using WHERE
as much as you can to filter down the results and then use joins.
No, you cannot gain performance from using a view. Behind the scene, your original query is run when you query the view.
Sometimes using views can gain a small bit of performance, like it says in High Performance MySQL
On the other hand the author of the book has written this blog: Views as performance trouble maker
Generally speaking, this depends on how you submit your query.
The view MAY be faster:
For example, in PHP it's common practice to submit the query "dynamically" (i.e. NOT as an prepared statement). That means MySQL has to compile the query every time you call it. When using a view, this in done once when the view is created.
Regarding MySQL as an DBMS, I heard about performance issues with Views in earlier versions. (Don't know what the current situation is, though).
As a general rule in such questions, just benchmark your query to get real life results. Looks like you have already populated your database with a lot of data, so this should yield meaningful results. (Don't forget to disable caching in MySQL).
There's little reason having a view run your query instead of running the query yourself be any faster with MySQL.
Views in MySQL is generally so poorly implemented, we had to back out of using them for many of our projects.
Check with EXPLAIN what your query does when you place it in a view, looking at that query, it can probably still use the proper indexes even it's part of a view, so it'll atleast not be slower.
精彩评论