Optimised upsert queries.
Updated SQL schema