Don't insert empty batches in the database

This doesn't do anything and produces an error.
Don't use references to QVector elements as the vector may be reallocated

When joins need to be added in buildJoins, join& becomes a dangling reference
and can cause crashes (and Valdrind doesn't like that).