教你50招提升ASP.NET性能(二十四):ORM小窍门

时间:2023-02-03 13:05:31

ORM Tips
ORM小窍门

More and more people are using Object to Relational Mapping (ORM) tools to jump the divide between application code that is object oriented and a database that is storing information in a relational manner. These tools are excellent and radically improve development speed. But, there are a few ‘gotchas’ to know about.
越来越多的人正在使用对象关系映射(ORM)作为越过面向对象应用程序代码和关系型存储数据库之间鸿沟的工具。这些工具很优秀从根本上提高了开发速度。但是有一些“陷阱”需要知道。

(42)Avoid following the ‘Hello World’ examples provided with your ORM tool that turns it into an Object to Object Mapping. Database storage is not the same as objects for a reason. You should still have a relational storage design within a relational storage engine such as SQL Server.

招数42:

避免按照你的ORM工具提供的‘Hello World’ 示例以至于把他变成对象到对象的映射。数据库存储和对象存储不一样是有原因的。你应该还有一个在关系型存储引擎比如:SQL Server的设计。

(43)Parameterized queries are exactly the same as stored procedures in terms of performance and memory management. Since most ORM tools can use either stored procedures or parameterized queries, be sure you’re coding to these constructs and not hard-coding values into your T-SQL queries.

招数43:

参数化查询与存储过程在性能和内存管理方面是完全一样的。然而大多数ORM工具可以使用存储过程或参数化查询,确保你根据这些结构进行编码而不要硬编码值到你的T-SQL查询中。

(44)Create, Read, Update, and Delete (CRUD) queries can all be generated from the ORM tool without the need for intervention. But, the Read queries generated are frequently very inefficient. Consider writing a stored procedure for complex Read queries.

招数44:

创建,读取,更新和删除(CRUD)查询都可以通过ORM工具生成不需要干预。但是,生成的读取查询常常是非常低效的。考虑写一个存储过程用于复杂的读取查询。

(45)Since the code generated from the ORM can frequently be ad hoc, ensure that the SQL Server instance has ‘Optimize for Ad Hoc’ enabled. This will store a plan stub in memory the first time a query is passed, rather than storing a full plan. This can help with memory management.

招数45:

由于从ORM生成的代码可以经常被 ad hoc,确保SQL服务器实例已经开启了‘对Ad Hoc优化’。在第一次获得查询后,将在内存中存储一个计划存根,而不是存储一个完整的计划。这样有利于内存管理。

(46)Be sure your code is generating a parameter size equivalent to the data type defined within table in the database. Some ORM tools size the parameter to the size of the value passed. This can lead to serious performance problems.

招数46:

确保你正在生成的代码与数据库中表数据类型定义的参数大小相当。一些ORM工具按照传输值设置参数大小。这可能会导致严重的性能问题。