Efficient way to ensure unique rows in SQLite3

后端 未结 5 1556
情话喂你
情话喂你 2021-01-31 04:20

I am using SQLite3 in one of my projects and I need to ensure that the rows that are inserted into a table are unique with regard to a combination of some of their columns. In m

5条回答
  •  甜味超标
    2021-01-31 04:39

    I have used sqlite to insert millions of rows at runtime and this is what I have used to increase performance:

    • Use as few transactions as possible.
    • Use parametrized commands for inserting the data (prepare the command once and just change paramater values in the loop)
    • Set PRAGMA synchronous OFF (not sure how it works with WAL)
    • Increase page size of the database.
    • Increase cache size. This is an important setting as it will cause sqlite to actually write the data to the disk fewer times and will run more operations in memory making the whole process faster.
    • If you need an index add it after inserting the rows by running the necessary sqlite command. In this case you will need to ensure uniqueness yourself as you are currently doing it now.

    If you try these please post your test results. I believe it will be interesting for everyone.

提交回复
热议问题