Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3940854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:28:12+00:00 2026-05-20T00:28:12+00:00

I have a stored procedure that runs to update gaming points for user balances.

  • 0

I have a stored procedure that runs to update gaming points for user balances. It’s an insert with 5 subqueries. I have isolated one of the subqueries as the query that slows the entire batch down. Without it, the stored procedure will run in under 2 seconds. With it, it will take as much as 8 seconds. 8 Seconds isn’t the end of the world, but for the sake of scalability, I will need to have it complete faster. Here is the isolated subquery:

(SELECT IsNull(Sum(A.TransAmount) + Sum(Case When A.BetResult = 1 Then (A.BetWinAmount + (A.TransAmount * -1)) End), 0)
            FROM User_T A
            LEFT OUTER JOIN User_TD B on A.TID = B.TID
            LEFT OUTER JOIN Lines_BL C ON B.LID = C.LID
            LEFT OUTER JOIN Lines_BM D ON C.BMID = D.BMID
            LEFT OUTER JOIN Event_M E ON D.EID = E.EID
            LEFT OUTER JOIN Event_KB F ON A.TransReason = F.BID
            LEFT OUTER JOIN Event_M G ON F.BID = G.EID
        where A.UserID = U.UserID AND (A.IsSettled = 1)
        AND 
        (
        (A.TransReason = 1 AND (datediff(dd, Convert(datetime, E.EDate, 101), Convert(datetime, @EndDate, 101)) = @DaysAgo)) OR 
        (A.TransReason >= 3000 AND (datediff(dd, Convert(datetime, G.EDate, 101), Convert(datetime, @EndDate, 101)) = @DaysAgo)
                AND  [dbo].[Event_CEAFKBID](A.TransReason) = 1) OR
        (A.TransReason BETWEEN 3 and 150 AND (datediff(dd, Convert(datetime, A.TransDT, 101), Convert(datetime, @EndDate, 101)) = @DaysAgo))
        )

What I have done to further isolate: When I run a Select * on just the joins (without the where clauses), the performance in very good – > 100000 rows in under a second. As I add in the where clauses, I believe the great slow down is from the ‘or’ clause and/or the function that needs to be evaluated.

As I understand it, a function inside the where clause evaluates each row – as opposed to somehow caching the definition of the function and evaluating that way. I do have indexes on the tables, but I am wondering if some of them are not correct.

Without you knowing the full database structure, I am sure it’s very difficult to pin down where the problem is, but I would like to get pointed in a direction to begin to further isolate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:28:13+00:00Added an answer on May 20, 2026 at 12:28 am

    I suspect your biggest performance hits are from the correlated subquery (whatever table is behind U.UserId) and from the embedded function call dbo.Event_CEAFKBID. Much of course depends upon how big the tables are (how many rows are being read). All those datetime conversions won’t help and generate a very strong “bad design” smell, but I don’t think they’d impact performance too much.

    Those left outer joins are ugly, as the optimizer has to check them all for row – so if “A” is big, all the joins on all the rows have to be performed, even if there’s no data there. If they can be replaced with inner joins, do so, but I’m guessing not because of that “table E or table G” logic. Lesses, it sure looks like what you’ve got is three separate queries moshed into one; if you broke it out into three, unioned together, it’d look something like the Frankenstein query below. I’ve no idea if this would run faster or not (heck, I can’t even debug the query and make sure the panetheses balance), but if you’ve got sparse data relative to your logic this should run pretty fast. (I took out the date conversions to make the code more legible, you’d have to plug them back in.)

    SELECT isnull(sum(Total), 0) FinalTotal from (
    SELECT 
       sum(A.TransAmount + Case When A.BetResult = 1 Then A.BetWinAmount - A.TransAmount else 0 End) Total
     FROM User_T A            
     INNER JOIN User_TD B on A.TID = B.TID             
    INNER JOIN Lines_BL C ON B.LID = C.LID             
    INNER JOIN Lines_BM D ON C.BMID = D.BMID             
    INNER JOIN Event_M E ON D.EID = E.EID             
     where A.UserID = U.UserID
      AND A.IsSettled = 1
      AND A.TransReason = 1 
      AND (datediff(dd, E.EDate, @EndDate) = @DaysAgo)) 
    
    UNION ALL SELECT 
       sum(A.TransAmount + Case When A.BetResult = 1 Then A.BetWinAmount - A.TransAmount else 0 End) Total
     FROM User_T A            
    INNER JOIN Event_KB F ON A.TransReason = F.BID             
    INNER JOIN Event_M G ON F.BID = G.EID        
     where A.UserID = U.UserID
      AND A.IsSettled = 1
      AND A.TransReason >= 3000 
      AND (datediff(dd, G.EDate, @EndDate) = @DaysAgo)                 
      AND [dbo].[Event_CEAFKBID](A.TransReason) = 1
    
    UNION ALL SELECT 
       sum(A.TransAmount + Case When A.BetResult = 1 Then A.BetWinAmount - A.TransAmount else 0 End) Total
     FROM User_T A            
     where A.UserID = U.UserID
      AND A.IsSettled = 1
      AND A.TransReason BETWEEN 3 and 150 
      AND datediff(dd, A.TransDT, @EndDate) = @DaysAgo)
    ) ThreeWayUnion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been running up a stored procedure that runs several separate update and
I have stored procedure which runs perfectly. And when I insert it into query
I have a stored procedure that updates my database. It runs great in query
I have a stored procedure that runs custom backups for around 60 SQL servers
I have an app that runs a stored procedure in SQL Server, checking the
I have a stored procedure that takes a user ID and calculates their balance
I have a large auditing stored procedure that prints values and runs some SELECT
I have a SqlCommand which runs a stored procedure that contains two integer output
My limited SQL knowledge prompted this post! I have a stored procedure that runs
I have a loooooong stored procedure that runs about 15 select statements across different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.