Is a MySQL view faster than a normal query?

后端 未结 2 1635
栀梦
栀梦 2021-02-11 18:04

I have a complex query using many joins (8 in fact). I was thinking of simplifying it into a view. After a little research I can see the benefits in simplicity and security. But

相关标签:
2条回答
  • 2021-02-11 18:38

    It can sometimes help, but it's no silver bullet. I've seen a view help performance, but I've also seen it hurt it. A view can force materialization, which can sometimes get you a better access path if MySQL isn't choosing a good one for you.

    0 讨论(0)
  • 2021-02-11 18:38

    No, a view is simply a stored text query. You can apply WHERE and ORDER against it, the execution plan will be calculated with those clauses taken into consideration.

    0 讨论(0)
  • A view's basically a stored subquery. There's essentially no difference between:

    SELECT *
    FROM someview
    

    and

    SELECT *
    FROM (
        SELECT somestuff 
        FROM underlying table
    );
    

    except the view's a bit more portable, as you don't have to write out the underlying query each time you want to work with whatever data it returns.

    0 讨论(0)
提交回复
热议问题