9 January, 2025

SQLLite too slow?
By: Matthew Jackson

Perhaps you're like me and saw your massive table slow down alot....

Here is a simple problem:

 

"SELECT * FROM episodes LIMIT {limit} OFFSET {start*limit};"

The problem is quite insidious because it runs in 0.05s when you start, but by the 30 millionth row it might take 10s.... This is because the offset rule in sqllite will actually go through the database throwing out rows, until you get to the offset desired!!

The solution? Don't use offset, instead use ROWID! Even if you have no "id" with autoincrement on your table, your table will create a ROWID internally hidden that can be used (unless you created your table "without ROWID").

 

TRY THIS!

"SELECT * FROM episodes WHERE ROWID > {start*limit} LIMIT {limit};"

Learn this lesson, time things and profile them individually, so you can know if things are slowing down... why! I might have assumed my upload from my sqllite was still fast, but the transfer to my server was causing the issue, it is known to slow down, but this was doubling or tripling the issue.

Tags: SQL, sqllite, efficiency