Sql query optimization for image detail

Current the image detail call uses left outer joins
for its queries, which makes it neglect the index on the
images table. The approach used in this patch, splits up
the querying into two parts and then does the join,
so that we can make use of the index

Change-Id: I65df0be5f156959c1e70ad9cb1a8b7eceebb4d8a
Merge "Sql query optimization for image detail"