Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, in most cases you do not have to be concerned about the performance of sqlite3, if you are not using it completely wrong.</p> <p>The following things boost the performance of <code>INSERT</code> statements:</p> <p><strong>Transactions</strong></p> <p>As you already mentioned, transactions are the most important feature. Especially if you have a large amount of queries, transaction will <strong>speed up your <code>INSERT</code>s by ~10 times</strong>.</p> <p><strong>PRAGMA Config</strong></p> <p>Sqlite3 provides several mechanism which avoid the corruption of your database in the worst cases. In some scenarios, this is not needed. In others, it is absolutely essential. The following sqlite3 commands may speed up your <code>INSERT</code> statements. A normal crash of your app will not corrupt the database, but a crash of the OS could.</p> <pre><code>PRAGMA synchronous=OFF -- may cause corruption if the OS fails PRAGMA journal_mode=MEMORY -- Insert statements will be written to the disk at the end of the transaction PRAGMA cache_size = 4000 -- If your SELECTs are really big, you may need to increase the cache PRAGMA temp_store=MEMORY -- Attention: increases RAM use </code></pre> <p><strong>Deactivate Indicies</strong></p> <p>Any SQL Index slows a <code>INSERT</code> statement down. Check if your table has some indices:</p> <pre><code>.indices &lt;table_name&gt; </code></pre> <p>If yes, DROP the <code>INDEX</code> and <code>CREATE</code> it after the transaction.</p> <p><strong>One Select</strong></p> <p>I do not see a way of using a BULK insert as you are generating new data. However, you could collect data and just perform one <code>INSERT</code> statement. This may boost up your performance dramatically, but it also rises the possibility of failure (syntax, for instance). One hack meets another hack: As sqlite3 does not support this directly, you have to use the UNION command to collect all insert statements accordingly.</p> <pre><code>INSERT INTO 'tablename'       SELECT 'data1' AS 'column1', 'data2' AS 'column2' UNION SELECT 'data3', 'data4' UNION SELECT 'data5', 'data6' UNION SELECT 'data7', 'data8' </code></pre> <p><strong>Statement Caching</strong></p> <p>I would suggest to avoid the use of statement caching as there is a <a href="https://github.com/ccgus/fmdb/issues/6">unfixed issue</a> with this feature (and far as I know, it does not influence the performance dramatically). </p> <p><strong>Memory Management</strong></p> <p>The last point I'd like to mention is about ObjC. Compared to basic operations, memory management needs very very much time. Maybe you could avoid some <code>stringWithFormat:</code> or <code>numberWithDouble:</code> by preparing these variable outside the loop.</p> <p><strong>Summary</strong></p> <p>All in all, I don't think that you will have a problem with the speed of sqlite3 if you simply use <strong>transactions</strong>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload