Add query profiling and logging, enhances #363

This is a slight modification of @tuupola's implementation of query profiling and logging (#363), which I believe addresses the feedback that @jpfuentes2 included in the discussion of #363.

Happy to have a go at further improvements, if desired.