原文地址:
https://blog.devart.com/increasing-sqlite-performance.html
One the major issues a developer encounters when using the SQLite DBMS in his applications is its performance issue.
Perhaps, a classic case everyone gets into when using SQLite for the first time is very slow execution of multiple INSERT/UPDATE/DELETE operations. Indeed, sequential executions of not even thousands, but hundreds of INSERTs into a table may take too long.The origin of the issue lies in the specificity of using transactions in SQLite. SQLite starts a transaction automatically every time before any DML statement execution and commits it after execution. Accordingly, when executing multiple consequent statements, a new transaction will be started and committed for each statement.
The solution of this problem is quite simple — the block of DML statements may be enclosed into BEGIN … END operators block ( https://www.sqlite.org/lang_transaction.html ). In this case, each DML statement won’t be executed in a separate transaction, but a single transaction will be started before the whole block execution and committed after all modifications.
Such an approach increases SQLite data modification performance by times. See more details about it in the SQLite documentation (https://www.sqlite.org/faq.html#q19).
However, this approach is not the only way to increase performance in SQLite. Parameters of the DBMS may also be configured using so-called PRAGMA (https://www.sqlite.org/pragma.html). The fact is that SQLite parameters are oriented not to high performance by default, but to maximum data safety and integrity. Modification of these parameters may increase performance, however, note, that the data corruption risks increase too.
Let’s analyze the impact to inserts performance by different PRAGMAs using LiteDAC.
We will use a test table SPEED_TEST in our project:
In each test, we will delete the database and re-create it, and then insert 10,000 records to the SPEED_TEST table as follows:
We’ll run the test project on 2 platforms: Microsoft Windows 7 x86 and MacOS X 10.9 Mavericks.
This parameter allows to specify location of temporary objects in the database: tables, indexes, triggers, views, etc. PRAGMA TEMP_STORE accepts 3 values:
When the TEMP_STORE parameter is changed, all the temporary tables, indexes, triggers, views are deleted.
Time: sec
Microsoft Windows 7 x86 | ||
---|---|---|
DEFAULT | FILE | MEMORY |
235 | 225 | 215 |
MacOS X 10.9 Mavericks | ||
---|---|---|
DEFAULT | FILE | MEMORY |
34 | 33 | 32 |
According to the retrieved results, making RAM a storage for temporary DB objects increases performance a little.
The parameter sets the database log working mode (rollback journal file used on transaction processing).
PRAGMA JOURNAL_MODE accepts the following values:
Time: sec
Microsoft Windows 7 x86 | ||||
---|---|---|---|---|
DELETE | TRUNCATE | PERSIST | MEMORY | OFF |
235 | 210 | 220 | 65 | 63 |
MacOS X 10.9 Mavericks | ||||
---|---|---|---|---|
DELETE | TRUNCATE | PERSIST | MEMORY | OFF |
34 | 4 | 3 | 2 | 1 |
Changing this parameter significantly increases performance when inserting data on both platforms. Note, that at using MEMORY or OFF values, the risk of data loss is maximal too.
Defines the mode of rollback journal synchronization with the data.
Time: sec
Microsoft Windows 7 x86 | ||
---|---|---|
FULL | NORMAL | OFF |
235 | 175 | 43 |
MacOS X 10.9 Mavericks | ||
---|---|---|
FULL | NORMAL | OFF |
34 | 32 | 32 |
The test demonstrated significant performance increase on Windows. It is highly recommended to speed up performance this way only with assurance of the operating system stability and power quality.
Defines the locking mode.
Time: sec
Microsoft Windows 7 x86 | |
---|---|
NORMAL | EXCLUSIVE |
235 | 155 |
MacOS X 10.9 Mavericks | |
---|---|
NORMAL | EXCLUSIVE |
34 | 3 |
The most secure approach to increase performance. Though, EXCLUSIVE MODE allows the database to serve only one connection.
Defines the number of pages from the database file for storing in RAM, i.e., the cache size. Increasing this parameter may increase performance of the database on high load, since the greater its value is, the more modifications a session can perform before retrieving exclusive lock.
Time: sec
Microsoft Windows 7 x86 | ||||||
---|---|---|---|---|---|---|
0 | 500 | 1000 | 2000 | 4000 | 8000 | 10000 |
222 | 185 | 230 | 230 | 230 | 230 | 250 |
MacOS X 10.9 Mavericks | ||||||
---|---|---|---|---|---|---|
0 | 500 | 1000 | 2000 | 4000 | 8000 | 10000 |
– | 34 | 34 | 34 | 34 | 34 | 34 |
Performance increase is observed only on Windows. Changing this parameter is almost secure, yet the performance growth is minor.
Defines the database page size.
The page size is set by default depending on some computer and OS specifications. They include disk sector size and the used encoding. SQLite supports page size range from 512 to 65536 bytes.
Time: sec
Windows 7 x86 | |||||||
---|---|---|---|---|---|---|---|
512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | 65535 |
240 | 235 | 227 | 225 | 255 | 295 | 450 | 295 |
MacOS X 10.9 Mavericks | |||||||
---|---|---|---|---|---|---|---|
512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | 65535 |
34 | 34 | 33 | 33 | 34 | 40 | 50 | 65 |
Like the previous parameter, PRAGMA PAGE_SIZE is almost safe with respect to risk of data corruption, however, tuning of this parameter doesn’t lead to significant performance increase and requires search for values depending on the used sector size.
原文:https://www.cnblogs.com/xalion/p/10990352.html