Query..generate(): optimize generated code by caching terms used more than once in local variables; document