直连MySQL的情况下,如果Client带了`CLIENT_FOUND_ROWS`标志位,影响行数返回是查询的行数,就算记录没有更新也会有影响行数。
官方文档:
* For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT\_FOUND\_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.
* For REPLACE statements, the affected-rows value is 2 if the new row replaced an old row, because in this case, one row was inserted after the duplicate was deleted.
* For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT\_FOUND\_ROWS flag, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
使用proxysql后,有几点需要注意,不同版本对CLIENT_FOUND_ROWS的标志位的设置不一样。
proxysql有一个`mysql-client_found_rows`配置,具体描述如下:
mysql-client_found_rows
When set to true, client flag CLIENT_FOUND_ROWS is set when connecting to MySQL backends.
* 在1.4.15中,当`mysql-client_found_rows`为true还是false时,无论客户端有没有带CLIENT\_FOUND\_ROWS,proxysql到后端的mysql都会带上这个标志。
* 在1.4.15中,当`mysql-client_found_rows`为false时,无论客户端有没有带`CLIENT_FOUND_ROWS`,proxysql到后端的mysql都不会带上这个标志。
* 在2.0.15中,无论`mysql-client_found_rows`为true还是false时,以客户端是否带了CLIENT\_FOUND\_ROWS的标志为准。
测试脚本如下:(因为`mysql-client_found_rows`只会影响新创建的连接,所以测试的时候尽量重启下proxysql)
#cat test.py
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function
import sys
#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))
import mysql.connector
from mysql.connector.constants import ClientFlag
config = {
'user': 'sbtest_rw',
'password': 'sbtest_rw_123',
'host': '10.20.131.3',
'port': 6033,
'client_flags': [ClientFlag.FOUND_ROWS]
}
cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("update sbtest.sb set pad='sxxxx' where id = 1;")
cnx.commit()
print(cur.rowcount, " modified")
#cur.execute("select ROW_COUNT();")
#print(cur.fetchone())
cur.close()
cnx.close()
通过调client\_flags参数,确定是否带client\_found\_rows标志,再观察cur.rowcount返回值。
文章最后更新时间:
2021年04月20日 19:17:49