Improved database query