[使用Python3.x]
基本思想是我必须运行第一个查询来提取一长串ID(文本)(大约一百万个ID),并在另一个查询的WHERE语句中的IN()子句中使用这些ID。我使用python字符串格式化来实现这一点,并且如果ID的数量很小 - 比如说100k,那么效果很好 - 但是当该集合确实大约有一百万个ID时,会给我一个错误(pyodbc.Error: ('08S01', '[08S01] [MySQL][ODBC 5.2(a) Driver][mysqld-5.5.31-MariaDB-log]MySQL server has gone away (2006) (SQLExecDirectW)')
)
我试着稍微阅读它,并认为它可能与SQLite设置的默认(?)limits有关。另外,我想知道我是否以正确的方式接近这一点。
这是我的代码:
def get_device_ids(con_str, query, tb_name):
local_con = lite.connect('temp.db')
local_cur = local_con.cursor()
local_cur.execute("DROP TABLE IF EXISTS {};".format(tb_name))
local_cur.execute("CREATE TABLE {} (id TEXT PRIMARY KEY, \
lang TEXT, first_date DATETIME);".format(tb_name))
data = create_external_con(con_str, query)
device_id_set = set()
with local_con:
for row in data:
device_id_set.update([row[0]])
local_cur.execute("INSERT INTO srv(id, lang, \
first_date) VALUES (?,?,?);", (row))
lid = local_cur.lastrowid
print("Number of rows inserted into SRV: {}".format(lid))
return device_id_set
def gen_queries(ids):
ids_list = str(', '.join("'" + id_ +"'" for id_ in ids))
query = """
SELECT e.id,
e.field2,
e.field3
FROM table e
WHERE e.id IN ({})
""".format(ids_list)
return query
def get_data(con_str, query, tb_name):
local_con = lite.connect('temp.db')
local_cur = local_con.cursor()
local_cur.execute("DROP TABLE IF EXISTS {};".format(tb_name))
local_cur.execute("CREATE TABLE {} (id TEXT, field1 INTEGER, \
field2 TEXT, field3 TEXT, field4 INTEGER, \
PRIMARY KEY(id, field1));".format(tb_name))
data = create_external_con(con_str, query) # <== THIS IS WHERE THAT QUERY IS INSERTED
device_id_set = set()
with local_con:
for row in data:
device_id_set.update(row[1])
local_cur.execute("INSERT INTO table2(id, field1, field2, field3, \
field4) VALUES (?,?,?,?,?);", (row))
lid = local_cur.lastrowid
print("Number of rows inserted into table2: {}".format(lid))
非常感谢任何帮助!
This可能是解决我问题的正确方法,但是当我尝试使用"SET SESSION max_allowed_packet=104857600"
时,我收到错误:SESSION variable 'max_allowed_packet' is read-only. Use SET GLOBAL to assign the value (1621)
。然后,当我尝试将SESSION更改为GLOBAL时,我会收到拒绝访问的消息。
答案 0 :(得分:0)
将ID插入同一数据库中的(临时)表,然后使用:
... WHERE e.ID IN (SELECT ID FROM TempTable)