Speed improvements of queries