简体   繁体   English

C#获取从中返回的数据大小和SQL查询

[英]C# Getting the size of the data returned from and SQL query

How can I get the size in bytes of the data returned from the database when executing a query? 执行查询时,如何获取从数据库返回的数据的大小(以字节为单位)? The reason for this is to compare load on the database server using two different techniques. 这样做的原因是使用两种不同的技术来比较数据库服务器上的负载。 We were running reports built from the same dataset which would load the entire dataset for every report. 我们正在运行从相同数据集构建的报表,这将为每个报表加载整个数据集。 Now we are caching the dataset and running reports from the cache. 现在,我们正在缓存数据集并从缓存中运行报告。 We run reports per client, some datasets are significantly bigger than others, and I need some way to give a measurable metric for the database server load reduction. 我们为每个客户端运行报告,一些数据集明显大于其他数据集,并且我需要某种方法来提供可衡量的指标来减少数据库服务器的负载。

I have tried looking through DbConnection, DbDataReader and DbCommand for any existing functionality, but was unable to find it. 我尝试通过DbConnection,DbDataReader和DbCommand查找任何现有功能,但无法找到它。 It would be great to measure the data throughput of a given connection or reader if possible, but any solutions are acceptable. 如果可能,最好测量给定连接或读取器的数据吞吐量,但是任何解决方案都是可以接受的。 Is there perhaps a database server proxy I can use to measure it? 也许有一个我可以用来衡量的数据库服务器代理?

The database is Oracle 10g. 该数据库是Oracle 10g。

One possibility would be to simply use a network sniffer. 一种可能性是仅使用网络嗅探器。 Wireshark is amazingly good. Wireshark非常好。 It would be tricky to measure by connection (when using multiple connections on a given machine), but you could use it to measure all the traffic to and from a client machine. 按连接进行度量(在给定计算机上使用多个连接时)比较棘手,但是您可以使用它来度量往返客户端计算机的所有流量。 One benefit of this is that it would also measure outgoing requests, which should be small in your situation (report generation) but are still a part of the overall load. 这样做的好处之一是它还可以测量传出的请求,在您的情况下(报告生成),传出的请求应该很小,但仍然是总负载的一部分。

Another benefit of measuring it this way is that it would find differences (if there are any) in the size of requests being made. 用这种方法进行度量的另一个好处是,它将发现发出的请求大小不同(如果有)。 For example, if one method caused individual records to be read from the server in separate requests and the other method caused a "batch" of records to be read in one request, then you would be able to see those differences. 例如,如果一种方法导致在单独的请求中从服务器读取单个记录,而另一种方法导致在一个请求中读取记录的“批处理”,那么您将能够看到这些差异。 Both methods in this case might show that the total data at the DbDataReader level is the same, but the first method would result in significantly more network traffic. 在这种情况下,这两种方法都可能表明DbDataReader级别的总数据相同,但是第一种方法将导致更多的网络流量。

Wireshark shows a lot of statistics that could be useful for this. Wireshark显示了很多统计信息可能对此有用。 It can give total number of packets, average size of packets, total size, average bytes per second, etc. 它可以给出数据包总数,数据包平均大小,总大小,每秒平均字节数等。

Have you tried measuring it on the database side? 您是否尝试过在数据库方面进行测量? Oracle 10g appears have a whole stack of performance tuning and monitoring tools . Oracle 10g似乎具有一整套性能调整和监视工具

you can't get size of result set except of looping all over it. 除了遍历整个结果集,您无法获得结果集的大小。 if you can't afford it from preformace point of view you can return size of result set from server in first row of result set. 如果您不能从表演前的角度承受它,则可以在结果集的第一行中从服务器返回结果集的大小。 or return two result sets. 或返回两个结果集。

I don't think there is any easy way of doing this. 我认为没有任何简单的方法可以做到这一点。 In order to get the metadata, you really need the data to be represented as DataSet or DataTable not through the DbDataReader or DataRow so that all keys, columns and metadata are represented as a whole. 为了获取元数据,您确实需要不通过DbDataReader或DataRow将数据表示为DataSet或DataTable,以便所有键,列和元数据都表示为一个整体。

What I will do is to create a binary formatter and memorystream and serialize the DataSet/DataTable into the memorystream as binary data. 我将要做的是创建一个二进制格式化程序和memorystream,并将DataSet / DataTable作为二进制数据序列化到memorystream中。 You should then be able to ask for the length from the stream. 然后,您应该能够从流中询问长度。 This should quite accurately tells you about the size of the data as posted on your question 这应该可以非常准确地告诉您有关问题的数据大小

This is probably a performance killer so you may only want to use it in debugging environment only to understand about the size of your data 这可能是一个性能杀手,因此您可能只想在调试环境中使用它,以便了解您的数据大小

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM