How does database indexing work? (2008)
stackoverflow.com
How does database indexing work? (2008)
1–10 of 62 posts
Re: How does database indexing work? (2008)
#2Re: How does database indexing work? (2008)
#3...the same way as non-database indexing? e.g. a lookup table for values in a different order than they're stored
Additionally, parent’s answer doesn’t actually answer the question, by saying what an index is, but the question was “how does it work?” The top SO answer does a great job with this one, I think. Another one a few answer down also goes into some of the downsides (it slows down writes, for one). The whole page is worth a skim if you’re not a DBA but have to fiddle with DBs on occasion.
Re: How does database indexing work? (2008)
#4Re: How does database indexing work? (2008)
#5...the same way as non-database indexing? e.g. a lookup table for values in a different order than they're stored
Re: How does database indexing work? (2008)
#6Does anyone know what an index card was?
Yep, that old :(
Re: How does database indexing work? (2008)
#7...the same way as non-database indexing? e.g. a lookup table for values in a different order than they're stored
1. Binary search. It's be really handy to have the URLs stored in a different order, maybe grouped by client_id first and then all the URLs for the same client are alphabetical. The index conceptually just contains the same rows in a different order.
2. The read vs write trade-off. Your data fits in memory if there's less than 10 TB of it (possibly up to 100 TB) so the big thing is latency, what an index does is, it requires you to write the same information twice in order to read it exponentially faster, so depending on how often do you write versus how often you read, indexes make less or more sense.
3. High fan-out trees. Once you have explained that the index is just an the same data in a different order, you have inserted a sort of ticking time bomb of misunderstanding. The problem is, a beginner programmer’s notion of a freely indexable list, such that binary search works on it, is an array. How do we insert into an array? With all the work of array copies! So you get people who think that random ID columns should never be indexed, because every write into the middle of the array has to shove half the data one cell to the right—unacceptable! So it really helps to say, “now we can store this as a binary tree, make one comparison per level of the tree.” Pause for understanding that the “list,” can be stored with such a tree. “but, modern computers have a nice property that right after accessing some memory the memory near that location is loaded into a cache... this makes arrays real fast. Suppose we don't just have the binary tree thing of 2 child nodes each representing 50% of our data, but an array with 100 pointers each representing 1% of the data, you get something like a 6x memory speedup from the cache, because your tree is one sixth the depth of the binary tree. And that's basically what a B-tree is, you use a higher fan-out of your sort tree to exploit the cache.”
Re: How does database indexing work? (2008)
#8Re: How does database indexing work? (2008)
#9Does anyone know what an index card was?
In school and public libraries it was a physical piece of thick paper, similar to a Rolodex card, which had multiple copies, always one per author and one per title, but sometimes backups by subject manager. And when I was a kid, there were often multiple, redundant copies that one could take to the librarian to check out the physical book if it were particularly important. Yep, that old :(
And I've been thinking that I don't use the subject indexes on the online catalog anywhere near enough. They can surface things that the standard keyword search might miss or bury among irrelevant results.
Re: How does database indexing work? (2008)
#10I had assumed btree in Postgres was referring to a binary tree but that is definitely not the case. I also learned when to use a hash index, rather than a btree. (If I understand correctly if you’re only using the = operator rather than something that needs sorting with >, <, <=, etc. then a hash will perform better.)
Binary trees are the ideal in a world of a pure von Neumann model. In practice data structures perform better when data is grouped together and this is what the b-tree gets you. It's both more disk and cache efficient in real world workflows