Python MySQL使用列表选择位置
我有以下Python MySQL代码Python MySQL使用列表选择位置,python,mysql,performance,select,coding-efficiency,Python,Mysql,Performance,Select,Coding Efficiency,我有以下Python MySQL代码 cursor = mydb.cursor() cursor.execute('SELECT id FROM table1 WHERE col1=%s AND col2=%s', (val1, val2)) ids = cursor.fetchall() for id in ids: cursor.execute('SELECT record_key FROM table2 WHERE id=%s limit 1', (id[0], ))
cursor = mydb.cursor()
cursor.execute('SELECT id FROM table1 WHERE col1=%s AND col2=%s', (val1, val2))
ids = cursor.fetchall()
for id in ids:
cursor.execute('SELECT record_key FROM table2 WHERE id=%s limit 1', (id[0], ))
record_keys = cursor.fetchall()
print(record_keys[0][0])
我怎样才能提高效率?我使用的是5.5.60-MariaDB和Python 2.7.5。我在表1中有大约3.5亿个条目,在表2中有1500万个条目。幸运的是,您可以使用左连接在单个查询中完成这项工作 cursor=mydb.cursor cursor.execute 从表1 t1中选择t1.id,t2.record_键 左连接表2 t1.id=t2.id上的t2 其中t1.col1=%s和t2.col2=%s, 瓦尔1,瓦尔2, 对于id,在cursor.fetchall中记录_键: 通过做某事。。。 游标。执行“从表2中选择记录\u键,其中id在%s限制1中”,id[0],?可能重复