Macro performance gotcha

Have you ever had the impression that the site felt slowish after altering for example a transformation? Let me share some intel that is crucial when working with K# macro conditions.

If you have worked with macros before, you will have probably written something like:

{% Documents.Where("NodeID = 1") %}

Tip: you can easily test macros using the macro console within the System module.

Macro console

This is actually a simple expression that underwater fires the following query:

Single query

Tip: to debug macros and SQL queries turn on the debug settings within System --> Debug.

This macro fires one query which by default queries the node and document data and not page type specific information. If you want to retrieve the page type info, then you will need to change the macro to something like:

{% Documents.Where("NodeID = 1").WithAllData %}

And this is where it gets interesting! If you take a look at the SQL query debug, notice that it will run one query for every page type in the system.

Multiple queries

This obviously results in unnessary amount of queries and will have a big influence on the performance of the site. This makes sense because we didn't specify the page type. Therefor the fix for this is really easy! Simply modify the macro as follows and you are all good to go :)

{% Documents.ClassNames("CMS.Root").Where("NodeID = 1").WithAllData %}

Tip: for more info on debugging macros check out the excellent post of Kentico MVP Brian McKeiver with awesome troubleshooting poster!