Fix bug and improve SQL command to update metrics

- fix bug where get of db.type config causes crash
- improve SQL statement to a single query. Still ugly, but at least only
  a single network round trip.