Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Reducing database queries
Hi there.
I understand that it’s wise to have a minimal amount of database queries to lighten the load on the server and keep sites snappy, so doing things like reducing the number of <txp:output_form />
tags to the bare minimum is good (instead, try to keep static code in the page templates or in sizeable form templates).
Does that also ring true for tags like <txp:site_url />
and <txp:if_individual_article />
, etc? i.e. do all Textpattern tags equal another database query?
In summary, would this:
<a href="http://example.com">Site name</a>
Be better to use than this:
<a href="<txp:site_url />"><txp:site_name /></a>
Obviously I’m referring to a coding specific site here, that example wouldn’t be good for a universal theme.
Offline
Re: Reducing database queries
philwareham wrote:
do all Textpattern tags equal another database query?
Not necessarily. Some (quite a few) just read data from the Textpattern globals $pretext
, $thisarticle
, $thiscategory
, $thisimage
, etc. So once those are populated then you can call them multiple times with no round trip to the database.
Other tags (such as getting an author title) are done once per author so if you refer to the same author later in a page with a second <txp:author title="1" />
tag (perhaps in an article list form) then you only hit the database once for each author’s name.
So, it depends. But if you are unsure, you can always assign the tag output to a txp:variable and use that multiple times without incurring any DB hits.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Offline
Re: Reducing database queries
Thanks.
Maybe that wasn’t a good example. I was referring to whether code with tags would be more of a performance hit than static code. I think Stef answered that for me.
Offline
Re: Reducing database queries
philwareham wrote:
In summary, would this:
<a href="http://example.com">Site name</a>
Be better to use than this:
<a href="<txp:site_url />"><txp:site_name /></a>
DB queries are not the only thing to take into consideration. The first snippet will run ~4-5 times (and up at 10 times if you disable parse
ing) faster than the second one, though neither requires a DB query. UDF calls are rather expensive in PHP too, and each txp tag requires at least one UDF call. This said, a right speed/maintainability balance is not evident to find, but I would avoid txp tags as much as possible inside loops (like <txp:article />
and other lists).
Offline
Pages: 1