We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

MSSQl computed values and functions

Medium Priority
303 Views
Last Modified: 2012-05-06
In the master table i have the following column :

[cn_latest_position]  AS ([dbo].[last_position]([cn_id]))

This is the function that corresponds to ([dbo].[last_position]

USE [PRTR_recruit_live]
GO
/****** Object:  UserDefinedFunction [dbo].[edu_gpa_max]    Script Date: 02/17/2009 13:27:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[edu_gpa_max]
      (
      @cn_id decimal
      )
RETURNS decimal(4,2)
AS

      BEGIN
         
      RETURN (SELECT     MAX(edu_gpa) AS MaxGpa
FROM         tblEducation
WHERE     (edu_candidate = @cn_id))

end


When i query the database It takes around 90 seconds to return the top 1000 rows

If i exclude the column from the SELECT statement it returns 1000 rows in 1 sec.

This used to be a database on a webserver which i downloaded and attached to be used on the local intranet.

Any idea why it is so slow??
Comment
Watch Question

Topic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
Well part of the problem is that it is kind of a recursive / runtime function being a computed column. So for each row, it needs to resolve the function before it can retrieve the row and that really stuffs up any plans.

given the structure, what about trying an inline query ?

select  cn_id, (SELECT MAX(edu_gpa) FROM  tblEducation  WHERE edu_candidate = @cn_id)  AS cn_latest_position
from mytable

or

select cn_id, maxgpa
from mytable
left outer join (select edu_candidate, max(edu_gpa) as maxgpa from tbleducation group by edu_candidate) g on g.edu_candidate = mytable.cn_id

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.