Database query improvements