See nested functions in SQL Server as a call tree
You are investigating a slow query, and the execution plan shows a multi-statement table-valued function that seems to cost almost nothing? The Function Tree shows which functions, views and procedures depend on each other, how deep the chain goes and where a look at the actual plan pays off.
Your script output stays in your browser.
The analysis runs entirely in JavaScript on your computer. Whatever you paste is not uploaded, not stored and not transmitted to PSG. PSG receives no data from your analysis. Once the page has loaded, you can also explore the tree offline.
Technical details and how to check for yourself
The page only loads its own files. A Content Security Policy blocks script connections such as fetch, XHR and beacons (connect-src 'none'), form submissions and third-party scripts. No code on the page transmits or stores your input. No tracking, no cookies; the PDF report and the image are also created locally. Feel free to check: open the developer tools and go to the “Network” tab, or disconnect from the network after the page has loaded.
If the object names themselves are confidential, the script replaces them with numbers via @anonym = 1 before you copy anything.
How it works
- 1Get the script. psg-funktionsbaum.sql reads the SQL Server catalog and searches module definitions for features such as
WHILE. It changes nothing except its own temporary tables. The output contains neither definition texts nor table contents. SQL Server 2016 or later; for a complete picture,VIEW DEFINITIONandSELECTonsys.sql_expression_dependencies. - 2Run it. In a new query window in the database you are investigating. Optionally set a function as
@objekt; otherwise you get the whole database. - 3Paste. In the second result set, click the XML cell
psg_funktionsbaum. Copy the entire text from the window that opens and paste it below.
Paste the script output
Paste text or drag a file here.
Call tree
Starting points
The output contains the whole database. The list shows modules that no other module calls, those with the most MSTVFs below them first. Or search directly:
Levels from left to right. Height of a block = number of end points below it. Clicking a block zooms in; clicking the leftmost block zooms out again. Hatched: already expanded further up.
The root in the center, each level a ring. Angle of a segment = number of end points below it. Clicking a segment zooms in; clicking the center zooms out again. Hatched: already expanded elsewhere.
Each module exactly once; column = longest path from the root. The number on a box shows how many modules call it here. Clicking a module highlights all paths that run through it; clicking an empty area clears the highlight. Dashed orange: return path in a cycle.
What to check in the execution plan
These are candidates for review based on the structure, not findings. Only the actual plan shows whether a spot is expensive. In our tests, it was often not the chain itself that was expensive, but a single statement inside it without a suitable index.
A look at the actual plan clarifies whether this is as intended. PSG’s SQL Server consultants are glad to go through these spots with you. You decide what you show.
Discuss the execution plan with PSGWhat the tree does not show
- Only static references from
sys.sql_expression_dependencies. Dynamic SQL, CLR code and calls from the application are missing. - Whether a function is evaluated once or per row is not part of the dependencies. A scalar function in the SELECT list can run per row; the plan shows whether SQL Server inlines it.
- References to other databases or through linked servers end as a leaf. Run the script again there.
- Features such as cursors or
WHILEcome from a text search; matches in comments are counted too. - “Inlineable” is the catalog value
is_inlineable. Only the plan shows whether SQL Server actually inlines a scalar function into the query. - Whatever the executing account is not allowed to see is missing: without
VIEW DEFINITION, a module’s calls are missing; withDENY VIEW DEFINITION, the module is missing entirely, without any notice. Ideally, run the script with an account that is allowed to see all definitions. - No runtimes. You can find those in Query Store or in the actual plan. How to find the statements inside multi-statement functions there is covered in the series Nested MSTVFs.