Performance improvements

* Consolidate tens of stats queries to one slower one
* Fixed a bug in SQL that scanned whole RC table

Change-Id: Ie2e27bff534dfbd499480c8c93d3e46b1dd97781