How we made the order list ten times faster
The list took 480 ms to open in our largest workspaces. It now takes 35. Here is what we measured, what we changed, and what we would do differently.
Measure before you change anything
Our first guess was the network. It was wrong. A trace of one slow request showed 430 of the 480 ms inside a single database query: the one that counts orders for each status tab.
We added timing to every step of the request and left it on in production for a week. The picture did not change: the count was the problem, and it grew with the size of the workspace.
One index did most of the work
The count scanned every order of the workspace because the status was not part of any index. A combined index on workspace, status and date let the database answer from the index alone.
That one change took the query from 430 ms to 22. It cost 3% more disk space and made writes about 1% slower, which we could not measure in practice.
Stop counting what nobody reads
The list showed “1–25 of 184,302”. Nobody needs the exact total of a list that long, and computing it was the second most expensive step.
We now count up to 10,000 and show “10,000+” beyond that. The pager offers next and previous instead of a jump to the last page, which nobody used: we checked.
Cache the parts that do not change
Status counts change only when an order changes. We keep them per workspace for 30 seconds and drop them on any write to that workspace.
The cache is small, easy to reason about and safe to lose. When in doubt we would rather serve a slow answer than a wrong one.
What we would do differently
We would have added the timing first, a year ago. Every guess we made without it was wrong, and each one cost a day.
We would also set a budget: a page that takes longer than 100 ms on the server now fails a test. The list has room to grow before anyone notices.