在SQLite数据库中输入大量条目

时间:2011-12-22 08:54:27

标签: python sqlite python-3.x

我使用以下python3代码在sqlite3数据库中添加和更新条目:

def increment_person_counts(count_per_person):
   with sqlite3.connect(r'./people_database') as connection:
      cursor = connection.cursor()
      for person in count_per_person:
         if cursor.execute('select * from personCounts where person = ?', [person]).fetchone()==None:
            cursor.execute('insert into personCounts(person, count) values (?, ?)', [person, count_per_person[person]])
         else:
            cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count_per_person[person], person])
      connection.commit()

count_per_person包含400万个条目,我似乎能够每秒添加/更新大约100个条目,这意味着添加这些值需要半天时间。我应该考虑采用更好/更快的方法来做到这一点吗?

感谢您的帮助,

巴里

1 个答案:

答案 0 :(得分:2)

您可以在开头将整个'select * from personCounts'读入python set(),然后根据此设置进行检查。

def increment_person_counts(count_per_person):
   with sqlite3.connect(r'./people_database') as connection:
      cursor = connection.cursor()
      cursor.execute('select person from personCounts')
      known_persons = set(row[0] for row in cursor.fetchall())
      for person, count in count_per_person.iteritems():
         if person in known_persons:
            cursor.execute('insert into personCounts(person, count) values (?, ?)', [person, count])
         else:
            cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count, person])
      connection.commit()

更新:在我的评论之后,这是executemany的更新:

def increment_person_counts(count_per_person):
    with sqlite3.connect(r'./people_database') as connection:
        cursor = connection.cursor()
        cursor.execute('select person from personCounts')
        known_persons = set(row[0] for row in cursor.fetchall())
        cursor.executemany('insert into personCounts(person, count) values (?, ?)', ((person, count) for count_per_person.iteritems() if person in known_persons))
        for person, count in count_per_person.iteritems():
            if person not in known_persons:
                cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count, person])
        connection.commit()