Reduce SQL queries. Issue #436.