Backend for AIintermediate11 min · 10 steps
Database Indexing
An index is a sorted side structure that lets the database jump to the rows a query needs instead of scanning the table: faster reads, paid for with storage and slower writes.
query1,800ms
pages readall
heavy workrequestdataresponsesignal
metrics are illustrativeProblemStep 1 of 10
Reading 2 million rows to return 20
The chat UI asks for the last 20 messages for user E12345, newest first. The messages table has 2 million rows and no suitable index.
So the database does a sequential scan: it reads every row, keeps the ones for E12345, sorts them by time and returns 20. About 1,800 ms (illustrative), and it gets slower every day the table grows.
SELECT * FROM messages
WHERE user_id = 'E12345'
ORDER BY created_at DESC
LIMIT 20;Lesson 9 of 20 in Backend for AI