Modified subquery to handle >16TB databases.