简体   繁体   English

存储过程与数据库查询中的代码

[英]Stored Procedures vs Code in Database Query

What are the performance differences between accessing a database to query using ASP.NET Code behind against using SQL Stored Procedure访问数据库以使用 ASP.NET 代码进行查询与使用 SQL 存储过程之间的性能差异是什么?

For ease of use, coding the query is easier, especially when coming to maintenance and changes.为了便于使用,对查询进行编码更容易,尤其是在进行维护和更改时。

However, a stored procedure is compiled in a database and run by the database.但是,存储过程是在数据库中编译并由数据库运行的。

Which one is more efficient, better to use?哪个更有效,更好用?

SQL Server caches the execution plan of any query, SPROC or not. SQL Server 缓存任何查询的执行计划,无论是否为 SPROC。 There is virtually no difference here.这里几乎没有区别。 Basically, you save sending the query text over the network when using an sproc, nothing more.基本上,您可以在使用 sproc 时通过网络发送查询文本,仅此而已。 Source: http://msdn.microsoft.com/en-us/library/ms181055.aspx来源: http : //msdn.microsoft.com/en-us/library/ms181055.aspx

Without and special reason being present, do what ever is more convenient to you.在没有特殊原因的情况下,做对你来说更方便的事情。

The other answers suggesting generally better performance for sprocs are not true.其他表明 sproc 性能通常更好的答案是不正确的。

As long as it is a database centric query then the stored procedure will in most times be the faster choice (Performance).只要它是以数据库为中心的查询,那么存储过程在大多数情况下将是更快的选择(性能)。

But it is harder to maintain because its not in your regular source bundle.但它更难维护,因为它不在您的常规源包中。

"Better to use" depends on the requirements. “更好用”取决于要求。 If its okay when the query is a tad slower (like 1 ms VS 3 ms) then keep your code together and have it in ASP.如果查询速度稍慢(例如 1 ms VS 3 ms)还可以,那么将您的代码放在一起并将其放在 ASP 中。 If performance is the thing you want put it in the Database.如果性能是您想要的东西,请将其放入数据库中。

I put most of my queries in the code and only the ones that NEED the performance in the database.我将大部分查询放在代码中,并且只将那些需要数据库中的性能。

Also it depends on the Database System used, of course.当然,这也取决于所使用的数据库系统。

Your question is very incomplete as to what you are actually comparing.关于您实际比较的内容,您的问题非常不完整。

Whether the SQL code is in a stored procedure or a full-blown inline SQL statement submitted from the client usually makes little difference (assuming proper parameterization and the SQL being non-pathological) to performance. SQL 代码是在存储过程中还是在客户端提交的完整的内联 SQL 语句中,通常对性能几乎没有影响(假设参数化正确且 SQL 是非病态的)。 It can make a large difference in the security architecture and access required to be given to base tables or views instead of execution rights on procedures.它可以在安全体系结构和访问基表或视图而不是过程的执行权限方面产生很大的不同。 Stored procs encourage parameterization as a requirement, but parameterization is also possible with inline SQL.存储过程鼓励将参数化作为一项要求,但使用内联 SQL 也可以进行参数化。

If you are talking about performing logic against sets returned from the database versus doing the work in the database, this can go both ways - it depends upon the type of operation, the type of indexing, the bandwidth between client and database and number of requests needed to be serviced.如果您正在谈论对从数据库返回的集合执行逻辑而不是在数据库中执行工作,这可以是双向的 - 这取决于操作类型、索引类型、客户端和数据库之间的带宽以及请求数量需要维修。

Usually, I'd look first at doing it in the database to keep the join/looping logic abstracted from the client and reduce data on the wire (both columns and rows) and present a simple data set API to the client, but IT DEPENDS.通常,我会首先在数据库中执行此操作,以保持从客户端抽象出来的连接/循环逻辑并减少线路上的数据(列和行)并向客户端提供一个简单的数据集 API,但它取决于.

This is an "it depends" question.这是一个“视情况而定”的问题。
Presuming this is SQL Server 2008R2 or higher Standard or Enterprise edition, stored procedures will cache differently than a TSQL statement.假设这是 SQL Server 2008R2 或更高的标准版或企业版,存储过程的缓存将与 TSQL 语句不同。 Complex T-SQL statements will almost always perform worse than a stored procedure due to multiple things such as parameterization, code compilation, parameter sniffing, and various other optimizations.由于参数化、代码编译、参数嗅探和各种其他优化等多种因素,复杂的 T-SQL 语句的性能几乎总是比存储过程差。 In general, I prefer stored procedures as they are MUCH easier to optimize.一般来说,我更喜欢存储过程,因为它们更容易优化。 Plus you can change a stored proceudre without re-compiling and re-deploying any code.此外,您无需重新编译和重新部署任何代码即可更改存储的程序。 And optimizations (such as "optimize for unknown" or "with recompile" can be applied to a stored procedure when parameter values vary drastically) can be applied to a stored procedure and un-done without end users even noticing (well, except for a performance change).并且优化(例如“优化未知”或“重新编译”可以应用于参数值剧烈变化时的存储过程)可以应用于存储过程并在最终用户甚至没有注意到的情况下取消(好吧,除了性能变化)。

A stored procedure will always end up in the plan cache after a single run and will never be considered an ad-hoc query.一次运行后,存储过程将始终在计划缓存中结束,并且永远不会被视为即席查询。 Ad-hoc queries, depending on SQL settings, may or may not be stored in the plan cache.临时查询,取决于 SQL 设置,可能会或可能不会存储在计划缓存中。 Plus adding or removing a character (presuming it is not parameterized) will cause SQL Server to build a new plan and building new plans is a slow operation.加上添加或删除字符(假设它没有参数化)将导致 SQL Server 构建新计划,而构建新计划是一个缓慢的操作。

TL;DR - preusming SQL Server 2008R2 or higher Standard/Enterprise; TL;DR - 使用 SQL Server 2008R2 或更高标准/企业版; for simple queries, you will notice no difference.对于简单的查询,您会发现没有区别。 For complex queries, stored procedure (if written properly) will almost always out perform T-SQL.对于复杂的查询,存储过程(如果编写得当)几乎总是会执行 T-SQL。 Stored procedures are easier to optimize at a later date as well.存储过程也更容易在以后优化。

Edit - added in SQL version.编辑 - 在 SQL 版本中添加。 I am uncertain about older SQL versions.我不确定旧的 SQL 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在项目中搜索代码以在数据库中存储过程 - Search Code in Project for Stored Procedures in the Database MVC 5,EF 6和“数据库中的代码优先”存储过程 - MVC 5, EF 6, and “Code First from Database” Stored Procedures VS 2010中的多个存储过程 - Multiple stored procedures in VS 2010 连接到旧版存储过程和数据库 - Connecting to Legacy Stored Procedures and Database 具有现有数据库和具有多个源的类与手动存储过程的实体框架 - Entity Framework with existing database and classes with multiple sources vs manual stored procedures DBContext与ObjectContext-使用存储过程 - DBContext vs ObjectContext - using Stored Procedures 如何在断开连接的体系结构中不使用存储过程的情况下在c#代码中添加数据库字段 - how to add a database field in the c# code without using stored procedures in a disconnected architecture 是否可以使用代码优先实体框架在数据库上生成基本的CRUD存储过程? - Is it possible to generate basic CRUD stored procedures on database with code-first entity framework? 存储过程中的输出参数(Oracle数据库) - Out parameters in stored procedures (oracle database) 带有存储过程的.Net MVC数据库第一个模型 - .Net MVC Database first model with stored procedures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM