add keys to improve performance; use temporary tables