数据库 SQlite3 python3 上的输入速度较慢



我正在尝试使用python在本地sqlite3 db上保存用户名。我真的是新人。我的表有 10000 行和一列用户名,但插入所有 10k 值需要半个多小时。我做错了什么?有什么线索吗?我的代码

def create_table(channel_username):
c.execute("CREATE TABLE IF NOT EXISTS {}(user_name UNIQUE)".format(channel_username))
conn.commit()

def insert_username(channel_username,user_name):
c.execute("INSERT OR IGNORE INTO {} VALUES(?)".format(channel_username),[user_name])
conn.commit()

create_table(channel_username)
x= client.get_participants(channel_username, aggressive=True)
z=[]
for user in x:
z.append(user.username)
fl = [i for i in z if i is not None]
fl.sort()
for user_name in fl:
insert_username(channel_username,user_name)
print("DBfached successful")

插入不慢;所有commit()调用都很慢。

删除它们,并仅在实际完成后执行commit()

最新更新