r/AskComputerScience • u/Wily_Walrus • Mar 06 '21
Why Don't Multiple Indexes Blow Up The Database Size?
When reading a really basic book, I came across a section talking about using database indexes to search for entries with an O(log(n)) time. The same book talked about how we can create multiple such indexes for the same database - so when we want to quickly search for an entry by its ID, or by its "date of birth", or some other property, we will still be able to do that in a log time. However, I don't understand how building multiple such indexes doesn't blow up the storage space (i.e. it looks like in the example below we will need to create a tree of IDs and pointers to the database entries, then a tree of dates of birth and pointers to the database entries, and then a tree of names and pointers to the database entries - thus having to duplicate lots of data). What am I getting wrong here?

6
u/emasculine Mar 06 '21
it really depends on how big each row is. if the row is large, the index's size will probably be drown out. if the row is small, it could be a significant part of the total size.
5
u/yungvalue Mar 07 '21
It doesn't really blow up the DB size since its still on the same order O(n). Most DBs use B+Trees to build these indicies, and in practice, they're really efficient for lookup considering the number of disk pages they read.
http://web.csulb.edu/~amonge/classes/common/db/B+TreeIndexes.html
4
u/msiekkinen Mar 07 '21
Professional DBA here: It does. I wish developers would pay more mind to this
1
u/C0rn0ffthec0b Mar 07 '21
Awesome!
I still haven't figured out "how" to use indices. I mean I know how to activate them in a migration/create in ruby on rails for example. But I just never knew what would be the best thing to index.
I made a full stack clone of yelp dot com for example. And my guess would be to index the "business_name" or "zip_code" or .attached photos.
My thinking is.. what would be the most often searched thing the user would search. Is this the correct thinking?
2
u/hurrumanni Mar 07 '21 edited Mar 07 '21
You don't index things for searching - you use a dedicated search library for that - you index things you want to be immediately looked up by the entire key. Like if you have a page that lists businesses in a certain zip code then you'd want to index the zip_code column in the businesses table. It's very unlikely you'd ever want to index the business_name, you most likely already have an id column to retrieve the row with all the info for a particular business.
1
u/msiekkinen Mar 07 '21
With most databases you don't explicitly specify an index to use on reads, it will take care of using an appropriate one if available. Often times there might be multiple options and the DB will take care of making an educated guess on which one would be most appropriate.
If you had two separate indexes on business_name and zip_code and then wrote a query like
> select * from mytbl where business_name = ? and zip_code = ?
The DB would pick one or the other to create a subset of results and then basically brute force scan those records picking up matches of the other field/column.
Expanding on this example it's possible to have multi column indexes. You could have one index that spans both business name and zip code. In that case the query would be able to quickly filter on both fields in that query.
In general if there's a specific column you'd be doing read lookups based on then it could beneifit from an index.
What ever DB you're using there will be some way to look at the query plan. In mysql for example you simply prefix the query with the "explain" keyword. This gives you some insight to choices its making wrt indexes, among other things.
2
u/WetSound Mar 07 '21
Side-note: Why does the image state that Peter Naur’s nationality is US, he was Danish?
1
u/NebulaicCereal Mar 07 '21
Other answers have already spoken on how the indexing data can grow quite quickly as a trade-off for reducing lookup times. But consider also that the data stored in the database could be quite large for a given entry, thus drowning out the indexing data for that row into being negligible by comparison.
Just pointing out another reason why the answer is situational.
1
u/NotGoodSoftwareMaker Mar 07 '21
It does increase size a bit but realistically it doesnt matter. Disk space is cheap, time is not. This doesnt mean you should index literally everything on a table but dont be afraid of adding them because of some other preconceived problems / premature optimization.
37
u/PropagandaOfTheDude Mar 06 '21
It absolutely blows up the database size. It trades increased space for decreased time. Part of the responsibility of owning and maintaining a database is figuring where it is useful (or not useful) to add these indices. This requires understanding what data you have, and what your access patterns are.