Index Queen — Database

Optimize SQL queries with indexes: 15 levels of EXPLAIN, composite, covering and partial indexes, SARGable rewrites and JSON indexes, timed on real sqlite-wasm.

What the levels ask

  1. Rara is looking for a book for her homework by its ISBN. There is no catalogue yet, so Mrs. Endang combs through 2000 books one by one (SQLite's plan: SCAN buku). Write a catalogue with CREATE INDEX so Mrs. Endang goes straight to its shelf.
  2. Mr. Harun wants to read again the books published 1990 to 1994, when he started driving the district bus. A sorted catalogue can be opened at one point and read on until another.
  3. Mrs. Ratna is inviting the members whose status is aktif and who joined since 2024 to a new reading club. For this request SQLite opens only one catalogue on the anggota table, and a two-column catalogue is read from its left column.
  4. Ms. Wulan from the post office matches email addresses the members wrote with capitals and small letters any old way, so her request compares lower(email). An ordinary catalogue keeps the email exactly as it was written.
  5. The neighbourhood head is writing a letter to the people of Kidul hamlet and only needs nama and dusun. With a catalogue on the hamlet alone, every card still sends Mrs. Endang to the shelf to read the name in the book.
  6. Mrs. Lastri is chasing the books not yet returned and past their due date. Of 4000 loans only a few are still out. The drawer on Mrs. Endang's desk is small: your new catalogues may hold at most so many cards (written below).
  7. Bagas is looking for tales whose title starts with the word kancil, then Grandma Marni for the ones that end with it. Both requests are counted together. Note: LIKE in SQLite does not tell capitals from small letters.
  8. Mrs. Ijah is looking for two customers of her stall: she remembers one's phone number and the other's email. The first request uses OR; Bagas wrote the second again with UNION ALL after reading an old blog. Both requests are counted together.
  9. Mr. Tono wants to know which books the people of Kidul borrow. The column pinjaman.anggota_id points at anggota.id (FOREIGN KEY), but it has no catalogue yet.
  10. Dina is setting up a "most wanted" shelf: books borrowed more often than their category's average. Its subquery runs again for every book, and every run combs the shelves again.
  11. Mrs. Yanti is counting how many titles and how many pages each category has for this month's report. Without a catalogue, Mrs. Endang brings every book to the reading table and sorts them by category first.
  12. Mr. Rizal delivers parcels and wants to see the last 20 visitors: the newest day first, and within a day in the order they came. Without a catalogue, Mrs. Endang sorts the whole guest book first.

And more, 15 levels in all.