Update Query fail sometime in SQL server

Sanjay Kumar 21 Reputation points
2021-04-01T08:29:24.777+00:00

I am running a SQL query to update the column,

UPDATE sip

SET

ScheduleID = 123222

FROM Price sip

INNER JOIN

(

SELECT

sip.ItemPriceID

FROM Charge sipc

INNER JOI Price sip ON sip.ItemPriceID = sipc.ItemPriceID

INNER JOIN CtlCycle ctl ON ctl.BillingRunID = @BillingRunID

WHERE sipc.BillingRunID = @BillingRunID

AND CONVERT(DATE, BilledThruDate) >= CONVERT(DATE, DATEADD(DAY, -1, sipc.ItemPriceEndDate))

AND ISNULL(sip.OrderDate, '2000-01-01') <= ctl.OrdersThruDate

GROUP BY

sip.ItemPriceID

)AS t ON t.ItemPriceID = sip.ItemPriceID

It works fine in one environment and failed in another environment with the SQL timeout error.

I check the table charge having lots of null in columns ItemPriceEndDate, and if I add null check for column ItemPriceEndDate it works(AND sipc.ItemPriceEndDate is not null) fine.

The Question :

Why it works in one environment without any change and failed in another environment. Have the same amount of data in both environments.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,547 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 100.8K Reputation points MVP
    2021-04-01T22:09:15.833+00:00

    Unless there is a blocking issue, you apparently have different execution plans in the two environments. This is can be for a quite a few reasons:

    1. Different set of indexes.
    2. Different statistics information.
    3. Different server configuration for parallelism.
    4. Different amount of memory available.

    And that is not an exhaustive list.

    You will need to capture the query plans to work further.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. MelissaMa-MSFT 24,176 Reputation points
    2021-04-01T09:00:26.687+00:00

    Hi @Sanjay Kumar ,

    Welcome to Microsoft Q&A!

    Please check whether the failed one had any deadlock or open transactions which might cause timeout error and also check whether both environment had the same indexes, execution plans,settings and so on.

    You could also try with below statement:

    UPDATE sip  
    SET ScheduleID = 123222  
    FROM Price sip  
    INNER JOIN Charge  sipc ON sip.ItemPriceID = sipc.ItemPriceID  
    INNER JOIN CtlCycle ctl ON ctl.BillingRunID = sipc.BillingRunID  
    WHERE sipc.BillingRunID = @BillingRunID  
    AND CONVERT(DATE, BilledThruDate) >= CONVERT(DATE, DATEADD(DAY, -1, ISNULL(sipc.ItemPriceEndDate,'2000-01-01')))  
    AND ISNULL(sip.OrderDate, '2000-01-01') <= ctl.OrdersThruDate  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Olaf Helper 40,656 Reputation points
    2021-04-01T10:21:27.2+00:00

    failed in another environment with the SQL timeout error

    A command timeout isn't raised by SQL Server, it's raised by the client, when query execution elapse more then a defined time out; default is 30 seconds.
    Optimize the query or increase the timeout period for the application.


  3. MelissaMa-MSFT 24,176 Reputation points
    2021-04-02T02:12:01.967+00:00

    Hi @Sanjay Kumar ,

    Thanks for your update.

    Since you mentioned that the same query sometimes works in a failing environment, there may be some problems in other aspects than the query itself.

    As mentioned by Erland, you could check below while it is running:

    1. Find out any Blocking.
    2. Display Execution Plans and compare with each other.
    3. Find out any memory/CPU/disk related issue by using performance counters.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments