SQL backend: Improve performance by reducing number of SQL queries