We help IT Professionals succeed at work.
Get Started

Optimize a MS SQL Scalar FUnction

Last Modified: 2019-02-25
In the function below...

I am building a #temp table that will have from 20k - 5k records in it
I am then doing a select on that table

Select * from #temp
WHERE 1 = 1
      AND dbo.brkOwnsTimeshareContract(ISNULL(b.ExternalID2, '0')) = 1;

Open in new window

It seems slow... and I am wondering if I have a better solution or tweak on either my select or the underlying function itself

The dbo.everyware_t_contract table generally has @ 60k records in it

ALTER FUNCTION [dbo].[brkOwnsTimeshareContract]
        @OwnerID INT = NULL

        DECLARE @OwnerBucket TINYINT = 0;

        IF EXISTS (   SELECT ContractID
                      FROM   dbo.everyware_t_contract c
                      WHERE  c.OwnerID = @OwnerID
                             AND (   c.ContractStatus = 'Active'
                                     OR CHARINDEX('Phase', c.ContractStatus) > 0
                                     OR CHARINDEX('Pender', c.ContractStatus) > 0 
									 OR CHARINDEX('Suspense', c.ContractStatus) > 0 
									 OR CHARINDEX('Not Back', c.ContractStatus) > 0 
									 OR CHARINDEX('Presale', c.ContractStatus) > 0 )
                             AND NOT (   CHARINDEX('club', c.ContractType) > 0
                                         OR CHARINDEX('guest', c.ContractType) > 0
                                         OR CHARINDEX('goc co', c.ContractType) > 0
                                         OR CHARINDEX('goc breck', c.ContractType) > 0
                                         OR CHARINDEX('GOC CO $1995', c.ContractType) > 0
                                         OR CHARINDEX('GOC Breck $1295', c.ContractType) > 0
           AND ISNULL(@OwnerID, 0) > 0
            SET @OwnerBucket = 1;

        RETURN @OwnerBucket;

Open in new window

Watch Question
Senior Developer
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE