Why execute stored procedures is faster than SQL query from a script?

前端 未结 8 2141
别那么骄傲
别那么骄傲 2020-11-30 03:31

In fact, if I call the stored procedures from my application, I need a connection to my DB.

So, why calling a \"stored procedures\" should be faster than \"passing a

8条回答
  •  时光说笑
    2020-11-30 03:51

    This article explains it pretty well: https://codingsight.com/dynamic-sql-vs-stored-procedure/

    From my experience, Stored Procedures are definitely faster, because of decreased network traffic (don't have to send the whole query) and caching of the procedure and query plans.

    I ran code similar to the following on a table filled with user logon data.

    "select top 1 * from Logons where ComputerName=@ComputerName order by LogonTime desc"

    It took 2 hours to run the query on 7000 computer names.

    When I placed the query into a stored procedure, it took about a minute to run on 7000 computer names.

    I'm certain that taking 1 second vs a 10 milliseconds per query doesn't make a big difference to humans if you are running the query just once. However, if you need to run the query one thousand times, it's a difference of 1000 seconds (approx. 16 min) vs 10 seconds.

提交回复
热议问题