Sql optimise pr28 30 merged

Sql optimise pr28 30 merged