3 ms·
Indexes in general are not flattened trees; they are just trees. Using a Python map as a mental model is fraught with peril since those are implemented as hash
by Sesse__ 1y ago
Indexes in general are not flattened trees; they are just trees. Using a Python map as a mental model is fraught with peril since those are implemented as hash tables, which don't have ordering (which most database indexes need to support). So it's the wrong model.
For multidimensional indexes, you don't need to do anything fancy about nesting; you just need to have keys that have more than one component. To a first order, string concatenation is a good enough model. So in your case, here's what the index looks like:
['10 red', 101],
['11 blue', 111],
['12 green', 121],
['13 red', 131],
['14 blue', 141],
['14 red', 142],
more values…
which is then organized in some sort of tree structure, like (the exact details don't matter):
['10 red', '12 green', # min and max values for the tree below
[
['10 red', 101],
['11 blue', 111],
['12 green', 121]
]],
['13 red', '14 red', # similar
[
['13 red', 131],
['14 blue', 141],
['14 red', 142]
]],
more nodes…
- lelandbatey 1y agoYes, this is true. I chose a nested-maps example because while it is inaccurate for actual DB applications, it's very helpful for explaining the limits of index ordering and the limits that range queries have when interacting with indexes. It's helpful to use maps as a first explanation because they're one of the most used datastructures that nearly all languages have built in (yes, I know, C, I'm talking about the other 9 of top 10 languages), and many work-a-day developers and dabblers will use nearly _only_ lists and maps (and objects/classes/structs) in their language of choice. I could've used Python's OrderedDict but I figured if I was going to stray away from "the most straightforward possible case using the datastructures everyone is already using daily", I'd have been better off jumping straight to a custom tree, like you've done. That's a great miniature example of such a tree!