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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:57:22+00:00 2026-05-15T13:57:22+00:00

I have written a tool for database replication in PHP. It’s working fine but

  • 0

I have written a tool for database replication in PHP. It’s working fine but there’s one issue:

I’m using PDO to connect to the different databases to keep it independent of any specific RDBMS, which is crucial to this application.

The tool does some analysis on the tables to decide how to convert certain types and some other stuff. Then it pretty much does a “SELECT * FROM <tablename>” to get the rows that need to be copied. The result sets are fairly large (about 50k rows in some tables).

After that it iterates over the result set in a while loop with PDOStatement::fetch();, does some type conversion and escaping, builds an INSERT statement and feeds that to the target database.

All this is working nicely with one exception. While fetching the rows, one ata time, from the result set, the PHP process keeps eating up more and more memory. My assuption is, that PDO keeps the already processed rows in memory until the whole result set is processed.

I also abserved that, when my tool is finished with one table and proceeds to the next, memory consumption drops instantly, which supports my theory.

I’m NOT keeping the data in PHP variables! I hold just one single row at any given moment for processing, so that’s not the problem.

Now to the question: Is there a way to force PDO not to keep all the data in memory? I only process one row at a time, so there’s absolutely no need to keep all that garbage. I’d really like to use less memory on this thing.

  • 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-15T13:57:22+00:00Added an answer on May 15, 2026 at 1:57 pm

    I believe the problem comes from php’s garbage collector, as it does not garbage collect soon enough.
    I would try to fetch my results in chunks of row_count size, like "SELCT ... LIMIT offset, row_count" in MySQL, or "SELECT * FROM (SELECT ...) WHERE ROW_NUM BETWEEN offset AND (offset + row_count)" in ORACLE.
    Using Zend_Db_Select one can generate DB-independent queries:

    $select = $db->select()
        ->from(array('t' => 'table_name'),
            array('column_1', 'column_2'))
        ->limit($row_count, $offset);
    $select->__toString(); 
    # on MySQL renders: SELECT column_1, column_2 FROM table_name AS t LIMIT 10, 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please remember that I am not using any ORM tool. I have just written
I have a tool written to be added to the configuration file but I
I have written this Mootools example of a simple tool using canvas: http://jsfiddle.net/beingalex/MZTbW/11/ It
I have written a tool tip snippet, its throwing error like TOO MUCH RECURSION
I have written a tool to collect log files within a time window specified
I have a command line tool written in c# (that i have the source
I have a couple processes running a tool I've written that are joined by
I have written this code in JavaScript and works perfectly fine when I include
Suppose I have a big database, about 800 tables. In this database there is
I have a django project that uses a sqlite database that can be written

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.