Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I re-use cursor object or create a new one with mysql.connector?

Should I reuse the cursor object or create a new one for each query?

Reusing the cursor:

    # we're going to connect to s3 and mysql
    db = mysql_connect(host="localhost",
                       user="user",
                       passwd="pass",
                       database="db")

    # Reusing the cursor
    cursor = db.cursor()

    # loop through all the files in the bucket one by one
    for key in bucket.get_all_keys():

        # the document id is the filename in the s3 bucket
        doc_id = key.name.split("/")[-1]
        cursor.execute("SELECT document_name FROM laws_docs WHERE id = %i", (doc_id,))
        doc_name = cursor.fetchone()[0]

    cursor.close()

- or -

New cursor every time:

    # we're going to connect to s3 and mysql
    db =  mysql_connect(host="localhost",
                       user="user",
                       passwd="pass",
                       database="db")

    # loop through all the files in the bucket one by one
    for key in bucket.get_all_keys():

        # new cursor
        cursor = db.cursor()

        # the document id is the filename in the s3 bucket
        doc_id = key.name.split("/")[-1]
        cursor.execute("SELECT document_name FROM laws_docs WHERE id = %i", (doc_id,))
        doc_name = cursor.fetchone()[0]

         ursor.close()

Does it even matter? That loop is going to run at least 50,000 times.

like image 872
mevers303 Avatar asked May 04 '19 23:05

mevers303


People also ask

What is the purpose of cursor object in MySQL?

The MySQLCursor class instantiates objects that can execute operations such as SQL statements. Cursor objects interact with the MySQL server using a MySQLConnection object.

What is the purpose of cursor object?

It is an object that is used to make the connection for executing SQL queries. It acts as middleware between SQLite database connection and SQL query. It is created after giving connection to SQLite database.

What is the difference between a connection and a cursor?

Connection object is your connection to the database, close that when you're done talking to the database all together. Cursor object is an iterator over a result set from a query. Close those when you're done with that result set. John Gaines Jr.

What is difference between MySQL and mysql connector?

mysqlclient is a forked version of MySQLdb with python3. 3+ support, and mysql connector is official module from mysql.


1 Answers

If you're using MySQL Connector/Python, cursor = db.cursor() will create a new CMySQLCursor instance (or MySQLCursor if you're using the pure Python version). Thus for your second example, you will create 50,000 cursor instances.

You only need one cursor. Open and close the cursor outside the for loop (use your first example).

like image 192
Nuno Mariz Avatar answered Sep 28 '22 02:09

Nuno Mariz