Sql optimise conflicts resolve branch

PR#9,PR#18 and PR#32 changes included in one PR