Run SQL queries asynchronously

Running SQL queries in the main thread is not a good idea because they
can take quite a while at times.