optimize db queries

Change-Id: Id6f7918e3a37aa108881a9820467e2b69972a6ab
Reviewed-on: http://review.couchbase.org/31186
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Pavel Paulau <pavel.paulau@gmail.com>