【问题标题】:Entering large amount of entries in a SQLite Database在 SQLite 数据库中输入大量条目
【发布时间】:2012-01-25 22:46:59
【问题描述】:

我使用以下 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 个条目,这意味着添加这些值需要半天时间。有没有我应该考虑的更好/更快的方法来做到这一点?

感谢您的帮助,

巴里

【问题讨论】:

    标签: python sqlite python-3.x


    【解决方案1】:

    您可以在开头将整个'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()
    

    【讨论】:

    • @Baz - 你也可以在插入时尝试executemany 方法:docs.python.org/library/sqlite3.html#sqlite3.Cursor.executemany
    • 是的,尤其是在我将新条目的插入和现有条目的更新分成单独的python函数之后,分别建立了需要更新或添加的内容。
    • 是的,我确实使用 executemany 添加和更新。我使用 set().difference 来获取 new_people。工作速度比我以前快得多!
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2013-06-03
    • 2011-05-09
    • 2013-04-17
    相关资源
    最近更新 更多