Consistent query generation by sorting hash keys/values

All uses of keys and values to build SQL queries are now sorted.
Consistent queries given the same inputs are important for query caches
which may require the query to match exactly, such as MySQL's
implementation.  It also improves debuggability and aggregation when
doing performance analysis by making it easier to find the same query
across runs.