Optimize inbox SQL queries