Optimize inbox SQL queries

I'll change it