Improving the implementation of an SQL query.