Sometimes tweaking performance is a must, not because of $$, but
to make something possible on the website.
Sample task for developer is to create sidebar:
- show list of states in country + count companies in each state AND
- show for each state list of sub-states with count of companies in it.
In the end we have > 20 states and > 450 substates.
It looks like this:

- list-of-states
- Image 046.png (12.66 KiB) Viewed 3275 times
Reference query with
cms:pages has following params:
Page generated in: 17.147 sec Queries: 608 -> my(dev) VM, CPU 70%, host: powersaver
Page generated in: 24.808 sec Queries: 608 -> my(dev) VM, CPU 70%, host: powersaver -> cold run
Page generated in: 1.655 sec Queries: 611 -> production server
Yeah, production server (PrS) screams! But.. with anything more than a couple of visitors 1.6 sec on production server will turn into really slow page load (which has other elements too).
Reference query looks like below, where I take each state and then each substate of that state. Notice the use of #1 rule:
skip_custom_fields='1'.
Rule#1
If you ever need to show/use only page titles and page names without all the rest info from page's editables - use skip_custom_fields, because it will reduce the number of queries CouchCMS sends to backend.
Limit=unlimited is my invention, where I set <cms:set unlimited='10000000' scope='global' />. It is important to know that CouchCMS never lists more than
1000 pages if limit is not specified in your code. Maybe @KK thinks that this is an impossible number for you, my friends,

but it of course helps to prevent some nasty code to loop indefinitely.
Rule #2
Always specify your limit in cms:pages if it can be over 1000 pages.
Finally, reference query:
- Code: Select all
<cms:pages masterpage='data/states.php' aggregate_by="admin/firma.php::firmastate_rel"
custom_field="k_rel_count>0" skip_custom_fields='1' limit=unlimited
orderby='page_name' order='asc' return_sql='0' >
<li>
<a class="list-group-item" href="<cms:embed code=link_for_location />">
<cms:show k_page_title />
<span class="text-muted text-regular pull-right"><cms:show k_rel_count /></span>
</a>
<cms:pages masterpage='data/substates.php' aggregate_by="admin/firma.php::firmasub_rel"
custom_field="k_rel_count>0 | state_rel=id(<cms:show k_page_id />)"
skip_custom_fields='1' paginate='0' orderby='page_name' order='asc' return_sql='0'>
<cms:if k_paginated_top ><ul></cms:if>
<li>
<a class="list-group-item" href="<cms:embed code=link_for_location />">
<cms:show k_page_title />
<span class="text-muted text-regular pull-right"><cms:show k_rel_count /></span>
</a>
</li>
<cms:if k_paginated_bottom ></ul></cms:if>
</cms:pages>
</li>
</cms:pages>
This is a very good query, based on the fact that each company (
firma) is related to 1 state and 1 substate with editable type='relation' in template with companies. So with
aggregate_by I count all companies. More details about this parameter in this topic
viewtopic.php?f=5&t=8581&p=16352&hilit=cms%3Aquery+query#p16352Later I will publish this query rewritten with
cms:query with details why it can speed up things at least 1.5x, making following stat possible:
Page generated in: 7.597 sec Queries: 44 -> my VM, CPU 70%, host: powersaver
Page generated in: 10.943 sec Queries: 44 -> my VM, CPU 70%, host: powersaver, cold run after restart
Page generated in: 1.107 sec Queries: 47 -> server
GOAL: 0.3s
This is much better, more than
twice faster on my local VM and 1.5x faster on PrS. Stay tuned.