==Variable Inlining==
The value of a variable can be ''inlined'': The variables references are replaced by the expression that is bound to the variable. The resulting expression can often be simplified, and further optimizations can be triggered:
<syntaxhighlight lang="xquery">
let $nodes := //nodes
return 'Results: ' || count($nodes)
(: will be rewritten to :)
let $nodes := //nodes
return 'Results: ' || count(//nodes)
(: will be rewritten to :)
'Results: ' || count(//nodes)
</syntaxhighlight>
As the example shows, variable declarations might be located in the query prolog and in FLWOR expressions. They may also occur (and be inlined) in {{Code|try}}/{{Code|catch}}, {{Code|switch}} or {{Code|typeswitch}} expressions.
==Function Inlining==
Functions can be inlined as well. The parameters are rewitten to {{Code|let}} clauses and the function is body is bound to the {{Code|return}} clause.
<syntaxhighlight lang="xquery">
declare function local:inc($i) { $i + 1 };
for $n in 1 to 5
return local:inc($n)
(: will be rewritten to :)
for $n in 1 to 5
return (
let $_ := $n
return $_ + 1
)
(: will be rewritten to :)
for $n in 1 to 5
return $n + 1
</syntaxhighlight>
Subsequent rewritings might result in query plans that differ a lot from the original query. As this might complicate debugging, you can disable function inling during development by setting [[Option|INLINELIMIT]] to {{Code|0}}.