Add indexes and optimize some queries