【 TiDB 使用环境】
集群版本:5.7.25-TiDB-v5.3.1
PyMySQL==0.9.3
【概述】 场景 + 问题概述
1.pymysql访问tidb分区表首次执行select * from t limit 1 无返回数据,访问相同表结构的非分区表select * from t2 limit 1 有返回数据;
2.在MySQL 5.7.26执行都没问题,都可以正常读取数据
【应用框架及开发适配业务逻辑】
查询分区表数据,SQL为最简单的方式:select * from t limit 1
【背景】 做过哪些操作
【现象】 业务和数据库现象
【问题】 当前遇到的问题
- 两个表结构,t1为分区表,t2为相同表结构的非分区表
CREATE TABLEt
(
id
varchar(255) NOT NULL ,
logdt
timestamp DEFAULT ‘1970-01-02 00:00:00’,
name
varchar(255) NOT NULL ,
newdate
datetime DEFAULT CURRENT_TIMESTAMP ,
updatetime
datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
UNIQUE KEYid
(id
,logdt
),
KEYnewdate_gid_dt
(newdate
,logdt
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T! SHARD_ROW_ID_BITS=4 PRE_SPLIT_REGIONS=3 */
PARTITION BY RANGE ( UNIX_TIMESTAMP(logdt
) ) (
PARTITIONp20220520
VALUES LESS THAN (1653062400),
PARTITIONp20220521
VALUES LESS THAN (1653148800),
PARTITIONp20220522
VALUES LESS THAN (1653235200),
PARTITIONp20220523
VALUES LESS THAN (1653321600)
);
CREATE TABLE t2
(
id
varchar(255) NOT NULL ,
logdt
timestamp DEFAULT ‘1970-01-02 00:00:00’,
name
varchar(255) NOT NULL ,
newdate
datetime DEFAULT CURRENT_TIMESTAMP ,
updatetime
datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
UNIQUE KEY id
(id
,logdt
),
KEY newdate_gid_dt
(newdate
,logdt
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T! SHARD_ROW_ID_BITS=4 PRE_SPLIT_REGIONS=3 */;
2.两个表都有数据,如下
mysql> select *from t;
±----±--------------------±-----±--------------------±--------------------+
| id | logdt | name | newdate | updatetime |
±----±--------------------±-----±--------------------±--------------------+
| id1 | 2022-05-20 18:00:00 | tidb | 2022-05-24 18:00:44 | 2022-05-24 18:00:44 |
| id1 | 2022-05-21 18:00:00 | tidb | 2022-05-24 18:00:49 | 2022-05-24 18:00:49 |
±----±--------------------±-----±--------------------±--------------------+
2 rows in set (0.01 sec)
mysql> select *from t2;
±----±--------------------±-----±--------------------±--------------------+
| id | logdt | name | newdate | updatetime |
±----±--------------------±-----±--------------------±--------------------+
| id1 | 2022-05-21 18:00:00 | tidb | 2022-05-24 18:02:38 | 2022-05-24 18:02:38 |
±----±--------------------±-----±--------------------±--------------------+
1 row in set (0.00 sec)
3.pymysql第一次查询分区表,无法返回数据;第二次查询有数据,第三次查也有数据;
1)执行操作1:查分区表无数据返回,查非分区表有数据
import pymysql
conn = pymysql.connect(**{
‘db’: ‘test’,
‘database’: ‘test’,
‘user’: ‘xxx’,
‘password’: ‘xxx’,
‘host’: ‘xxx’,
‘port’: 4000,
‘cursorclass’: pymysql.cursors.DictCursor
})
#分区表无返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
#非分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t2 limit 1”)
print(cur.fetchall())
执行python a.py输出结果:
()
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’logdt’: datetime.datetime(2022, 5, 21, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
2)执行操作2:调整查询表的顺序,这一次查询分区表和非分区表都有数据返回
import pymysql
conn = pymysql.connect(**{
‘db’: ‘test’,
‘database’: ‘test’,
‘user’: ‘xxx’,
‘password’: ‘xxx’,
‘host’: ‘xxx’,
‘port’: 4000,
‘cursorclass’: pymysql.cursors.DictCursor
})
#非分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t2 limit 1”)
print(cur.fetchall())
#分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
执行python a.py输出结果:
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’logdt’: datetime.datetime(2022, 5, 21, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 0, 44), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 0, 44), u’logdt’: datetime.datetime(2022, 5, 20, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
3)执行操作3:pymysql第一次查询分区表,无法返回数据;第二次查询有数据
import pymysql
conn = pymysql.connect(**{
‘db’: ‘test’,
‘database’: ‘test’,
‘user’: ‘xxx’,
‘password’: ‘xxx’,
‘host’: ‘xxx’,
‘port’: 4000,
‘cursorclass’: pymysql.cursors.DictCursor
})
#分区表无返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
#分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
#非分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t2 limit 1”)
print(cur.fetchall())
执行python a.py输出结果:
()
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 0, 49), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 0, 49), u’logdt’: datetime.datetime(2022, 5, 21, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’logdt’: datetime.datetime(2022, 5, 21, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
4)执行操作4:pymysql第一次查询分区表,无法返回数据;第二次查询有数据;第三次查询有数据
import pymysql
conn = pymysql.connect(**{
‘db’: ‘test’,
‘database’: ‘test’,
‘user’: ‘xxx’,
‘password’: ‘xxx’,
‘host’: ‘xxx’,
‘port’: 4000,
‘cursorclass’: pymysql.cursors.DictCursor
})
#分区表无返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
#分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
#分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t limit 1”)
print(cur.fetchall())
#非分区表有返回数据
cur = conn.cursor()
cur.execute(“select * from t2 limit 1”)
print(cur.fetchall())
执行python a.py输出结果:
()
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 0, 49), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 0, 49), u’logdt’: datetime.datetime(2022, 5, 21, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 0, 44), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 0, 44), u’logdt’: datetime.datetime(2022, 5, 20, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
[{u’newdate’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’updatetime’: datetime.datetime(2022, 5, 24, 18, 2, 38), u’logdt’: datetime.datetime(2022, 5, 21, 18, 0), u’id’: ‘id1’, u’name’: ‘tidb’}]
【业务影响】
无法读取分区表数据
【TiDB 版本】
集群版本:5.7.25-TiDB-v5.3.1