Cleanup of database and query usage