SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Is it better to pull data from a view or table?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
422 Posts

Posted - 10/05/2012 :  10:30:21  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
Performance wise (and best practice wise) if I had Table1 (Col1, Col2, and Col3), Table2 (ColA, ColB, ColC), and View1 (Col2, Col3, ColB, ColC) and I only needed the data of Col2 and Col3 would it be better to have my program select right from Table1 or should I have it selected from the View?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

robvolk
Most Valuable Yak

USA
15663 Posts

Posted - 10/05/2012 :  10:38:24  Show Profile  Visit robvolk's Homepage  Reply with Quote
Without seeing your table and view definitions, or sample data and results, it's difficult to say. Do you have a JOIN condition that would impact which rows are returned, or a WHERE clause that accesses ColA, ColB or ColC?
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

USA
422 Posts

Posted - 10/05/2012 :  10:59:51  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
in this example the two tables would be joined on Col1 = ColA inside the join. I guess I am just looking for general guidelines on when it is better to put a basic 1 table select statment into a view for vs just running it against the table it's self.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/05/2012 :  11:07:12  Show Profile  Reply with Quote
I would prefer to do the select against the underlying table instead of the view unless there was a security related reason to select from the view. My reasoning is as follows:

a) Because the view is the result of a join, the results that you get when you select from the table may not be the same as the results you get when you select from the view. So logically the two queries may be different.

b) Even if the two queries were the same logically, when you select from the table, you are reading only from one table. When you are reading from the view, SQL Server will have to load and examine Table2 even though you are not retrieving any columns from it. So it is likely to be less efficient except in trivial cases, or in cases where the view is a materialized view.
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

USA
422 Posts

Posted - 10/05/2012 :  11:11:46  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
Those are good points I did not think of, thanks sunitabeck

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000