Some SQL and indexes improvements