Fetch performance
This page is for designers whose pages got slow. In templates, almost all cost comes from fetches and the attributes
that quietly fetch (children, parent, data_map). The rules below keep the number of database queries small.
Contents
The rules
- Fetch once. Store the result in a variable; pass it to parts as a parameter.
- Always a
limitonlistandtree; paginate withoffsetand alist_count. - Filter in the fetch (
class_filter_array,attribute_filter), not with{if}in the loop. - Prefer what you already have. In the pagelayout,
$module_result.content_infoholds the class, node id, depth and URL alias of the page without a fetch. - Cache what is the same for many visitors: menus and footers in cache-blocks (Cache-block).
- Keep full views cacheable so the view cache serves them (Caching for designers).
Attributes that fetch
| Attribute | Cost |
|---|---|
$node.children | all children, no limit, no filter |
$node.children_count | a count query |
$node.parent, $node.path | one or more node fetches |
$node.data_map | the object's fields (loaded once per object) |
$node.object.owner, $node.creator | an object fetch |
$node.children on a folder with thousands of items is the most common cause of a slow page: use a fetch with a
limit instead.
The loop trap
{* slow: one fetch per child *}
foreach $children as $child
def $sub = fetch( 'content', 'list', hash( 'parent_node_id', $child.node_id ) )
...
/foreachTen children mean eleven queries, a hundred mean a hundred and one. Instead, fetch the descendants once with tree and
depth 2 and group them in the template, or show the sub-level only on the child's own page.
Measuring
On a test siteaccess, enable the debug output for your address ([DebugSettings] DebugOutput=enabled,
DebugByIP=enabled, DebugIPList[]): it shows the time of the page and the database queries. Compare before and after a
change, with caches cleared and again warm.
Related pages
- Fetching nodes and lists
- Theme performance
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.