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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:35:43+00:00 2026-06-12T19:35:43+00:00

[Python/MySQLdb] – CentOS – Linux – VPS I have a page that parses a

  • 0

[Python/MySQLdb] – CentOS – Linux – VPS

I have a page that parses a large file and queries the datase up to 100 times for each run. The database is pretty large and I’m trying to reduce the execution time of this script.

My SQL functions are inside a class, currently the connection object is a class variable created when the class is instantiated. I have various fetch and query functions that create a cursor from the connection object every time they are called. Would it be faster to create the cursor when the connection object is created and reuse it or would it be better practice to create the cursor every time it’s called?

import MySQLdb as mdb
class parse:
    con = mdb.connect( server, username, password, dbname )
    #cur = con.cursor() ## create here?
    def q( self, q ):
        cur = self.con.cursor() ## it's currently here
        cur.execute( q )

Any other suggestions on how to speed up the script are welcome too. The insert statement is the same for all the queries in the script.

  • 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-12T19:35:44+00:00Added an answer on June 12, 2026 at 7:35 pm

    Opening and closing connections is never free, it always wastes some amount of performance.

    The reason you wouldn’t want to just leave the connection open is that if two requests were to come in at the same time the second request would have to wait till the first request had finished before it could do any work.

    One way to solve this is to use connection pooling. You create a bunch of open connections and then reuse them. Every time you need to do a query you check a connection out of the pool, preform the request and then put it back into the pool.

    Setting all this up can be quite tedious, so I would recommend using SQLAlchemy. It has built in connection pooling, relatively low overhead and supports MySQL.

    Since you care about speed I would only use the core part of SQLAlchemy since the ORM part comes is a bit slower.

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

Sidebar

Related Questions

So I have a file that looks like so: #!/usr/bin/python import MySQLdb import subprocess
I'm trying to enforce a time limit on queries in python MySQLDB. I have
i have to install mysqldb module of python in my centos server. i have
Python 3 really complicated the whole file reading process, when you have a binary
I would like to get Python's documentation for MySQLdb in Man -format such that
How can execute sql script stored in *.sql file using MySQLdb python driver. I
I am using the MySQLdb in Python. I was told that to properly create
I'm using Python and MySQLdb to add rows to my database. It seems that
I installed python-mysqldb using sudo apt-get install python-mysqldb I have tried to use it
I have a problem with MySQL and Python's MySQLdb when I try to INSERT

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.