5

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

storm_buster
  • 7,362
  • 18
  • 53
  • 75
  • 1
    Cautionary note...we had an ancient view that had become a defacto standard throughout the site, It was being used in many, many places as though it was a table. The view sorted a number of fields, then it's use elsewhere also sorted that result.......nice and slow – GDP Jun 13 '12 at 15:00

7 Answers7

7

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.

ewernli
  • 38,045
  • 5
  • 92
  • 123
1

I think your question is a little bit confusing in what you are trying to achieve (Gain knowledge regarding SQL Views or how to structure your application).

I believe all database logic should be stored at the database tier, ideally in a stored procedure, function rather in the application logic. The application logic should then connect to the database and retrieve the data from these procedures/functions and then expose this data in your application.

One of the the benefits of storing this at the database tier is taking advantage of the execution plans via SQL Server (which you may not get by accessing it directly via the application logic). Another advantage is that you can separate the application, i.e. if the database needs to be changed you don't have to modify the application directly.

For a direct point on Views, the advantages of using them include:

  • Restrict a user to specific rows in a table. For example, allow an employee to see only the rows recording his or her work in a labor- tracking table.

  • Restrict a user to specific columns. For example, allow employees who do not work in payroll to see the name, office, work phone, and department columns in an employee table, but do not allow them to see any columns with salary information or personal information.

  • Join columns from multiple tables so that they look like a single table.

http://msdn.microsoft.com/en-us/library/aa214068(v=sql.80).aspx

Darren
  • 68,902
  • 24
  • 138
  • 144
0

Personally I prefer views, especially for reports/apps as if there are any problems in the data you only have to update a single view rather than re-building the app or manually editing the queries.

bendataclear
  • 3,802
  • 3
  • 32
  • 51
0

You can run into performance issues and constraints on the types queries you can run against a view.

Restrictions on what you can do with views.

In my experience a well indexed table, using the right engine, and properly tuned (for example setting an appropriate key_buffer value) can perform better than a view.

Alternatively you could create a trigger that updates a table based on the results of other tables. http://dev.mysql.com/doc/refman/5.6/en/triggers.html

Girish Rao
  • 2,609
  • 1
  • 20
  • 24
0

SQL views have many uses. Try first reading about them and then asking a more specific question:

Milimetric
  • 13,411
  • 4
  • 44
  • 56
0

I have seen that views are used a lot to do two things:

  1. Simplify queries, if you have a HUGE select with multiple joins and joins and joins, you can create a view that will have the same performance but the query will be only a couple of lines.
  2. For security reason, if you have a table with some information that shouldn't be accessed for all the developers, you can create views and grant privileges to see the views and not the main table, I.E: table 1: Name, Last_name, User_ID, credit_card, social_security. You create a view table.table view: name, last_name, user_id .
jcho360
  • 3,724
  • 1
  • 15
  • 24
0

The technic you are saying is called denormalization. Cal Henderson, software engineer from Flickr, openly supports this technic.

In theory JOIN operation is one of the most expensive operations, so it is a good practice to denormalize, since you are transforming n queries with m JOIN in 1 query with m JOIN and n queries that select from a view.

That said, the best way is to test it for yourself. Because what could be incredibly good for Flickr may not be so good for your application.

Moreover, the performance of views may vary a lot from one RBDMS to another. For instance, depending on the RBDMS views can be updated when the orginal table is changed, can have indexes, etc.

eversor
  • 3,031
  • 2
  • 26
  • 46