简体   繁体   English

CommandBehavior.SequentialAccess是否有任何性能提升?

[英]Is there any performance gain from CommandBehavior.SequentialAccess?

I realized I always read my fields in the order they are returned by index (using constants). 我意识到我总是按索引返回的顺序读取我的字段(使用常量)。 So my code is already compatible with CommandBehavior.SequentialAccess as far as i understand. 所以据我所知,我的代码已经与CommandBehavior.SequentialAccess兼容。

Would there be any benefits if i turn it on? 如果我打开它会有任何好处吗? DataReader is already forward only, read only which is the real performance gain right? DataReader只是前传,只读是真正的性能增益吗?

The main usage of this is when you are reading very large CLOB ( nvarchar(max) etc) or BLOB ( varbinary(max) ) fields. 这种情况的主要用途是在读取非常大的CLOB( nvarchar(max)等)或BLOB( varbinary(max) )字段时。 In the default usage, it buffers the entire row of data before letting you near it - which could mean it has to allocate a large buffer for any BLOB / CLOB fields. 在默认用法中,它会在让您靠近它之前缓冲整行数据 - 这可能意味着它必须为任何BLOB / CLOB字段分配一个大缓冲区。 When using sequential mode, it does not buffer the row ; 使用顺序模式时,它不缓冲行 ; you can use the regular API for small fields (as long as you access them in the correct order), but for the CLOB / BLOB fields you can use the chunk-based APIs ( GetBytes and GetChars ) to access fractions of the data in turn. 您可以将常规API用于小字段(只要以正确的顺序访问它们),但对于CLOB / BLOB字段,您可以使用基于块的API( GetBytesGetChars )依次访问数据的分数。 By doing this you could, for example, process a 40 MB image using only a 1k or 4k buffer. 通过这样做,您可以,例如,仅使用1k或4k缓冲区处理40 MB图像。

MSDN says the same MSDN说的一样

Provides a way for the DataReader to handle rows that contain columns with large binary values. 为DataReader提供一种处理包含具有大二进制值的列的行的方法。 Rather than loading the entire row, SequentialAccess enables the DataReader to load data as a stream. SequentialAccess不是加载整行,而是使DataReader能够将数据作为流加载。 You can then use the GetBytes or GetChars method to specify a byte location to start the read operation, and a limited buffer size for the data being returned. 然后,您可以使用GetBytes或GetChars方法指定用于启动读取操作的字节位置,以及用于返回数据的有限缓冲区大小。

Yes, there is supposed to be at least some performance gain for using CommandBehavior.SequentialAccess , even when not accessing BLOBs. 是的,即使不访问BLOB,使用CommandBehavior.SequentialAccess也应该至少有一些性能提升。 The Microsoft KB article, An "Invalid attempt to read from column ordinal" error occurs when you use DataReader in Visual C# , states: Microsoft KB文章, 在Visual C#中使用DataReader时出现“无效尝试从列序号读取”错误 ,指出:

Setting the CommandBehavior.SequentialAccess flag causes the DataReader to read both rows and columns sequentially. 设置CommandBehavior.SequentialAccess标志会导致DataReader按顺序读取行和列。 The rows and columns are not buffered. 行和列不缓冲。 After you read past a column, it is dropped from memory. 读过列后,它将从内存中删除。 Any attempt to re-read the column or read previously read columns results in an exception. 任何重新读取列或读取先前读取的列的尝试都会导致异常。

Using the CommandBehavior.SequentialAccess flag provides a performance benefit, especially when using Binary Large Object (BLOB) fields. 使用CommandBehavior.SequentialAccess标志可提供性能优势,尤其是在使用二进制大对象(BLOB)字段时。 If you do not use SequentialAccess , all the BLOB data is copied to the client. 如果不使用SequentialAccess ,则会将所有BLOB数据复制到客户端。 This can consume a lot of resources. 这可能会消耗大量资源。

CommandBehavior.SequentialAccess also improves performance when accessing non-BLOB fields. CommandBehavior.SequentialAccess还可以在访问非BLOB字段时提高性能。 When CommandBehavior.SequentialAccess is not set, you can access a column out of order; 如果未设置CommandBehavior.SequentialAccess,则可以不按顺序访问列; however, you incur the following overhead: 但是,您会产生以下开销:

  • The column is checked to see if the column is later than a previous accessed column. 检查该列以查看该列是否晚于先前访问的列。
  • The data for all of the previously accessed columns is retrieved and then cached for potential later retrieval. 检索所有先前访问的列的数据,然后对其进行高速缓存以供以后检索。

Columns must be checked and cached because when you use the DataReader , the underlying stream is forward-only for rows as well as column access. 必须检查和缓存列,因为在使用DataReader时 ,基础流仅对行进行转发以及列访问。

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

相关问题 SQLDataLReader和CommandBehavior.SequentialAccess - SQLDataLReader and CommandBehavior.SequentialAccess 带有ExecuteReaderAsync的ASP.NET webapi HttpResponseMessage CommandBehavior.SequentialAccess - ASP.NET webapi HttpResponseMessage with ExecuteReaderAsync CommandBehavior.SequentialAccess 使用C#6.0中的功能时是否有任何性能提升? - Is there any performance gain when using features from C# 6.0? 在C#中使用类型后缀可以提高性能吗? - Is there any performance gain from using type suffixes in C#? 通过声明循环外的对象是否有任何性能提升 - Is there any performance gain by declaring an object outside the loop 使用Indexer for Objects可以提高性能吗? - Is there any performance gain when using Indexer for Objects? 在列表上使用“任意”等时的任何性能提升 - Any performance gain when using Any etc on List 在数据访问层中使用异步时是否有任何性能提升? - Is there any performance gain when using async in Data Access Layer? C#性能增益从SqlDataReader返回Nullable Type - C# Performance gain returning a Nullable Type from a SqlDataReader 可以在我当前的网站上添加命名空间,是否可以提高性能或灵活性? - Can Adding Namespace to my current website, will give me any performance or flexibility Gain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM