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 6155379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:25:32+00:00 2026-05-23T20:25:32+00:00

I have a sequence of sql queries that result in very large datasets that

  • 0

I have a sequence of sql queries that result in very large datasets that I have to query against a database and write them to files. I have about 80 queries and each one produces somewhere between 1000 records to 10,000,000 records. I cannot change the queries themselves. What I’m trying to do is read 500,000 records at a time for each query and write to a file. Here’s what I have so far

void WriteXml(string tableName, string queryString)
{
    int pageSize = 500000;
    int currentIndex = 0;
    using (
       SqlConnection connection =
           new SqlConnection(CONNECTION_STRING))
    {
        using (SqlCommand command = new SqlCommand(queryString, connection))
        {
            try
            {
                connection.Open();
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                int rowsRead = 0, count = 0, index = 0;
                do
                {
                    DataSet dataSet = new DataSet("SomeDatasetName");
                    rowsRead = dataAdapter.Fill(dataSet, currentIndex, pageSize, tableName);
                    currentIndex += rowsRead;
                    if (dataSet.Tables.Count > 0 && rowsRead > 0)
                    {
                        dataSet.Tables[0].WriteXml(string.Format(@"OutputXml\{0}_{1}.xml", tableName, index++),
                                                   XmlWriteMode.WriteSchema);
                    }
                }
                while (rowsRead > 0);

            }
            catch (Exception e)
            {
                Log(e);
            }
        }
    }
}

This works but it’s very very slow. I’m pretty sure I’m doing something wrong here because when I run it, the application hogs up most of my memory (I have 6GB) and it takes for ever to run. I started it last night and it is still running. I understand I’m dealing with a lot a records but I don’t think it’s something that would take so many hours to run.

Is this the right way to do paged/segmented data read from a database? Is there any way this method could be optimized or is there any other way I can approach this?

Do let me know if I’m not clear on anything and I’ll try to provide clarification.

  • 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-23T20:25:33+00:00Added an answer on May 23, 2026 at 8:25 pm

    The paging overloads for DataAdapter.Fill still get the entire result set beneath the covers. Read here:

    http://msdn.microsoft.com/en-us/library/tx1c9c2f%28vs.71%29.aspx

    the part that pertains to your question:

    The DataAdapter provides a facility for returning only a page of data,
    through overloads of the Fill method. However, this might not be the
    best choice for paging through large query results because, while the
    DataAdapter fills the target DataTable or DataSet with only the
    requested records, the resources to return the entire query are still
    used. To return a page of data from a data source without using the
    resources required to return the entire query, specify additional
    criteria for your query that reduces the rows returned to only those
    required.

    In Linq2Sql, there are convenient methods Skip and Take for paging through data. You could roll your own by using a parameterized query constructed to do the same thing. Here is an example to skip 100, and take 20 rows:

    SELECT TOP 20 [t0].[CustomerID], [t0].[CompanyName],
    FROM [Customers] AS [t0]
    WHERE (NOT (EXISTS(
        SELECT NULL AS [EMPTY]
        FROM (
            SELECT TOP 100 [t1].[CustomerID]
            FROM [Customers] AS [t1]
            WHERE [t1].[City] = @p0
            ORDER BY [t1].[CustomerID]
            ) AS [t2]
        WHERE [t0].[CustomerID] = [t2].[CustomerID]
        ))) AND ([t0].[City] = @p1)
    ORDER BY [t0].[CustomerID]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Linq to SQL query sequence that is basically returning a search
I have a web service (JAX-RS/Spring) that generates SQL queries which run against a
Possible Duplicate: SQL query to find Missing sequence numbers I have a table which
I have a linq to sql database. Very simplified we have 3 tables, Projects
I'm helping implement a SQL Server 2008 database and I have several columns that
I have a sequence defined in my database that I want to use in
I have dropped on this question: SQL query to find Missing sequence numbers .
Lets say I have a database that looks like this: tblA: ID, Name, Sequence,
I have a sequence of SQL calls, which I want to use to detect
I was wondering if this was possible... I have a sequence of variables that

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.