DataReader 基于序数的查找与命名查找 [英] DataReader ordinal-based lookups vs named lookups

查看:96
本文介绍了DataReader 基于序数的查找与命名查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft(以及许多开发人员)声明 SqlDataReader.GetOrdinal 方法提高了从 DataReader 检索值的性能,而不是使用命名查找,即.读者[列名"].问题是,如果处理小的分页记录集,真正 的性能差异是什么?在整个代码中查找和引用有序索引的额外开销值得吗?

Microsoft (and many developers) claim that the SqlDataReader.GetOrdinal method improves the performance of retrieving values from a DataReader versus using named lookups ie. reader["ColumnName"]. The question is what is the true performance difference if dealing with small, paged record sets? Is it worth the extra overhead of finding and referencing ordinal indexes throughout the code?

推荐答案

Microsoft 建议不要在循环内调用 GetOrdinal.

这将包括对字符串索引器的间接调用.

That would include indirect calls with the string indexer.

您可以在循环的顶部使用 GetOrdinal 将序数放在一个数组中,并使数组中的索引为 const 或为它们设置一个枚举(根本没有 GetOrdinal),或者将 GetOrdinal 用于具有描述性名称的单个变量.

You can use GetOrdinal at the top of your loop put the ordinals in an array and have the indexes in the array be const or have an enum for them (no GetOrdinal at all) or use GetOrdinal into individual variables with descriptive names.

只有当你的集合很小时,我才会真正认为这是过早的优化.

Only if your sets are small would I really consider this to be premature optimization.

它是显然是 3% 的罚款.

这篇关于DataReader 基于序数的查找与命名查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