I have a table/schema with two columns named day of DateTime and user_id of Integer. Right know I made both columns indexed.
Is performance improvements gained from indexing worth it, considering huge fraction of additional space used by the index and there are only two columns? How do you justify them?
How does this differ if I use MongoDB or MySQL?
Depends on the queries you intend to run.
WHERE day = ..., then you’ll need an index whose leading edge containsday. If properly used, indexes can speed-up querying many orders of magnitude, especially on large data sets.At the end of the day, I recommend you measure on realistic amounts of data and come to your own conclusions.
BTW, If you are using InnoDB, then your table is clustered (see also: Understanding InnoDB clustered indexes) and the whole table is effectively stored in the primary index. The secondary indexes in clustered tables contain copy of the PK fields, which (I’m assuming) is
user_idin this case. And since we only have two fields in the table, the secondary index on {day} will cover theuser_idas well, avoiding a double-lookup that could otherwise happen in a clustered table. Effectively, you’ll end-up with two separate (but synchronized) B-Trees and an index-only scan no matter which one of them you access (which is good). Of course, you could explicitly make a composite index on {day,user_id} instead of just {day}, for a very similar effect.