Some optimization improvements (particularly adding indexes to speed up queries needed to run clustering)