翻译:改进.NET应用程序的性能和可伸缩性(一)-概述及ADO.NET 性能

 翻译:改进.NET应用程序的性能和可伸缩性

简介:这是一篇2004年的帖子,虽然旧,但内容大多不过时,翻译过来,大家看看有哪些不对的地方,最后整理出一个CheckList用于CodeReview来改进我们的代码质量。

 

可能翻译有误或者理解不对的地方,我用红色标注出来,大家可在评论中进行指正和讨论,我最后会更新到列表中去,谢谢。

内容

概述
设计类清单
应用程序系列清单
数据库服务器清单


概述

这是一个改进.net应用程序性能和可伸缩性的清单,它可以帮助你把这些信息应用到你的实际行动中去(译注:这个句子太长,翻译不了),

下面的列表包括:

清单:ADO.NET 性能
清单:Review架构和设计的性能和可伸缩性
清单:ASP.NET 性能
清单:企业服务性能
清单:互操作性能
清单:托管代码性能
清单:Remoting性能
清单:SQL Server性能
清单:Web服务性能
清单:XML 性能

设计类清单


清单:为性能和可伸缩性做架构和设计的Review,可以帮助你检查应用程序在架构和可伸缩性的各个方面。这个列表包括部署和架构,数据结构和算法,通信,资源管理,缓存,状态管理,并发,耦合和内聚,数据访问,异常处理,以及类设计上的考虑。

应用程序系列清单

应用程序系列清单中的每一个列表都包含性能和可伸缩性的检查,具体到每个技术细节的性能和可扩展框架,这个系列包括如下清单:

清单:ADO.NET 性能
清单:Review架构和设计的性能和可伸缩性
清单:ASP.NET 性能
清单:企业服务性能
清单:互操作性能
清单:托管代码性能
清单:Remoting性能
清单:SQL Server性能
清单:Web服务性能
清单:XML 性能

数据库服务器清单

