Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. stored procedure is fast, but slow from code

stored procedure is fast, but slow from code

Scheduled Pinned Locked Moved Database
databasesql-servercsharpsysadminquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Meysam Mahfouzi
    wrote on last edited by
    #1

    I have got a stored procedure which executes very fast in Sql Server Management Studio, but when I call it with the same parameters from C#, it executes slower and sometimes even throws timeout exception. Does anybody know the reason? hint: I have noticed that when I comment out one of the conditions in where clause, the query executes very fast both on SSMS and C#. Another point is that when I remove the comment and put the condition back in the query (which causes the stored procedure to compile again) the query execution becomes a little faster in C#, but after a while it becomes slow again (all with the same parameters)

    H D M 3 Replies Last reply
    0
    • M Meysam Mahfouzi

      I have got a stored procedure which executes very fast in Sql Server Management Studio, but when I call it with the same parameters from C#, it executes slower and sometimes even throws timeout exception. Does anybody know the reason? hint: I have noticed that when I comment out one of the conditions in where clause, the query executes very fast both on SSMS and C#. Another point is that when I remove the comment and put the condition back in the query (which causes the stored procedure to compile again) the query execution becomes a little faster in C#, but after a while it becomes slow again (all with the same parameters)

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I think that for anybody to stand a chance of answering your question, you should edit your post and include the stored procedure and the c# code that calls it. Please do NOT reply to me, this is just some advice to help you get an answer. :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      1 Reply Last reply
      0
      • M Meysam Mahfouzi

        I have got a stored procedure which executes very fast in Sql Server Management Studio, but when I call it with the same parameters from C#, it executes slower and sometimes even throws timeout exception. Does anybody know the reason? hint: I have noticed that when I comment out one of the conditions in where clause, the query executes very fast both on SSMS and C#. Another point is that when I remove the comment and put the condition back in the query (which causes the stored procedure to compile again) the query execution becomes a little faster in C#, but after a while it becomes slow again (all with the same parameters)

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        May I suggest ... In SQL Server Studio Manager, execute your stored procedure, but under the menu "Query" choose the option to "Display Esitmated Execution Plan". This will give you great insight into how the SQL server is going to retrieve your data. You want to avoid large "scans" of tables, try to take advantage of indexes. Also, you may want to look into SQL Profiler and watch what is going on while your code executes.

        M 1 Reply Last reply
        0
        • D David Mujica

          May I suggest ... In SQL Server Studio Manager, execute your stored procedure, but under the menu "Query" choose the option to "Display Esitmated Execution Plan". This will give you great insight into how the SQL server is going to retrieve your data. You want to avoid large "scans" of tables, try to take advantage of indexes. Also, you may want to look into SQL Profiler and watch what is going on while your code executes.

          M Offline
          M Offline
          Meysam Mahfouzi
          wrote on last edited by
          #4

          Thanks for your advice, All the indexes are being used (index seek) and no table scan is happening. That's why the query gets executed very fast in Sql Server. That's why I wonder what the difference is when I execute it from code with the same parameters.

          D 1 Reply Last reply
          0
          • M Meysam Mahfouzi

            Thanks for your advice, All the indexes are being used (index seek) and no table scan is happening. That's why the query gets executed very fast in Sql Server. That's why I wonder what the difference is when I execute it from code with the same parameters.

            D Offline
            D Offline
            David Mujica
            wrote on last edited by
            #5

            I would now look into the SQL Profiler, because you can now monitor how your application is interacting with the database. Things to check on the client side would be network traffic, memory usage and CPU usage while this is executing. Sounds like a fun challenge. :cool:

            1 Reply Last reply
            0
            • M Meysam Mahfouzi

              I have got a stored procedure which executes very fast in Sql Server Management Studio, but when I call it with the same parameters from C#, it executes slower and sometimes even throws timeout exception. Does anybody know the reason? hint: I have noticed that when I comment out one of the conditions in where clause, the query executes very fast both on SSMS and C#. Another point is that when I remove the comment and put the condition back in the query (which causes the stored procedure to compile again) the query execution becomes a little faster in C#, but after a while it becomes slow again (all with the same parameters)

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              I found something recently that discussed arith_abort (I think, the link is at work) which is set on by default, setting it to off apparently made a difference to some procs that showed this behavior. I have seen this behavior and after doing all the optimising possible the proc was still slow, that's when I started chasing down the more esoteric suggestions. If I find the link I will post it Monday....

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups