• Lexa (10/4/2012)


    TheSQLGuru (10/4/2012)


    Please show the code for the sproc. Does the sproc reference any UDFs by any chance?

    Also, what tool/mechanism did you use to determine that this sproc was doing the blocking and what the actual locked pages/objects were?

    Kevin, I used Ignite's blocking analysis which showed the procedure at the top of the blocking chain. The procedure does not use any UDFs or cursors. I recall from your session at a SQL Saturday that those could contribute significantly to a poor performing procedure.

    Here is the statement that caused the blocking (according to Ignite)

    insert into #temp

    (fied1_id,

    fied2_id,

    fied3_id,

    fied4_id,

    create_date,

    fied5_id,

    fied6_id,

    select

    c.fied1_id,

    c.fied2_id,

    ci.fied3_id,

    ci.fied4_id,

    ci.create_date,

    count(ci.field10)as fied5_id,

    sum(case when field13 is not null and field11 = 0 then 1 else 0 end)as fied6_id

    from

    table1 c

    inner join table2 ci on

    c.fied2_id = ci.fied2_id

    inner join #table3 cta on

    c.fied2_id = cta.fied2_id

    inner join #table4 td on

    ci.fied2_id = td.fied2_id and

    ci.fied4_id = td.fied4_id

    where

    ci.create_date between @begin_date and @end_date and

    ci.fied4_id is not null

    group by

    c.fied1_id,

    c.fied2_id,

    ci.fied3_id,

    ci.fied4_id,

    ci.create_date

    While doing more digging, above statement is blocking an "ALTER INDEX ix on table2 REBUILD Partition xx" which in return blocks inserts into table2 but into partition y which is the active partition. Hence next question, is that a normal SQL Server behavior for ALTER INDEX ix on table2 partition x to block inserts that occur into table2 but partition y?

    Also, would be great to know whether setting LOCK_TIMEOUT on a statement within a procedure would be inherited by the session executing that store procedure. Since our sessions are re-used by the application, would setting LOCK_TIMEOUT would unintentionally get transferred to another proc?

    1) Again, what are the exact objects and type of locks that are blocking?? Maybe there is something with schema stability going on here? I note that rebuilds take pretty strict locks, and partitioning can introduce other issues/features with lock types and escalations.

    2) What else is in this sproc? It is possible that you are not getting proper information from ignite and something else is at fault.

    3) I wonder if your use of between @begin_date and @end_date is leading to parameter sniffing and a bad cached query plan. I have seen that a LOT in cases where the date range can be widely varying.

    4) Assuming you have a .NET app here (or other connectivity types as well I think), connection pooling is likely in play, and then you need to dig into what sp_reset_connection does to see if LOCK_TIMEOUT is one of the many things affected by that call.

    From BOL: At the beginning of a connection, this setting has a value of -1. After it is changed, the new setting stays in effect for the remainder of the connection. So again the question becomes what happens to that connection if connection pooling is in play. If it isn't, then when the connection is severed, a new connection will again start with the default.

    I note from this link (http://stackoverflow.com/questions/596365/what-does-sp-reset-connection-do) that SETs are returned to their default by sp_reset_connection, so it should go back to "infinite". Note I do not know the provenance of that post.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service