Merge database optimisations from lpotherat.