Ref T13242. For context, see this transaction.search profile from secure:
https://secure.phabricator.com/xhprof/profile/PHID-FILE-47g3frqfj7vmdcjfaczc/
There's ~80ms spent on query string construction.
Currently, we build query strings twice: once on construction (to test for errors) and once on use. We always expect to get the same result, so we can cache these strings. This should improve query string construction cost by ~50%.
Also, about 25% of the strings are empty: qsprintf($conn, ''). This is important for safety/correctness, but we can short-circuit these when building strings. This should reduce construction cost by ~25%.