sql optimalization, frontend fixes