清单:SQL Server性能帮助你审查改进您的SQL Server的性能和可扩展性的关键点。这个清单包括向上扩容和平面扩容(译注:scaling up and scaling out,所谓Scale up就是向上扩容,Scale out就是平面型的扩容。两种扩容的方式分别从两个维度来解决数据库压力,纵向的扩容就是将DB Server的配置提高,增加硬件配置,通过硬件速度提升来解决访问压力,横向扩容就是将应用的数据拆分,将原来集中存储的数据根据一定的规则分布到不同的物理数据库服务器上。http://gocom.primeton.com/modules/newbb/item43643_43643.htm),部署,查询,索引,事务,存储过程,查询计划,查询计划重编译,SQL XML,微调(译注:可能是调整一些SQL的配置等),测试和监控。

清单: ADO.NET 性能

 

设计上的考虑

Design your data access layer based on how the data is used.
在你的数据如何使用的基础上设计你的数据库访问层。
Cache data to avoid unnecessary work.
缓存数据以避免不必要的工作。
Connect by using service accounts.
使用服务帐户连接数据库(译注:不要用SA)。
Acquire late, release early.
尽量晚的获取,尽量早的释放(译注:指数据库连接,缓存数据等)。
Close disposable resources.
及时关闭可释放的资源。
Reduce round trips.
减少不必要的来回(译注:有些操作使用一条查询语句就能返回,不要变成多次查询数据库,会加大网络流量及数据库压力)。
Return only the data you need.
只返回你需要的数据(译注:不要使用Select *,应只选择自己需要的列)
Use Windows authentication.
使用windows认证(译注:用数据库认证性能好,用windows认证安全,好像是)。
Choose the appropriate transaction type.
选择适当的事务类型(译注:事务隔离级别有四种,默认是Read Committed)。
Use stored procedures.
使用存储过程(译注:这个有争议,具体看情况)。
Prioritize performance, maintainability, and productivity when you choose how to pass data across layers.
当你选择让数据传递多层的话,尽量考虑性能,可维护性和生产力(译注:productivity这个形容词不知道具体指啥)。
Consider how to handle exceptions.
考虑如何处理异常(译注:SQL里也有try和select @@error)。
Use appropriate normalization.
使用合适的范式(译注:不是范式越高,性能就越好,看具体情况)。

微软.NET数据库库驱动程序

Use System.Data.SqlClient for Microsoft SQL Server? 7.0 and later.
为SQL Server7.0及以上的数据库版本使用System.Data.SqlClient。
Use System.Data.OleDb for SQL Server 6.5 or OLE DB providers.
为SQL Server 6.6或者OLE DB providers使用System.Data.OleDb
Use System.Data.ODBC for ODBC data sources.
为ODBC数据源使用System.Data.ODBC
Use System.Data.OracleClient for Oracle.
为Oracle使用System.Data.OracleClient
Use SQLXML managed classes for XML data and SQL Server 2000.
为XML数据和SQL Server 2000使用SQLXML

数据库连接

Open and close the connection in the method.
在方法里打开和关闭数据库连接。(译注:用的时候打开,不用的时候赶紧关了,关了就自动入池了。)
Explicitly close connections.
显式的关闭数据库连接(译注:不要等着GC给你回收数据库连接)。
When using DataReaders, specify CommandBehavior.CloseConnection.
如果使用DataReaders,指定CommandBehavior.CloseConnection参数(译注:貌似是关闭reader的时候会自动关闭连接)。
Do not explicitly open a connection if you use Fill or Update for a single operation.
如果你为一个单独的操作填充或更新数据,不要显式的打开数据库连接(译注:估计是说DataAdapter,Fill的时候会自动打开数据库连接)。
Avoid checking the State property of OleDbConnection.
避免检查OleDbConnection的State属性。
Pool connections.
使用数据库连接池。

命令

Validate SQL input and use Parameter objects.
验证数据库的输入,使用参数对象(译注:使用参数化查询,否则可能会引起更多的执行计划重编译)。
Retrieve only the columns and rows you need.
只选择你需要的行和列。
Support paging over large result sets.
让大型结果集分页。
Batch SQL statements to reduce round trips.
用批语句来减少不必要的来回。
Use ExecuteNonQuery for commands that do not return data.
如果不需要返回数据,为命令使用ExecuteNonQuery方法。
Use ExecuteScalar to return single values.
用ExecuteScalar方法返回单一值。
Use CommandBehavior.SequentialAccess for very wide rows or for rows with binary large objects (BLOBs).
为非常宽的列及二进制大对象使用CommandBehavior.SequentialAccess参数。
Do not use CommandBuilder at run time.

存储过程
Use stored procedures.
使用存储过程。
Use CommandType.Text with OleDbCommand.
把OleDbCommand.和CommandType.Text一起用(译注:不明白OleDbCommand为什么就要用Text)。
Use CommandType.StoredProcedure with SqlCommand.
把SqlCommand和CommandType.StoredProcedure一起使用(译注:不用CommandType.StoredProcedure就不能执行存储过程了吧)。
Consider using Command.Prepare.
考虑使用Command.Prepare(译注:没用过)。
Use output parameters where possible.
在可能的情况下使用输出参数。
Consider SET NOCOUNT ON for SQL Server.
考虑为SQL Server设置NOCOUNT配置项(译注:不知有何用。)。

参数
Use the Parameters collection when you call a stored procedure.
调用存储过程时使用参数集合。
Use the Parameters collection when you build SQL statements.
建立SQL语句的时候使用参数集合。
Explicitly create stored procedure parameters.
显式创建存储过程参数。
Specify parameter types.
指定参数类型。
Cache stored procedure SqlParameter objects.
缓存存储过程的SqlParameter对象。

DataReader
Close DataReader objects.
记着关闭DataRader对象。
Consider using CommandBehavior.CloseConnection to close connections.
考虑使用CommandBehavior.CloseConnection来关闭数据库连接。
Cancel pending data.
取消未完成的数据(译注:不太明白具体指什么场景)。
Consider using CommandBehavior.SequentialAccess with ExecuteReader.
考虑为ExecuteReader方法使用CommandBehavior.SequentialAccess参数(译注:选取列比较宽的时候)。
Use GetOrdinal when using an index-based lookup.
当使用基于索引的查找时用DataReader.GetOrdinal方法(译注;可能翻译有误)。

DataSet
Reduce serialization.
减少序列化。
Use primary keys and Rows.Find for indexed searching.
为索引搜索使用主键和Rows.Find方法(译注:可能翻译有误)。
Use a DataView for repetitive non-primary key searches.
为非主键的搜索使用DataView对象(译注:先从数据库选出数据,然后再内存里用DataView再筛选数据)。
Use the optimistic concurrency model for datasets.
为Dataset使用乐观并发模型。

XML和DataSet对象
Do not infer schemas at run time.
不要再运行时推演 XML的Schema(译注:不明白具体指啥)。
Perform bulk updates and inserts by using OpenXML.
使用OpenXML方法执行批量更新和插入。

类型
Avoid unnecessary type conversions.
避免不必要的类型转换(译注:不明白具体场景)。

异常管理
Use the ConnectionState property.
使用ConnectionState属性(译注:使用ConnectionState干啥?)。
Use try/finally to clean up resources.
使用try/finally块来清理资源。
Use specific handlers to catch specific exceptions.
捕获具体异常的时候使用具体的handlers。

事务
Use SQL transactions for server controlled-transactions on a single data store.
在单一的数据存储上为服务器事务控制使用SQL事务(译注:翻译可能有误,可能是T-SQL的事务,如begin tran语句)。
Use ADO.NET transactions for client-controlled transactions on a single data store.
在单一的数据存储上为客户端事务控制使用ADO.NET的事务(译注:ADO.NET里有SQLTransaction对象)。
Use Distributed Transaction Coordinators (DTC) for transactions that span multiple
为跨越多个服务的事务使用DTC。

数据存储
Keep transactions as short as possible.
保持事务尽可能短。
Use the appropriate isolation level.
使用合适的隔离级别。
Avoid code that can lead to deadlock.
避免代码导致死锁。
Set the connection string Enlist property to false.
把数据库连接字符串的Enlist属性设置为false(译注:不知有何用)。

二进制大对象
Use CommandBehavior.SequentialAccess and GetBytes to read data.
用CommandBehavior.SequentialAccess和GetBytes读取数据。
Use READTEXT to read from SQL Server 2000.
使用READTEXT从SQL Server 2000读取数据。
Use OracleLob.Read to read from Oracle databases.
使用OracleLob.Read从ORACLE数据库里读取数据。
Use UpdateText to write to SQL Server databases.
使用UpdateText把数据写到SQL Server数据库。
Use OracleLob.Write to write to Oracle databases.
使用OracleLob.Write把数据写到Oracle数据库。
Avoid moving binary large objects repeatedly.
避免重复移动二进制大数据。

原文链接:
Improving .NET Application Performance and Scalability
http://msdn.microsoft.com/en-us/library/ms998530.aspx

http://www.microsoft.com/downloads/details.aspx?FamilyId=8A2E454D-F30E-4E72-B531-75384A0F1C47&displaylang=en

posted @ 2008-12-27 23:21  蛙蛙王子  Views(3452)  Comments(14Edit  收藏  举报