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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Microsoft 建议不要在循环内调用 GetOrdinal。
这包括使用字符串索引器间接调用。
您可以在循环顶部使用 GetOrdinal 将序号放入数组中,并使数组中的索引为 const 或为它们提供一个枚举(根本没有 GetOrdinal),或者使用 GetOrdinal 到具有描述性名称的各个变量中。
只有当你的集合很小时,我才会真正认为这是不成熟的优化。
这显然是 3 % 惩罚。
Microsoft recommends not calling GetOrdinal within a loop.
That would include indirect calls with the string indexer.
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.
It's apparently a 3% penalty.
任何差异都会被维护开销所抵消。
如果您拥有如此多的数据以至于会产生明显的差异,我建议您的客户端代码中的数据过多。或者当您考虑使用序数而不是名称时
Any difference will be more than outweighed by maintenance overhead.
If you have that much data that it makes a noticeable difference, I'd suggest you have too much data in your client code. Or this is when you consider use ordinals rather than names
是的,也不是。
如果您正在处理大量数据,那么使用序数而不是列名肯定会让您受益匪浅。
否则,请保持简单、可读且更安全——并坚持使用列名称。
仅在需要时进行优化。
Yes and no.
If you're dealing with a massive amount of data then you'd certainly benefit from using the ordinals rather than the column names.
Otherwise, keep it simple, readable, and somewhat safer - and stick with the column names.
Optimize only when you need to.
我为 SqlDataReader 创建了一个包装器,它将 orindals 存储在字典中,并以列名作为键。
它使我获得了顺序性能提升,同时保持代码更具可读性,并且如果有人更改从存储过程返回的列顺序,则不太可能中断。
I created a wrapper for SqlDataReader that stores orindals in a dictionary with the column name as the key.
It gives me ordinal performance gains while keeping the code more readable and less likely to break if someone changes the column order returned from stored procedures.