Use a group query for metadata

For large surveys, we save significant time by doing this processing on the database. For a test survey of ~75k entries, the metadata step went from the range of 45-48 seconds down to about 6 seconds.

/cc @hampelm