Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:28:36+00:00 2026-05-31T11:28:36+00:00

I’m having a time issue with looping through like 1 million potential rows from

  • 0

I’m having a time issue with looping through like 1 million potential rows from a database. I basically pull the rows into a DataTable and loop through them, but it is getting slow. What is the alternative out there? I can split this rows into chunks like 20,000 a piece. Can I use parallel processing in C#? Basically the code loops through every potential record that matches a certain query and tries to figure out if it is a legitimate entry. That is why every record needs to be individually visited. A record for a one object could reach 10 million rows. Approaches seem like parallel processing in multiple computers or PP in single machine with multiple cores, or some kind of data structure/approach change?

Any opinions, thoughts and guesses are helpful to make this fast and reasonable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:28:38+00:00Added an answer on May 31, 2026 at 11:28 am

    First off: Do not use DataTable for operations like these:

    • it’s slow
    • it’s consuming too much memory
    • and you need to wait a long time before you can actually start processing the data
      • During this time additional cores are doing nothing, since reading data into a DataTable is not parralized.
      • Also while reading data the CPU is usually almost under utilized, since the network or other I/O delay is often the leading factor.

    So again: Do not use DataTable for operations like these.

    Instead use the DataReader. This allows you to immediately start consuming/process the data, instead of waiting for it to be loaded. The simplest version would be (sample for MS SQL Server):

    var command = new SqlCommand()
    {
      CommandText = "SELECT * FROM Table";
      Connection = new SqlConnection("InsertConnectionString");
    };
    
    using(var reader = command.ExecuteReader())
    {
      while(reader.Read())
      {
        var values = new object[reader.FieldCount];
        reader.GetValues(values);
    
        // process values of row
      }
    }
    

    The reader will be blocked while executing your processing code, meaning no more rows are read from the DB.
    If the processing code is heavy, it might be worth it to use the Task library to create Tasks that perform the check, which would enable you to make use of multiple cores. However, there is an overhead of creating a Task, if one Task does not contain enough ‘work’ you can batch a couple of rows together:

    public void ReadData()
    {
      var taskList = new List<Task<SomeResultType>>();
    
      var command = new SqlCommand()
      {
        CommandText = "SELECT * FROM Table";
        Connection = new SqlConnection("InsertConnectionString");
      };
      using(var reader = command.ExecuteReader())
      {
        var valueList = new List<object[]>(100);
        while(reader.Read())
        {
          var values = new object[reader.FieldCount];
          reader.GetValues(values);
    
          valueList.Add(values);
    
          if(valueList.Count == 100)
          {
            var localValueList = valueList.ToList();
            valueList.Clear();
    
            taskList.Add(Task<SomeResultType>.Factory.StartNew(() => Process(localValueList));
          }
        }
        if(valueList.Count > 0)
          taskList.Add(Task<SomeResultType>.Factory.StartNew(() => Process(valueList));
      }
    
      // this line completes when all tasks are done
      Task.WaitAll(taskList.ToArray());
    }
    
    public SomeResultType Process(List<object[]> valueList)
    {
      foreach(var vals in valueList)
      {
        // put your processing code here, be sure to synchronize your actions properly
      }  
    }
    
    • The batch size (currently 100) depends on the actual processing being done and might need to be adjusted.
    • Synchronizing holds it’s own challenges, you need to be very careful about shared resources
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.