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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:54:32+00:00 2026-06-15T13:54:32+00:00

I’m trying to import some XML data into my MySQL database after processing it

  • 0

I’m trying to import some XML data into my MySQL database after processing it with Python. To keep things simple, I’m doing it all from one script that uses SQLAlchemy to access my database.

The XML file has about 80,000 entries and I process it using xml.etree.cElementTree‘s iterparse method and delete nodes after I use them to keep my memory usage at around 20mb.

Once I include SQLAlchemy and start adding things into the database, my memory usage rises by about 10mb per second until the script exhausts all of my memory and the OS kills it.

Here’s basically what my code looks like:

index = 0

for element in iterate_xml():
    ...

    index += 1

    session.add(Model(**data))

    if index % 1000 == 0:
        session.flush()
        session.commit()

I’m not sure what else to try. A periodic .flush() and .commit() do help a little bit, but they don’t fix the problem.

Is SQLAlchemy not the right tool for this task?


I setup SQLAlchemy like this:

Base = declarative_base()
engine = create_engine(config.SQLALCHEMY_DATABASE_URI, echo=False)

Session = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
session = Session()

And my table looks like this:

columns = []

for name, datatype in structure.iteritems():
    if isinstance(datatype, int):
        datatype = String(datatype or 20)

    column = Column(name, datatype)
    columns.append(column)

metadata = MetaData(bind=engine)
table = Table('table_name', metadata,
    Column('id', Integer, primary_key=True),
    *columns
)

metadata.drop_all(engine)
metadata.create_all(engine)

class MyTable(Base):
    __tablename__ = 'table_name'
    __table_args__ = {
        'autoload': True,
        'autoload_with': engine
    }

structure is a dictionary that maps column names to data types (it’s generated from the XML):

structure = {
    'column_name': SQLAlchemyDataType,
    ...
}
  • 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-15T13:54:33+00:00Added an answer on June 15, 2026 at 1:54 pm

    Here’s a SQLAlchemy-only version of your code. Testing in 0.7 and 0.8, it doesn’t leak any memory, which is not a surprise to me because we have over a dozen tests under continuous integration to ensure nothing leaks under many scenarios. So first step is to confirm this script doesn’t leak for you, then try to figure out what changes between this script and yours to produce a test case that actually shows the leaking memory.

    from sqlalchemy import Column, String, Integer, create_engine
    from sqlalchemy.orm import Session
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class Model(Base):
        __tablename__ = "a"
    
        id = Column(Integer, primary_key=True)
        data = Column(String)
    
    e = create_engine("sqlite:///somefile.db")
    
    Base.metadata.create_all(e)
    
    session = Session(e)
    
    for index in xrange(10000000):
        session.add(Model(data="data %d" % index))
    
        if index % 1000 == 0:
            print "flushing... %d" % index
            session.flush()
            session.commit()
    

    It’s important to note of course, those issues where SQLAlchemy has leaked memory in the past. Here’s a recent history of leaks fixed:

    0.7.8 – the most recent. The leak fixed here only occurred when using: 1. the C extensions, 2. the pyodbc driver, during certain result fetch operations (not all of them)

    0.6.6 – the “Decimal” result processor in the C extensions had a leak.

    0.6.6 – the SQLSoup extension was identified as having a potential leak if used to select rows in certain ways (SQLSoup is now it’s own project)

    0.5.5 – fixed potential memory leak when objects would be unpickled and placed back into a Session

    0.5.4 – major improvements to the Session’s memory usage were made. You definitely want to be well past this version.

    • 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
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a

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.