{"id":241,"date":"2026-11-22T08:07:00","date_gmt":"2026-11-22T13:07:00","guid":{"rendered":"https:\/\/quentinpidcock.com\/content\/how-a-database-finds-one-record-in-a-million-without-reading-them-all\/"},"modified":"2026-11-22T09:12:10","modified_gmt":"2026-11-22T14:12:10","slug":"how-a-database-finds-one-record-in-a-million-without-reading-them-all","status":"publish","type":"post","link":"https:\/\/quentinpidcock.com\/content\/how-a-database-finds-one-record-in-a-million-without-reading-them-all\/","title":{"rendered":"How a Database Finds One Record in a Million Without Reading Them All"},"content":{"rendered":"<p>Every program I have worked on eventually hits the same wall. When a table is small, looking something up feels instant. As that table grows \u2014 more customers, more orders, more posts \u2014 the same lookup starts to crawl. Nothing about the question changed. I am still asking for one record. What changed is how much the database has to read to find it. The usual fix is not a faster computer. It is an index.<\/p>\n<h2>The Slow Way: Reading Everything<\/h2>\n<p>Without an index, a database answers a lookup the blunt way. If I ask for the customer with a particular email address, it starts at the first row, checks it, moves to the next, and keeps going until it finds a match or runs out of rows. Programmers call that a full table scan.<\/p>\n<p>For a small table, that is perfectly fine. Reading a few dozen rows takes no real time. The trouble is that the work grows in direct proportion to the table. Double the rows and you roughly double the reading. By the time a table holds a very large number of records, every lookup pays the cost of reading almost everything just to hand back one thing.<\/p>\n<h2>An Index Is a Sorted Shortcut<\/h2>\n<p>Think about the index at the back of a printed book. I do not read the whole book to find one topic. I go to the back, where topics are listed in alphabetical order, find the one I want, and follow the page number straight to it. A database index works on the same idea.<\/p>\n<p>When I create an index on a column \u2014 say, the email column \u2014 the database builds a separate, ordered list of the values in that column. Next to each value it stores a pointer to the full row that value belongs to. The table itself stays in whatever order its rows were written. The index is the tidy, sorted copy of just the part I search by, with directions back to the real data.<\/p>\n<p>Keeping that list sorted is the whole trick. A sorted list can be searched without checking every entry, the same way I can find a word in a dictionary without starting at the first page. I open near the middle, decide whether what I want comes before or after what I see, and narrow the search again and again. Each step cuts the remaining work down, so even a very long list can be searched in a small number of steps.<\/p>\n<h2>How the Lookup Actually Runs<\/h2>\n<p>Most database indexes are stored as a balanced tree \u2014 a top entry that points to a small set of middle entries, which point down to the sorted values at the bottom. What matters is what happens when I run a query.<\/p>\n<p>First, the database checks whether an index exists for the column I am searching. If it does, it searches the index, not the table. It walks down the tree, following the ordering until it lands on the value I asked for, then follows the pointer to fetch the one full row I wanted. If several rows share that value, it follows each pointer. Either way, it touched a handful of entries and the matching rows \u2014 not the whole table.<\/p>\n<p>That is also why the wording of a query matters. An index on email helps when I search by email. It does nothing for a search by last name, and it may not help if I search in a way that breaks the ordering, like looking for any email that merely contains a certain word somewhere in the middle. The shortcut only works when I use it in the direction it was built.<\/p>\n<h2>Why I Do Not Index Everything<\/h2>\n<p>If indexes make lookups fast, why not put one on every column? Because an index is not free. It takes storage of its own, since that sorted list and its pointers have to live somewhere.<\/p>\n<p>The index also has to be kept true. Every time I add, change, or delete a row, the database has to update the index as well as the table. One index is a small extra cost on every write. A table covered in indexes makes every write do a pile of extra bookkeeping, and past a point the writes get slow enough to cancel out what the reads gained. An index also only helps if the column actually narrows things down. Indexing a column where nearly every row has the same value saves little, because the database still ends up fetching most of the table through the pointers.<\/p>\n<p>So the real work is choosing. I index the columns I search and join by often, I leave the rest alone, and I check the slow queries I actually have instead of guessing.<\/p>\n<h2>The Practical Takeaway<\/h2>\n<p>Here is the plain version I keep in mind. A table is the data. An index is a sorted map to that data, built for one kind of question. Searching without a map means reading everything; searching with one means reading the map, then going straight to the answer.<\/p>\n<p>When an application of mine gets slow as its data grows, I look at the index list before I look at the hardware. In my own work, the query was usually fine and the data was fine \u2014 the map was just missing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every program I have worked on eventually hits the same wall. When a table is small, looking something up feels instant. As that table grows \u2014 more customers, more orders, more posts \u2014 the same lookup starts to crawl. Nothing about the question changed. I am still asking for one record. What changed is how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-241","post","type-post","status-publish","format-standard","hentry","category-articles"],"_links":{"self":[{"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/posts\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":1,"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":242,"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/posts\/241\/revisions\/242"}],"wp:attachment":[{"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/quentinpidcock.com\/content\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}