I’m wondering how these work under the hood, especially for large result sets. For example, would the DB likely write the raw result to disk and then do an external sort?
I’m also wondering how this works with LIMIT…OFFSET. If the DB can’t utilize an existing index it seems like the DB would have to sort the whole thing and pluck the subset of the raw result set.
Indexes are ordered; if there’s a suitable index, that will be used. Otherwise, they’ll need to sort, as you suppose. The execution plan for a query (which you can get with e.g.
EXPLAINor via client menus; the exact method of getting it varies with the DBMS) may contain hints as to how a query will be sorted.See: