由于...,DataSet 是否比 DataReader 慢? [英] Is DataSet slower than DataReader due to...?

查看:14
本文介绍了由于...,DataSet 是否比 DataReader 慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataSets 在从 DB 检索数据时可能比 DataReader 慢 10 倍以上.我认为这是由于 DataSets 必须处理关系等的开销.但是 DataSetsDataReader 之间的速度差异是由于 DataSets 必须从 DB 中检索更多数据(有关关系的信息......),或者由于应用程序必须进行更多处理,或者两者兼而有之?

DataSets can be 10+ times slower than DataReader at retrieving data from DB. I assume this is due to overhead of DataSets having to deal with relations etc. But is the speed difference between DataSets and DataReader due to DataSets having to retrieve more data (information about relations ...) from DB, or due to application having to do more processing, or both?

我假设 DataAdapter 在后台使用 DataReader,因此应用程序需要执行的命令数才能使用 DataAdapter 检索 100 行如果这 100 行由 DataReader 直接检索,则等于或大于应用程序需要执行的命令数.DataReader 是一次检索一行还是一次检索一个(特定行的)字段?

I assume DataAdapter uses DataReader under the hood and thus the number of commands application needs to execute in order to retrieve 100 rows with DataAdapter is equal to or greater than number of commands app needs to execute if these 100 rows are retrieved directly by DataReader. Does DataReader retrieve one row at the time or one field (of a particular row) at a time?

推荐答案

在 DataReader 上使用 DataSet 时可能会出现一些不同类型的开销:

There are some different types of overhead that can occur when using a DataSet over a DataReader:

DatSet 包含包含数据的 DataTable 对象,其中包含 DataRow 对象.创建所有对象的开销很小.每个 DataRow 都将其所有值视为对象,因此任何值类型都被装箱,这为每个字段增加了一些开销.

A DatSet contains DataTable objects, which contains DataRow object, that contain the data. There is a small overhead creating all the objects. Each DataRow treats all it's values as objects, so any value types are boxed which adds a bit of overhead for each field.

当您使用 DataAdapter 填充 DataSet 时,很容易获得大量您不会使用的数据.如果您不指定所需的字段,即使您不会全部使用它们,您也会获得所有字段.如果您不过滤查询,则会从表中获取所有行.即使您稍后使用 DataTable 上的 DataView 过滤它们,您仍然可以从数据库中获取它们.使用 DataReader,您更接近于获取数据的查询,因此与您在结果中获得的内容之间的联系更加明显.

When you use a DataAdapter to populate a DataSet, it's easy to get a lot of data that you won't use. If you don't specify what fields you want, you get all the fields even if you won't use them all. If you don't filter the query, you get all the rows from the table. Even if you filter them later with a DataView on the DataTable, you still have fetched them from the database. With a DataReader you are closer to query that gets the data, so the connection to what you get in the result is more obvious.

如果您将数据提取到 DataSet 中的多个 DataTable 对象中,并使用关系让 DataSet 组合数据,那么您可以让 DataSet 完成您本可以让数据库完成的工作,这对其进行了更优化.

If you fetch data into several DataTable objects in a DataSet and use relations to let the DataSet combine the data, you make the DataSet do work that you could have let the database do, which is more optimised for it.

如果你很好地使用了 DataSet,开销也不会那么糟糕,而是 30% 而不是 1000%.

If you use a DataSet well, the overhead is not that bad, rather 30% than 1000%.

假设 DataAdapter 使用 DataReader 是正确的.如果你小心使用 DataAdapter,那么数据库操作本身就和你自己使用 DataReader 一样.

You are correct to assume that a DataAdapter uses a DataReader. If you are careful how you use the DataAdapter, the database operations itself is the same as if you use the DataReader yourself.

DataReader 将一次从底层数据库驱动程序获取一条记录,而该驱动程序又将一次从数据库中获取一个充满记录的缓冲区.如果记录非常大,一次可能只有一条记录可以放入缓冲区,但通常缓冲区中有数十条记录,如果它们真的很小,甚至有数百条记录.

A DataReader will fetch a record at a time from the underlying database driver, which in turn will fetch a buffer full of records at a time from the database. If the records are very large only one at a time might fit in the buffer, but usually there are tens of records in the buffer or even hundreds if they are really small.

这篇关于由于...,DataSet 是否比 DataReader 慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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