Execution of complex db Queries in tpl What is performance cost of executing queries like EntityFieldQuery directly in tpl.php files ?

I have some situations, where i have put db queries and more complex php logic in various tpl.php files.

It works, but my concern is performance.
What are best practice ? Should i move those queries somewhere else and what are advantages ?

Thanks!
To be honest you should never, ever run an SQL query inside a template file. This sort of thing should be handled either at the preprocess level, or even better, at an earlier stage in the page build (usually in a custom module file). There are good reasons for this:


Putting business/application logic inside a template file is never a good idea. Literally reams of volumes have been written on the subject; over the past few years MVC has been the 'it' pattern so I'd recommend reading up on that to understand the reasoning behind this kind of separation.
If you execute queries in your template files they will run even for pages for which the rest of the page content has been cached, which is a definite performance no-no.


I imagine there are other good reasons not to do it as well, suffice to say these are two quite strong arguments for moving the logic out of your template files.