---
title: "💭 Optimizing SQLite for servers"
description: "!https://kerkour.com/sqlite-for-servers"
date: 2024-04-01
published: true
tags:
  - sql
  - sqlite
  - thought
template: link
---


<div class="embed-card embed-card-external">
  <a href="https://kerkour.com/sqlite-for-servers" class="embed-card-link" target="_blank" rel="noopener noreferrer">
    <div class="embed-card-image">
      <img src="https://kerkour.com/icon-256.png" alt="Optimizing SQLite for servers — SQLite is often misconceived as a &amp;#34;toy database&amp;#34;, only good for mobile applications and embedded systems because it&amp;#39;s default configuration is optimized for embedded use cases, so most people trying it will encounter poor performances and the dreaded SQLITE_BUSY error. But what if I told you that by tuning a" loading="lazy">
    </div>
    <div class="embed-card-content">
      <div class="embed-card-title">Optimizing SQLite for servers</div>
      <div class="embed-card-description">SQLite is often misconceived as a &amp;#34;toy database&amp;#34;, only good for mobile applications and embedded systems because it&amp;#39;s default configuration is optimized for embedded use cases, so most ...</div>
      <div class="embed-card-meta">Sylvain Kerkour &middot; kerkour.com</div>
    </div>
  </a>
</div>


Very interesting article by Sylvain, suggested by Simon Willison.

Definitely some things that I want to come back and try later on.


Here is the TLDR of the whole post

``` bash
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 5000;
PRAGMA synchronous = NORMAL;
PRAGMA cache_size = 1000000000;
PRAGMA foreign_keys = true;
PRAGMA temp_store = memory;
```

This is interesting, and something I need to consider.  I definitely have an application with slow count queries.  I am not sure how to make it better as its not a full `count(*)` so a count table doesn't work, nor does counting by index.

I might need to have a table of cached results, and if a write matches the counter increase it, or update all counters on write.

> COUNT queries are slow
> SQLite doesn't keep statistics about its indexes, unlike PostgreSQL, so COUNT queries are slow, even when using a WHERE clause on an indexed field: SQLite has to scan for all the matching records.

> One solution is to use a trigger on INSERT and DELETE that updates a running count in a separate table then query that separate table to find the latest count.

!!! note

    This post is a <a href="/thoughts/" class="wikilink" data-title="Thoughts" data-description="These are generally my thoughts on a web page or some sort of url, except a rare few don&#39;t have a link. These are dual published off of my..." data-date="2024-04-01">thought</a>. It's a short note that I make
    about someone else's content online <a href="/tags/thoughts/" class="hashtag-tag" data-tag="thoughts" data-count=2 data-reading-time=3 data-reading-time-text="3 minutes">#thoughts</a>
