devxlogo

Slow view with lots of calculations

Slow view with lots of calculations

Question:
I created a view that does a lot of calculations and references several tables. If I do a

select * from linkview

everything comes up instantaneosly. However, if I do a

select * from linkview where id='12345'

it is mind numbingly slow. Which makes it seem that the problem is not the calculations, but querying the view. I am using SQL server 6.5.

Answer:
I suggest you do the following: Run the query using a straight select statement instead of using a view. So instead of “select from view where …” combine the SQL statement used to create the view with the clause specifying an id.

If the timing is slow, it means that you don’t have the index you need to support the additional limitation (that is, id = ‘12345’). However, if, in fact, the query runs fine through a select statement but degrades through a view, you are facing a different issue.

I’ve seen cases where the plan that the Optimizer decides on for a view is not updated when an additional where clause is entered–just as in your case. For that reason, I stopped using views in many cases. To be honest, it might have been fixed in later releases or service packs. I’m not even sure I didn’t pick up this prejudice from version 4.2.

The good news is that I verified that version 7 deals with this correctly.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist