Added indexes, optimizations to database

Added indexes to the database to make queries faster.
When a database is upgraded, ANALYZE and VACUUM are run to improve the
speed and size of the database.