A
Optimize SQL query in template code