English 中文(简体)
利用MySQLdb在Adhur
原标题:Chronic stale results using MySQLdb in Python

我的沙尔方案询问了一家MySQL银行的一套表格,30秒钟的睡觉,然后再次问他们等等。 有关表格由第三方不断更新,(显然)我想每30秒看到新的结果。

让我问一下:

"select * from A where A.key > %d" % maxValueOfKeyFromLastQuery

定期 我将看到,我的方案停止在一次或两次迭接之后找到新的成果,尽管表格中出现了新的“are>>。 我知道在表格中出现了新的一行,因为当我发表互动的我的问询时,我可以看到这些新行。

我发现,如果我在每次问询后终止与数据库的联系,然后为下一个询问建立新的联系,那么这一问题就会消失。

我认为,这可能是一个服务器边的ach问题,在这里讨论:。 在方案的某些部分,明显可分辨的MySQL query cache

然而:

  1. When I check the interactive mysql shell, it says that caching is on. (So if this is a caching problem, how come the interactive shell doesn t suffer from it?)

  2. If I explicitly execute SET SESSION query_cache_type = OFF from within my Python program, the problem still occurs.

Creating a new DB connection for each query is the only way I ve been able to make the problem go away.

How can I get my queries from Python to see the new results that I know are there?

最佳回答

This website and this website contain information on the same problem. In order to keep your tables up to date, you must commit your transactions. Use db.commit() to do this.

As mentioned by the post below me, you can remove the need for this by enabling auto-commit. this can be done by running db.autocommit(True)

Also, auto-commit is enabled in the interactive shell, so this explains why you didn t have the problem there.

问题回答

您不妨检查贵国数据库的交易孤立程度。 你们描述的行为是,如果将行为定在REPEATABLE-READ,你会期望什么。 你不妨将其改为反恐执行局。

Since the original poster of the problem mentions that he is merely querying the database, it cannot be a commit that was forgotten. Inserting a commit seems to be a workaround though since it causes a new transaction to begin; and a new snapshot may need to be established. Still, having to insert a commit before every select doesn t sound like good programming practices to me.

There is no python code to show since the solution lies in correctly configuring the database.

<><>>> http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html。

REPEATABLE READ
This is the default isolation level for InnoDB. For consistent reads, there is an important difference from the READ COMMITTED isolation level: All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other. See Section 14.3.9.2, “Consistent Nonlocking Reads”.

READ COMMITTED
A somewhat Oracle-like isolation level with respect to consistent (nonlocking) reads: Each consistent read, even within the same transaction, sets and reads its own fresh snapshot. See Section 14.3.9.2, “Consistent Nonlocking Reads”.

Checking the configured isolation level:

>mysql > SELECT @@GLOBAL.tx_isolation, @@tx_isolation;
+-----------------------+-----------------+
| @@GLOBAL.tx_isolation | @@tx_isolation  |
+-----------------------+-----------------+
| REPEATABLE-READ       | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.01 sec)

Setting the transaction isolation level to READ-COMMITTED

mysql> SET GLOBAL tx_isolation= READ-COMMITTED ;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SESSION tx_isolation= READ-COMMITTED ;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @@GLOBAL.tx_isolation, @@tx_isolation;
+-----------------------+----------------+
| @@GLOBAL.tx_isolation | @@tx_isolation |
+-----------------------+----------------+
| READ-COMMITTED        | READ-COMMITTED |
+-----------------------+----------------+
1 row in set (0.01 sec)

mysql>

并再次提出申请......

页: 1 如下:

conn = MySQLdb.Connect("host", "user", "password")
conn.autocommit(True)

这使你在互动赛事中使用同样的行为。

我不能确切地说,为什么出现这一问题,但我已经测试了3个解决办法,以解决这个问题。

  1. 在进行每次盘问之前,建立新的我方(和新方)连接

    cnx = mysql.connector.connect(**config2)
    csr = cnx.cursor(named_tuple=True, buffered=True)
    

我确信,这不是建议的做法,而是解决问题。

  1. Commit - either by setting autocommit to true or by manually committing after each query
  2. Understand that the problem only occurs with transaction storage engines, so if it is not important to use transactional, you can set the table you are querying to a non transactional type like MyISAM - probably the easiest option

我没有给READ COMMITED增加交易孤立程度,因为我没有审判过,有人声称自己没有工作。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...