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. Slow Performance of using EXECUTE sp_executesql

Slow Performance of using EXECUTE sp_executesql

Scheduled Pinned Locked Moved Database
databasesharepointperformancehelp
4 Posts 3 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.
  • H Offline
    H Offline
    HatakeKaKaShi
    wrote on last edited by
    #1

    HI Guys need ur advise here I am facing this issue that when i use the sp_executesql to execute my statement, the query time is way longer then normal. Previously i am using this methed in stored procedure simpely coding the statement SELECT * FROM Bla Bla but when i change to using DECLARE @SQL AS NVARCHAR(MAX) SET @SQL = 'SELECT * FROM bla bla Table' EXECUTE sp_executesql @SQL the query time slow down greatly i change to using sp_executesql because i need to create the STATEMENT base on some condition checking will be gald if u guys have any advise Thanks a million KaKaShi HaTaKe

    D M 2 Replies Last reply
    0
    • H HatakeKaKaShi

      HI Guys need ur advise here I am facing this issue that when i use the sp_executesql to execute my statement, the query time is way longer then normal. Previously i am using this methed in stored procedure simpely coding the statement SELECT * FROM Bla Bla but when i change to using DECLARE @SQL AS NVARCHAR(MAX) SET @SQL = 'SELECT * FROM bla bla Table' EXECUTE sp_executesql @SQL the query time slow down greatly i change to using sp_executesql because i need to create the STATEMENT base on some condition checking will be gald if u guys have any advise Thanks a million KaKaShi HaTaKe

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      This might be because SQL is not choosing the best execution plan. Check the execution plan which is followed to execute that dynamic query. Although, I am not aware that there is much you can do about this (except for getting rid of dynamic queries if possible), you can check out parameter sniffing if it can help. Here[^] is an article that tells about it.

      1 Reply Last reply
      0
      • H HatakeKaKaShi

        HI Guys need ur advise here I am facing this issue that when i use the sp_executesql to execute my statement, the query time is way longer then normal. Previously i am using this methed in stored procedure simpely coding the statement SELECT * FROM Bla Bla but when i change to using DECLARE @SQL AS NVARCHAR(MAX) SET @SQL = 'SELECT * FROM bla bla Table' EXECUTE sp_executesql @SQL the query time slow down greatly i change to using sp_executesql because i need to create the STATEMENT base on some condition checking will be gald if u guys have any advise Thanks a million KaKaShi HaTaKe

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

        Everyone keeps telling me that there is little or no difference between a stored proc and dynamic SQL, then I come across a post like this one and I think they are full of bullshit. Change your var from nvarchar(max) to varchar(8000), it may help and cannot hurt. REALLY make sure that you must use dynamic sql, there is often a way to get around using it if you construct your query correctly. Using case, conditional where and even if clauses to avoid dynamic sql. As danish suggested, take a look at the execution plan of the dynamic and a normal version of the proc and see where you are paying the penalty.

        Never underestimate the power of human stupidity RAH

        D 1 Reply Last reply
        0
        • M Mycroft Holmes

          Everyone keeps telling me that there is little or no difference between a stored proc and dynamic SQL, then I come across a post like this one and I think they are full of bullshit. Change your var from nvarchar(max) to varchar(8000), it may help and cannot hurt. REALLY make sure that you must use dynamic sql, there is often a way to get around using it if you construct your query correctly. Using case, conditional where and even if clauses to avoid dynamic sql. As danish suggested, take a look at the execution plan of the dynamic and a normal version of the proc and see where you are paying the penalty.

          Never underestimate the power of human stupidity RAH

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Mycroft Holmes wrote:

          Everyone keeps telling me that there is little or no difference between a stored proc and dynamic SQL, then I come across a post like this one and I think they are full of bullsh*t.

          They obviously are. Just ask a simple question: What takes more time - cooking and eating or just eating? They should understand.

          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