Optimize inbox SQL queries

Ok, I'll remove this