简体   繁体   English

优化SQL数据读取器

[英]Optimizing SQL data reader

I am using SQl data reader to get value from SQL database. 我正在使用SQl数据读取器从SQL数据库获取价值。

Language VB.NET 语言VB.NET

After getting data into reader, i run a while loop 将数据读入阅读器后,我运行一会儿循环

   While reader.Read

      If reader.HasRows() Then

/* Proessing of data */

      End If

  End While

I observe that the while loop takes a lot of time to process as there are many rows. 我观察到while循环要花很多时间,因为有很多行。 Is there any better way to implement. 有没有更好的实现方法。

Please comment on: Should i get the SQlDataReader data into arraylists, and then process data using the arraylists? 请评论:我应该将SQlDataReader数据放入arraylist,然后使用arraylist处理数据吗?

Algo: ALGO:

 While reader.read

    /* Put data into Arraylists */

    End While

    for(arraylist row count)
    /*Process data*/
    end for

Could you place the logic of the loop into SET based logic within the TSQL? 您能否将循环逻辑放入TSQL中基于SET的逻辑中?

So, rather than iterate through each row in code, have the RDBMS execute it in TSQL. 因此,让RDBMS在TSQL中执行它,而不是遍历代码中的每一行。 This is what RDBMS's are designed for and are good at. 这就是RDBMS的设计目标并擅长。

Please tell us what you are doing in the loop and what TSQL is used to generate the dataset. 请告诉我们您在循环中正在做什么,以及使用什么TSQL生成数据集。

It's pointless to check if the data reader has rows inside the loop. 检查数据读取器是否在循环内有行毫无意义。 If there are no rows, the Read method returns false so it will not enter the loop, and if there are rows the HasRows method returns true even when you have read all rows. 如果没有行,则Read方法将返回false,因此它将不会进入循环,并且如果存在行,则即使您已读取所有行, HasRows方法也将返回true。 Removing it will save you some tiny bit of time, but hardly noticable: 删除它可以节省您一点点时间,但几乎不会引起注意:

While reader.Read

  /* Proessing of data */

End While

Reading all the data into a list before processing will not give you any better performance. 在处理之前将所有数据读取到列表中不会给您带来更好的性能。 On the contrary you will be using a lot of memory that might be better used for something else. 相反,您将使用大量的内存,这些内存可能会更好地用于其他用途。

Don't assume that reading the data from the database is what's taking a lot of time. 不要以为从数据库中读取数据会花费很多时间。
You can measure the time elapsed in a block of VB code like: 您可以在一段VB代码中测量经过的时间,例如:

startTime = Now
....
stopTime = Now
elapsedTime = stopTime.Subtract(startTime)

Measure first, and then optimize the thing that's really taking time. 首先进行测量,然后优化真正需要时间的事情。

(PS if you're using Team Studio, you could try the built-in profiler.) (PS,如果您使用的是Team Studio,则可以尝试使用内置的探查器。)

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

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