Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-07-15 09:38:29

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

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

#2 2013-07-15 11:09:32

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,794
Website GitHub

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

#3 2013-07-15 11:23:18

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Reducing database queries

philwareham wrote:

Be better to use than this:

No. Only thing that will do is to hard-code the URL to the template, which makes development and deployment impossibly hard, since every link will be wrong.

Offline

#4 2013-07-15 11:26:51

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

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

#5 2013-07-15 12:10:31

etc
Developer
Registered: 2010-11-11
Posts: 5,397
Website GitHub

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 parseing) 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

Board footer

Powered by FluxBB