To understand the answers to your questions, I think it'd be helpful to first talk a bit more about what an index is.
The classic analogy is to compare a database index to an index in a book. Y'know, like this:
Imagine that you have a cook book, and for whatever reason, recipes in the book are ordered randomly. Suppose you want to look for an Italian desert recipe. What do you do? Well, unfortunately, you're forced to scan through the book, from front cover to back cover until you find it.
Now imagine that we have an index like the one in the image above where recipes are organized alphabetically. So "chicken parmigiana" would be listed under "C". Hm, actually, that isn't helpful. You know you want an Italian desert recipe, but you don't know what letter it'd start with.
Now imagine that you have an index that is organized by cuisine. Italian, French, Indian, Chinese, Thai, etc. With this, we can go to the Italian section and look there. Woo hoo!
Another thing that'd be helpful is if we had an index organized by meal type. Breakfast, lunch, dinner, desert, appetizer, etc.
Let's relate this to software. Imagine that we have an analogous situation where you have a list of recipes and you want to find an Italian desert.
var recipes = [{
cuisine: "french",
mealType: "dinner",
name: "coq au vin"
}, {
cuisine: "thai",
mealType: "lunch",
name: "pad thai",
}, {
cuisine: "italian",
mealType: "dinner"
name: "chicken parmigiana",
},
...
{
cuisine: "italian",
mealType: "desert",
name: "tiramisu",
}, {
}];
Just like with the cook book, you'd have to traverse through the list to find your Italian desert. But now imagine that we have the following data structure:
var alphabeticalIndex = {
c: [ coqAuVinPointer, chickenParmigianaPointer ],
p: [ padThaiPointer ],
t: [ tiramisuPointer ],
};
That doesn't help much does it. We don't know what letter to look under. How about this?
var cuisineIndex = {
french: [ coqAuVinPointer ],
thai: [ padThaiPointer ],
italian: [ chickenParmigianaPointer, tiramisuPointer ],
};
Nice! That's helpful! And this is helpful too:
var mealTypeIndex = {
lunch: [ padThaiPointer ],
dinner: [ coqAuVinPointer, chickenParmigianaPointer ],
desert: [ tiramisuPointer ],
};
Hopefully this illustrates why database index's help you find stuff more quickly. Perhaps it also illustrates the downside of indexes: they use up extra memory. Imagine if we had indexes for everything: expensivenessIndex
, easeOfCookingIndex
, popularityIndex
. Actually, those seem pretty useful. If you're planning on doing a lot of lookups using the index, it's probably worth having even though it uses up extra memory. But what about something like dateOfRecipeOriginIndex
? Are you ever looking up recipes that were created in the 1950s
vs recipes that were created in the 1830s
? Probably not. In that case, the cost of using additional memory outweighs the benefit of faster lookup speed. Similarly, in the real world, if you have a user
with a mothersMaidenName
property, you probably wouldn't want to create an index for mothersMaidenName
since you'd never be looking up users by their mothers maiden name. On the other hand, you might want to index something like firstName
.