Only lock the database mutex when actual queries are performed, but not during the entire flush loop.