changed query for pp to speed up query on large databases