简体   繁体   English

创建多个联接表的视图是否比使用查询中的联接直接获取数据更快?

[英]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. 以下是命中的查询,每个表都有超过100,000条记录。

   SELECT b.login as userEmail, imgateway_instance_id as img, u.id as userId 
   FROM  buddy 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. 作为一个个人经验丰富的解决方案,我建议您尝试使用WHERE尽量减少查询的结果,以筛选出结果,然后使用联接。

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 有时使用视图可能会获得一点点性能,就像在高性能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" (ie NOT as an prepared statement ). 例如,在PHP中,通常的做法是“动态”提交查询(即,不作为准备好的语句 )。 That means MySQL has to compile the query every time you call it. 这意味着MySQL每次调用时都必须编译查询。 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. 关于MySQL作为DBMS,我听说过早期版本中Views的性能问题 (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). (不要忘记在MySQL中禁用缓存)。

There's little reason having a view run your query instead of running the query yourself be any faster with MySQL. 没有理由让视图运行查询而不是自己运行查询,因为使用MySQL可以更快。

Views in MySQL is generally so poorly implemented, we had to back out of using them for many of our projects. 通常,MySQL中的视图实现得很差,我们不得不放弃在许多项目中使用它们。

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. 使用EXPLAIN检查将查询放在视图中时的查询行为,查看该查询,即使它是视图的一部分,它仍可能仍使用正确的索引,因此它至少不会变慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM