When to use database views and when not?

后端 未结 8 980
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 19:31

This question is about database views, not materialized-views.

Pros:

  • Query simplification.
  • Avoid repeat the same joins on mul
8条回答
  •  感动是毒
    2020-12-13 19:42

    I've had to use views a few times for doing strange joins and grouping by aliases.

    By strange joins, I mean selecting a list of distinct dates and then outer joining them back to the table they came from to get null entries for empty days. I couldn't figure out any other way of doing it.

    As for grouping by aliases, it seemed to depend on the complexity of the formula inside the alias. If the alias didn't reference any actual columns, or columns that were already being grouped, everything was ok, but aliases on columns that weren't included in the grouping were throwing errors.

    I seem to recall reading or hearing somewhere during my university days that selecting from a view was faster than selecting from a bunch of joined tables, but I don't know if that's true.

    One last advantage of using a view: pivot tables in Excel. I don't think there is a way of joining tables, or at least not in the wizard interface. It might be possible to do joins in Microsoft Query, but I haven't tried yet because the thought just occurred to me now.

提交回复
热议问题