Optimize db queries



<!---
@huboard:{"order":2.25}
-->