SQLite uses external n-way merge sort to sort the record when you execute an order by statement without having index. I replace the merge sort routine with radix sort hoping to asymptotically improve the sorting performance, but the result is a little bit surprising.

I tested on a randomly generated dataset, and run an “order by” query on every million row data inserted.

The things that strike me the most is, even with 18 million row of record, SQLite (which uses external n-way merge sort) still looks like O(n). I even test until 350000000 record, but the graph still looks linear.

This result make me start doubting the necessity of any linear time sorting algorithm.

Source code : https://github.com/AlvarHHM/sqite3-cmake/blob/nowhile/src/vdbesort.c#L1655

Update: I fix a integer overflow program and now able to test bigger dataset.