Queries taking longer than usual time for a subset of inputs when reaching the end

  • Hi all,
    I would explain my problem in detail.
    We have a c# code that calls a sql query viz. a T.Sql query (embedded sql file in the solution)
    This code accepts an input and is called for multiple inputs one after other.
    This sql code creates temporary tables and temporary variables, with joining some physical tables and returns the output.
    Basically we are not inserting / updating any physical table in this sql code, this sql code outputs several resultset back to c#

    The problem is initially it runs fast for some inputs but in the end it starts taking longer time and eventually gets transaction timeout.(The call to this sql code is being made within transaction scope of .net)

    I have seen people have used OPTION(Recompile) in almost 5 places while creating temp tables, for which I think is not required since we are creating temporary table and the input to this query is also a temporary table and not a parameter.

    Your inputs are required and helpful

  • Can you post the code?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Hi Gail,
    Posting the code might not be possible.
    However, when I am running the same code for any one input, the code is taking usual time and nothing looks like causing a timeout.
    What could be the reason for slowness of return, when the Tsql code is executed 'N' number of times for different input
    Let me know if you need to verify something in the query or if you are looking at possibility of something.

  • er.mayankshukla - Friday, July 28, 2017 2:33 AM

    Hi Gail,
    Posting the code might not be possible.
    However, when I am running the same code for any one input, the code is taking usual time and nothing looks like causing a timeout.
    What could be the reason for slowness of return, when the Tsql code is executed 'N' number of times for different input
    Let me know if you need to verify something in the query or if you are looking at possibility of something.

    If you run something N times then it is expected that the duration will be N times as long as a single run. 

    If you run code with different inputs (tabular or parameter) you can get VASTLY different performance due to either a) large row count differences, b) data value skew or c) widely varying input parameters. OPTION (RECOMPILE) can help with those, but cannot solve all problems.

    If you cannot post the code, query plans, table schemas (with indexing), etc. then you will need to hire a performance tuning professional that WILL be allowed to see those things.

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

  • Yesterday, we ran a batch of 3000.
    And enabled the profiler to capture the sql query, time taken, cpu, reads etc.

    All 2999 inputs ran as expected, however the last 1 input took high time.
    I ran the below query to see the waiting query:
    SELECT req.session_id   ,blocking_session_id   ,ses.host_name   ,DB_NAME(req.database_id) AS DB_NAME   ,ses.login_name   ,req.status   ,req.command   ,req.start_time   ,req.cpu_time   ,req.total_elapsed_time / 1000.0 AS total_elapsed_time   ,req.command   ,req.wait_type   ,sqltext.textFROM  sys.dm_exec_requests reqCROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltextJOIN  sys.dm_exec_sessions ses   ON ses.session_id = req.session_idWHERE req.wait_type IS NOT NULL

    And I could see below result

    session_idblocking_session_idhost_nameDB_NAMElogin_namestatuscommandstart_timecpu_timetotal_elapsed_timecommand2wait_type
    790WINPrusersuspendedSELECT13:49.79401738.823SELECTASYNC_NETWORK_IO

    Does this Network IO wait could be a problem and if yes, what might be the reason for this ?

  • er.mayankshukla - Thursday, August 3, 2017 9:31 PM

    Does this Network IO wait could be a problem and if yes, what might be the reason for this ?

    NetworkIO is a well-documented wait.
    As for a problem, no idea, how long was the session waiting for it?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • er.mayankshukla - Friday, July 28, 2017 2:33 AM

    What could be the reason for slowness of return, when the Tsql code is executed 'N' number of times for different input

    Higher load on the server due to you running it N times.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • er.mayankshukla - Thursday, August 3, 2017 9:31 PM

    Yesterday, we ran a batch of 3000.
    And enabled the profiler to capture the sql query, time taken, cpu, reads etc.

    All 2999 inputs ran as expected, however the last 1 input took high time.
    I ran the below query to see the waiting query:
    SELECT req.session_id   ,blocking_session_id   ,ses.host_name   ,DB_NAME(req.database_id) AS DB_NAME   ,ses.login_name   ,req.status   ,req.command   ,req.start_time   ,req.cpu_time   ,req.total_elapsed_time / 1000.0 AS total_elapsed_time   ,req.command   ,req.wait_type   ,sqltext.textFROM  sys.dm_exec_requests reqCROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltextJOIN  sys.dm_exec_sessions ses   ON ses.session_id = req.session_idWHERE req.wait_type IS NOT NULL

    And I could see below result

    session_idblocking_session_idhost_nameDB_NAMElogin_namestatuscommandstart_timecpu_timetotal_elapsed_timecommand2wait_type
    790WINPrusersuspendedSELECT13:49.79401738.823SELECTASYNC_NETWORK_IO

    Does this Network IO wait could be a problem and if yes, what might be the reason for this ?

    ASYNC_NETWORK_IO wait type occurs when the client application is unable to pick or catch up with the readying of the processed data at SQL server. It can be normal . In ur case I advise to look at the amount and type / range of data (skewed) which might be much larger compared to that generated for the other input parameters. Also check the elapsed time for this wait_type for the other inputs to have see difference.

    Arshad

  • Again bringing out the same issue.
    There are 2 ec2 instances with Sql server installed, one is prod and other is staging, they both have same machine configurations and same sql configurations.
    Running a set of calculation, in Prod causes a last calculation to take much higher time and then finally timeout, however it runs well in time in the staging environment.

    When I say set of calculation, A set of calculation will contain around 4k records for which multiple embedded sql queries will be run against the sql server.
    There will be select, Insert, Delete and update statements.

    In order to make sure I have the same dataset, I tested it after restoring the db from prod to staging, still the same result.i.e. fast in staging but failing in prod.
    For reference below is the output of sp_configure.

    nameminimummaximumconfig_valuerun_value
    access check cache bucket count06553600
    access check cache quota0214748364700
    Ad Hoc Distributed Queries0100
    affinity I/O mask-2147483648214748364700
    affinity mask-2147483648214748364700
    affinity64 I/O mask-2147483648214748364700
    affinity64 mask-2147483648214748364700
    Agent XPs0111
    allow updates0100
    backup compression default0100
    blocked process threshold (s)08640000
    c2 audit mode0100
    clr enabled0100
    common criteria compliance enabled0100
    contained database authentication0100
    cost threshold for parallelism03276755
    cross db ownership chaining0100
    cursor threshold-12147483647-1-1
    Database Mail XPs0100
    default full-text language0214748364710331033
    default language0999900
    default trace enabled0111
    disallow results from triggers0100
    EKM provider enabled0100
    filestream access level0200
    fill factor (%)010000
    ft crawl bandwidth (max)032767100100
    ft crawl bandwidth (min)03276700
    ft notify bandwidth (max)032767100100
    ft notify bandwidth (min)03276700
    index create memory (KB)704214748364700
    in-doubt xact resolution0200
    lightweight pooling0100
    locks5000214748364700
    max degree of parallelism03276700
    max full-text crawl range025644
    max server memory (MB)1282147483647100000100000
    max text repl size (B)-121474836476553665536
    max worker threads1286553500
    media retention036500
    min memory per query (KB)512214748364710241024
    min server memory (MB)02147483647100000100000
    nested triggers0111
    network packet size (B)5123276740964096
    Ole Automation Procedures0100
    open objects0214748364700
    optimize for ad hoc workloads0100
    PH timeout (s)136006060
    precompute rank0100
    priority boost0100
    query governor cost limit0214748364700
    query wait (s)-12147483647-1-1
    recovery interval (min)03276700
    remote access0111
    remote admin connections0100
    remote login timeout (s)021474836471010
    remote proc trans0100
    remote query timeout (s)02147483647600600
    Replication XPs0100
    scan for startup procs0100
    server trigger recursion0111
    set working set size0100
    show advanced options0111
    SMO and DMO XPs0111
    transform noise words0100
    two digit year cutoff1753999920492049
    user connections03276700
    user options0327676464
    xp_cmdshell0100

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply