Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLdb is caching SELECT results?

i'm running a SELECT query in a loop.

Once in a while, the database table is updated (by another program).

The first SELECT retrieves the correct data, but further calls in the loop return the first values.

How can I retrieve up-to-date data?

The only workaround I found so far, is reconnect to the DB on each iteration! In my example, uncommenting BOTH comment #1# and #2#. Uncommenting only #2# is not enough (i.e., cursor is recreated), the results are still cached.

Here's a working sample that gives the error.

import MySQLdb
from time import sleep

class DB:
    def __init__(self):
        self.connection = MySQLdb.connect(mysql_host, mysql_user, mysql_pass, mysql_db)
        self.cursor = self.connection.cursor()

    def get(self):
            sql = ''' SELECT id, message FROM mps_messages
                      WHERE topic=%s ORDER BY id LIMIT 1  '''
            #1# self.connect()
            #2# self.cursor = self.connection.cursor()
            self.cursor.execute(sql, ("topic",) )
            rec = self.cursor.fetchone()
            print rec

    def loop(self):
        while True:
            self.get()
            sleep(4)

db=DB()
db.loop()
  • OS: ubuntu,
  • python: 2.7.4
  • mysqldb: 1.2.3
  • mysql: 5.5.34
like image 319
Berry Tsakala Avatar asked May 13 '14 15:05

Berry Tsakala


People also ask

Does hibernate cache query results?

Hibernate also implements a cache for query resultsets that integrates closely with the second-level cache. This is an optional feature and requires two additional physical cache regions that hold the cached query results and the timestamps when a table was last updated.

Does MySQL use caching?

Query cache is a prominent MySQL feature that speeds up data retrieval from a database. It achieves this by storing MySQL SELECT statements together with the retrieved record set in memory, then if a client requests identical queries it can serve the data faster without executing commands again from the database.

How do I stop MySQL from caching?

Query to disable the cache From the MySQL command line, a application like phpMyAdmin, or from a script, run the following SQL command to disable the query cache: SET GLOBAL query_cache_size = 0; This will take effect immediately. You will also need to modify the MySQL configuration file to keep it disabled on reboot.


1 Answers

i had to add

connection.autocommit(True)

Adding SQL_NO_CACHE had no effect on the presented case, apparently because there was no caching involved.

I still don't understand why a SELECT needs COMMIT.

I'll open a new question about it.

like image 84
Berry Tsakala Avatar answered Oct 13 '22 14:10

Berry Tsakala