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

  • Home
  • SEARCH
  • 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 8059919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:49:19+00:00 2026-06-05T09:49:19+00:00

In the program I created the following logic for reading the data from the

  • 0

In the program I created the following logic for reading the data from the database and storing it into List<>:

                NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
                List<UserInfo> result = new List<UserInfo>();
                Npgsql.NpgsqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    string userId = rdr[0].ToString();
                    string sex = rdr[1].ToString();
                    string strDateBirth = rdr[2].ToString();
                    string zip = rdr[3].ToString();

                    UserInfo userInfo = new UserInfo();
                    userInfo.Msisdn = userId;
                    userInfo.Gender = sex;
                    try
                    {
                        userInfo.BirthDate = Convert.ToDateTime(strDateBirth);
                    }
                    catch (Exception ex)
                    {
                    }
                    userInfo.ZipCode = zip;
                    userInfo.DemographicsKnown = true;
                    userInfo.AgeGroup = getAgeGroup(strDateBirth);
                    if (result.Count(x => x.Id== userId) == 0)
                        result.Add(userInfo);
                }

The performance of this code is really poor. There are over 2M of records and after half an hour the list userInfo contains just 300.000 records.

Does anyone know how to speed up data reading from the database?

  • 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-06-05T09:49:21+00:00Added an answer on June 5, 2026 at 9:49 am

    You are using .Count when you really mean .Any()
    Whenever you call .Count you are enumerating the entire collection just to see if you have a single match….

    Consider the question you’re asking:
    “How many rows do you have that match this condition? Is that number equal to zero?”

    What you really mean is:
    “Do any rows match this condition?”

    In that context, you could create a Hashset of the userId values. Checking for the existence in a Hashset (or dictionary) can be much faster than checking the same in a list.

    Furthermore, if you do already have the userId, then you parsed and read all the values for no reason. Check for myHashset.Contains(userId) first, then add.

    This is the primary reason it’s slow. For n rows you’re performing the nth-triangular enumerations of the collection!

    EDIT: Consider this untested change: I don’t know if your reader supports typed read methods like GetString() so if it doesn’t then simply use what you had before.

    NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
    List<UserInfo> result = new List<UserInfo>();
    Npgsql.NpgsqlDataReader rdr = cmd.ExecuteReader();
    HashSet<string> userHash = new HashSet<string>(); // is this actually an int?
    
    while (rdr.Read())
    {
        string userId = rdr.GetString(0);
        If (!userHash.Contains(userId))
        {
            string strDateBirth = rdrGetString(2);
            UserInfo userInfo = new UserInfo();
            userInfo.Msisdn = userId;
            userInfo.Gender = rdr.GetString(1);
            datetime parseddate; // this is not used if the parse fails
            if (Datetime.TryParse(strDateBirth, out parseddate))
            {
                userInfo.BirthDate = parseddate;
                // userInfo.AgeGroup = getAgeGroup(strDateBirth); // why take the string?
                // rewrite your getAgeGroup method to take the datetime
                userInfo.AgeGroup = getAgeGroup(parseddate);
            }
            userInfo.ZipCode = rdr.GetString(3);
            userInfo.DemographicsKnown = true;
            result.Add(userInfo);
            userHash.Add(userId);
        }
    }
    

    This will always keep the first instance of a user row you find (which is what your current code does). If you want to keep the last instance then you can use a dictionary and eliminate the .Contains() call altogether.

    EDIT: I just noticed that my sample never added the userId to the hash… whoops… added it in there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to the package java.util.concurrent . I've created the following program testing if
Using the TinyCarousel program I created the following sample page. http://www.dealred.com/wa/jqimage.htm The carousel is
I have created the following program which allows a user to guess a word
As a debugger/test program for my brainf*ck implementation, I have created the following counting
I'm new to C and I have created the following code, but when run
I have created the following test program: static void Main(string[] args) { using (var
The following currently takes my dependencies and places them into newly created lib directory.
With win32api, I want that the following program creates two process and creates a
I cannot figure out why the following simple program doesn't (create) and then write
I have to refactor a Java program created with Eclipse RCP. My predecessor handed

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.