views:

68

answers:

4

I know in SQL Server you can create indexes on a view, then the view saves the data from the underlying table. Then you can query the view. But, why I need to use view instead of table?

+1  A: 

You may want to use a view to simplify on queries. In our projects, the consensus is on using views for interfaces, and especially "report interfaces".

Imagine you've got a client table, and the manager wants a report every morning with the client's name, and their account balance (or whatever). If you code your report against the table, you're creating a strong link between your report and your table, making later changes difficult.

On the other hand if your report hits a view, you can twist the database freely; as long as the view is the same the report works, the manager is happy and you're free to experiment with the database. You want to separate client metadata from the main client table? go for it, and join the two tables in the view. You want to denormalize the cart info for the client? no problem, the view can adapt...

To be honest, it's my view as a programmer but other uses will certainly be found by db gurus :)

samy
Does your manager wants a report every morning with the outdated frozen at some moment of time clients balances? If he wants actualized data then it is again the same creating the same strong link between report and table but through additional intermediary (indexed view). What do you mean experimenting with database. Is database a toy? Experiment with views! I could not see any piece of any valid rationale under this logic
vgv8
Acutally often he does. Daily Sales mayy be defined to be al sales UP TO MIDNIGHT LAST DAY - so that everyone runs the same numbers, even if running the report an hour later. Data Warehouses (typical reporting) are noramlly loaded in intervals, not real time.
TomTom
@TomTom, yep, you got it. In addition, you may have a very active server where complex queries of the report interface may kill actual performance.
samy
+1  A: 

Basicaly of view:
1. When use same query (complex on many table) many times.
2. When new system need to read old table but don't want to change them.
Beside Indexed Views can improving Performance by create more specific index for a specific using but don't increase redundancy.

pinichi
A: 

A view is simply a SELECT statement that has been given a name and stored in a database. The main advantage of a view is that once it's created, it acts like a table for any other SELECT statements that you want to write.

The select statement for the view can reference tables, other views and functions.

You can create an index on the view (indexed view) to improve performance. An indexed view is self-updating, immediately reflecting changes to the underlying tables.

If your indexed view only selects columns from one table, you could just as well place the index on that table and query that table directly, the view would only cause overhead for your database. However, if your SELECT statement covers multiple tables with joins etc. than you could gain a performance boost by placing an index on the view.

Rewinder
The topic starter asked about indexed views not views. I'd recommend you delete your answer in its current content
vgv8
If I may qoute the topic starter: " But, why I need to use view instead of table? "
Rewinder
+1  A: 

A table is where the data is physically stored.

A view is where tables are summarized or grouped to make groups of tables easier to use.

An indexed view allows a query to use a view, and not need to get data from the underlying table, as the view already has the data, thus increasing performance.

You could not achieve the same result with just tables, without denormalizing your database, and thus potentially creating other issues.

Bravax
What is the sense in "You could not achieve the same results with just tables"? Extracting some pieces of data, isolating them from database. The next step is just to drop database to ultimately increase performance. Sorry, I could not see any piece of any valid rationale under this logic
vgv8