Results 1 to 6 of 6

Thread: Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multiple,

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2020
    Posts
    70

    Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multiple,

    We are using VB.NET with System.Data.DataTable objects that contain large volumes of data. We are evaluating which approach is the fastest and most efficient for selecting rows in specific scenarios.
    1. Single row
    2. Multiple rows
    3. Single row with simple condition
    4. Single row with complex condition
    5. Multiple rows with simple condition
    6. Multiple rows with complex condition
    Which method is best for each case? Rows.Find(), DataTable.Select(), Manual Loops (For Each), LINQ, DataView.RowFilter? Any other methods?
    Any performance benchmarks or timing comparisons for these methods would be helpful.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multip

    What version of .NET are you using?

    Regardless, ADO.NET operations will probably be the fastest because it is the de facto low-level foundation for data access in .NET. The reason I ask is because Microsoft revamped ADO.NET around .NET 9 when they deprecated System.Data.SqlClient in favor of Microsoft.Data.SqlClient. This change really boosted performance, particularly with bulk copying, but with your typical every day selects too.

    I also need to know how you are selecting the data for a single row. Because if DataTable has its PrimaryKey set, then the absolute 100% best way to select a row is by using the Find method.

    Also, here's an older article but still relevant: https://chrisbitting.com/2016/05/04/...rallel-vs-for/

    Basically, the idea is that if you cannot use the Find method, then the second-best approach for large datasets is to use a parallel for loop to iterate over the result set. If you're returning a single row, then have it exit the loop prematurely and if you're returning multiple rows then don't. Here are some simple examples:
    Code:
    Public Function FindSingleParallel(table As DataTable, predicate As Func(Of DataRow, Boolean)) As DataRow
        Dim result As DataRow = Nothing
    
        Try
            Parallel.ForEach(table.AsEnumerable(), Sub(row, loopState)
                If (result IsNot Nothing) Then
                    Return
                End If
                If (predicate(row)) Then
                    If (Interlocked.CompareExchange(result, row, Nothing) Is Nothing) Then
                        loopState.Stop()
                    End If
                End If
            End Sub)
        Catch ex As OperationCanceledException
            ' Expected when a match is found
        End Try
    
        Return result
    End Function
    
    Public Function FindManyParallel(table As DataTable, predicate As Func(Of DataRow, Boolean)) As IEnumerable(Of DataRow)
        Dim results As New ConcurrentBag(Of DataRow)
    
        Parallel.ForEach(table.AsEnumerable(), Sub(row)
            If (predicate(row)) Then
                results.Add(row)
            End If
        End Sub)
    
        Return results
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Junior Member
    Join Date
    Feb 2026
    Posts
    19

    Re: Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multip

    From a data perspective, can keep large tables on the DB and run the SQL necessary to pull whatever you need, as opposed to bringing those tables into your app.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multip

    I agree. Whenever possible, get just what you need. It may seem like working with a datatable in memory is going to be superior to doing round trips to the DB, and sometimes that is true, but often it is not. Is there a reason why you need large amounts of data in memory? If so, then you have to do so, but consider whether there isn't an alternative.
    My usual boring signature: Nothing

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multip

    Would also be helpful to know what "large volumes of data" is in this case. 10k, 100k, ....... rows. Are the columns per row large?

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Fastest & Most Efficient DataTable Row Selection Methods in VB.NET (Single/Multip

    IT_Researcher looking for free help. Best performance? Let the DB engine handle it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width