Improve select query
Add upsert