More optimizations for #75

Condenses many of the SQL queries down to a single query per call,
dramatically speeding up quark in several instances. Additionally, adds
the profiling decorators I've been using in testing to util.py for
future usage and optimization.