lowered the running time by minimizing database trips