SqlDataReader 在访问值方面的性能差异?

发布于 2024-07-12 12:01:09 字数 442 浏览 6 评论 0原文

当从 SqlDataReader 访问值时,这两者之间是否存在性能差异:

string key = reader.GetString("Key");

或者

string key = reader["Key"].ToString();

在此代码示例中:

string key;

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        key = reader.GetString("Key");
        // or
        key = reader["Key"].ToString();
    }
}

When accessing values from an SqlDataReader is there a performance difference between these two:

string key = reader.GetString("Key");

or

string key = reader["Key"].ToString();

In this code sample:

string key;

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        key = reader.GetString("Key");
        // or
        key = reader["Key"].ToString();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幽梦紫曦~ 2024-07-19 12:01:09

我刚刚查看了.NET源代码,两种访问方法本质上是相同的。 唯一的区别是第二个进行了额外的拳击。 因此,第一个对应于类似的内容(在基本类型的情况下):

int key = GetInternalSQLDataAsInt32("Key");

而第二个对应的是:

int key = (int)(object)GetInternalSQLDataAsInt32("Key");

GetInternalSQLDataAsInt32(...) 函数表示将数据从 SQL 编组到 .NET 的 SQL 数据库机制。

但如上所述,基于字符串的键和基于序数的键之间预计会有更显着的差异。

I’ve just peeked into the .NET source code, and the two access methods are essentially the same. The only difference is that the second does an additional boxing. So the first one corresponds to something like (in case of elementary types):

int key = GetInternalSQLDataAsInt32("Key");

while the second one would be:

int key = (int)(object)GetInternalSQLDataAsInt32("Key");

The GetInternalSQLDataAsInt32(...) function represents the SQL data library machinery of marshalling data from SQL to .NET.

But as pointed above, a more significant difference can be expected between string-based keys and ordinal-based keys.

猫瑾少女 2024-07-19 12:01:09

一位 Microsoft MVP 就此撰写了一篇博客文章:
http://jeffbarnes. net/portal/blogs/jeff_barnes/archive/2006/08/09/Maximize-Performance-with-SqlDataReader.aspx

这是他的结论:

                                    .NET 1.1    .NET 2.0
Ordinal Position:                   8.47        9.33
Get Methods:                        11.36       8.07
Case Sensitive:                     14.51       12.53
Case Insensitive (All Upper):       13.93   12.23
Case Insensitive (All Lower):       14.47   12.48
Case Insensitive (Mixed):           14.48   12.57

但实际上,除非您正在处理大量的行,否则它不是不值得担心。

A Microsoft MVP wrote a blog entry about this:
http://jeffbarnes.net/portal/blogs/jeff_barnes/archive/2006/08/09/Maximize-Performance-with-SqlDataReader.aspx

Here are his conclusions:

                                    .NET 1.1    .NET 2.0
Ordinal Position:                   8.47        9.33
Get Methods:                        11.36       8.07
Case Sensitive:                     14.51       12.53
Case Insensitive (All Upper):       13.93   12.23
Case Insensitive (All Lower):       14.47   12.48
Case Insensitive (Mixed):           14.48   12.57

But really, unless you are dealing with a significant number of rows, it isn't worth worrying about.

空心空情空意 2024-07-19 12:01:09

即使有,与从数据库获取数据的成本相比,它也不太可能有丝毫意义。

就我个人而言,我更喜欢第一个版本 - 它表明我希望该列是字符串列,而不是采用数字并将其转换为字符串。

Even if there is, it's unlikely to be significant in the slightest compared with the cost of getting the data from the database in the first place.

Personally I prefer the first version - it shows that I expect the column to be a string column, rather than taking a number and converting it into a string.

我们只是彼此的过ke 2024-07-19 12:01:09

尽管您的代码需要修改; 使用序数是最快的方法。

Although your code would need to be modified; using the ordinal is the fastest method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文