db.cpp: improve performance

Much faster, especially on systems with disk drives.