QuickBug: Several fix to query optimizations.