Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-10-05 : 10:30:21
|
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
15732 Posts |
Posted - 2012-10-05 : 10:38:24
|
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? |
|
|
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-10-05 : 10:59:51
|
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 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-10-05 : 11:07:12
|
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. |
|
|
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-10-05 : 11:11:46
|
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 |
|
|
|
|
|
|
|