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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:05:19+00:00 2026-06-13T06:05:19+00:00

I am using Doctrine and MongoDB for an application and there is one task

  • 0

I am using Doctrine and MongoDB for an application and there is one task that should import data from an CSV file into a collection. There are about 5 different CSV files with at least 450.000 entries per file that should be important 1 to 3 times per year.

Currently I iterate through all lines of a file, create an object, call persist() and flush every 2.500 items.

Each item is not very large, it has an ID, a string that is 10-20 characters, a string that is 5-10 characters and a boolean value.

My first question is:
When I flush every 5.000 items inserting gets significantly slower. In my test environment flushing 5.000 items took about 102 seconds, flushing 2.500 items took about 10 seconds.

Flushing gets slower after a while. As mentioned, at the beginning flush 2.500 items took about 10 seconds, after 100.000 items, flushing 2.500 items takes nearly one minute.

Are there any optimisations I can do to speed things up?

  • 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-13T06:05:20+00:00Added an answer on June 13, 2026 at 6:05 am

    I think there are two parts that may be optimized in your script.

    • The way you read your CSV files, depending on how you load it, you either totally load it in memory (using file_get_contents() or file() for example), or reading it chunck by chunck with fopen(), fread().

    The last option is prefered because it will only take the necessary amount of memory when processing a bunch of lines.

    • You need to clear() the object you already processed, else it will be kept in memory until the end of your script. This means that if one DC2 object uses 1Mo of memory, and you have 100,000 objects, at the end of your scripts it will use 100,000Mo.
      So batching your insert by range of 2,500 is quite a good idea but you obviously need to remove the processed object from the EntityManager.

    It can be done using $entityManager->clear();

    The clear() will clear the whole EntityManager, you want to clear a single entity, you may use $entityManager->detach($object).


    If you want to profile your memory usage you may also be interested in the functions

    memory_get_usage()

    memory_get_peak_usage()

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

Sidebar

Related Questions

I have a Doctrine mongodb document that I have turned into a form. The
I'm using Doctrine MongoDB ODM in a Symfony2 application. How can I set default
I'm using Doctrine ODM with MongoDB. I have a product model like this: namespace
Using Doctrine 2 I want to get some users that are contacts of another
We are using Doctrine 2 for a new project and have run into an
I am using Doctrine and it fails to INSERT data the first time I
Using Symfony2 and Doctrine ODM with MongoDB, I need to access the parent document
I am using Doctrine 1.2, how could I get the query object into json
I'm using Doctrine 2 in a Zend Framework application and require functionality similar to
Is there any way, when using Doctrine, to create a table like another? I

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.