SQL optimization

Bug 1358092

Change-Id: I688f290dd8904621a7d9328dfeed549eed8ab2aa
Merge "SQL optimization" into 1.9_STABLE