SingleStore Interview Question

Optimize this given C++ code.