Search code examples
mysqlsqlsql-server

Best practises : is sql views really worth it?


I am building a new web applications with data stored in the database. as many web applications, I need to expose data from complexe sql queries (query with conditions from multiple table). I was wondering if it could be a good idea to build my queries in the database as sql view instead of building it in the application? I mean what would be the benefit of that ? database performance? do i will code longer? debug longer?

Thank you


Solution

  • This can not be answered really objectively, since it depends on case by case.

    With views, functions, triggers and stored procedures you can move part of the logic of your application into the database layer.

    This can have several benefits:

    • performance -- you might avoid roundtrips of data, and certain treatment are handled more efficiently using the whole range of DBMS features.
    • consisness -- some treatment of data are expressed more easily with the DBMS features.

    But also certain drawback:

    • portability -- the more you rely on specific features of the DBMS, the less portable the application becomes.
    • maintenability -- the logic is scattered across two different technologies which implies more skills are needed for maintenance, and local reasoning is harder.

    If you stick to the SQL92 standard it's a good trade-off.

    My 2 cents.