Debuting sp_helpExpandView

Who here hates nested views? I sure do. And whenever I find them used in code, untangling them can be a behemoth of a task.

Having faced them one too many times, I finally became fed up. I thought to myself, that there has to be a better way than opening up each object, checking each table reference, and crawling further and further down the rabbit hole. SQL Server must have functionality buried within it that would allow me to programmatically dissect and untangle a nested view.

So I started writing a script against system views. But then I ran into some roadblocks. As I researched those, I discovered the DMF sys.dm_sql_referenced_entities(). And thanks to that wonderful DMF all of those problems went away.

The result is a script tool that I am calling sp_helpExpandView!


TL;DR

sp_helpExpandView will take a view & return a list of all objects referenced underneath. V1 supports references to child views, tables, inline functions, table valued functions, and 1 level of synonyms.

Output comes in two formats – vertical and horizontal.  Default is ‘all.’

Usage:
EXEC sp_helpExpandView
@ViewName = ‘[schema].[view]’,
@OutputFormat = ‘[all|vertical|horizontal]’

Download Latest at GitHub – Update Available: 2016-05-05

Known Issues:

  • Synonyms against objects not fully qualified with 3 part names will not be correctly processed.
  • Does not process 3 part names correctly. ex: ([db].[schema].[object])
  • If no schema specified, does not default dbo. schema.
  • Error in output if 3 part name used in underlying code & no schema specified. ex: ([db]..[object])

I hope people find this helpful.  I would love to hear any suggestions or feedback you may have.

Special thanks to Ben Thul (b|t), who mentioned PARSENAME() to me on Twitter. I’d forgotten that function existed & it helped clean some things up.

sp_helpExpandView – Well I didn’t expect that…

So a few minutes ago, I had a random thought.  sys.dm_sql_referenced_entities(), which is the fundamental basis of sp_helpExpandView, works on an assortment of objects, not just views.  Hmmm… I wonder… let’s run it against a stored procedure?  BOOM!  I had a list of all objects referenced in that proc!!!  And of course, all of the views that were referenced were also expanded out.

Well that’s nifty!  You can use sp_helpExpandView to quickly assess objects referenced by a stored procedure!

Yep – I planned that feature… sure did!  Hah!

Check out the latest version here:  sp_helpExpandView

Debuting sp_helpExpandView

 

How Am I Using sp_helpExpandView? #SQLNewBlogger Challenge

Welcome to my first blog post of my good friend Ed Leighton-Dick’s (b|t) #SQLNewBlogger Challenge! A few weeks ago, he challenged new #SQLFamily bloggers to commit to writing 4 blog posts in April – 1 per week. The response has been tremendous and I am excited to read everyone’s posts.

Today, I’m going to write about how I’ve been using my sp_helpExpandView tool. I joined a new company in 2015, and their environment has many nested views. These views are a significant contributing factor to their performance issues.  In order to address these performance issues, I am spearheading a project to consolidate nested view code.  The first key step is untangling each view.

Why Are Nested Views Bad?

In brief, heavy usage of nested views will force the Query Optimizer to work through even more steps when creating a query execution plan. Each query within each nested view must be parsed. Keep in mind that when generating plans, the Query Optimizer only has a limited amount of time. It will not generate the “best possible” plan for your query, but a “good enough” plan. And often times that “good enough” plan is still a poor execution plan.

Imagine how much work SQL Server must do to put together a plan consisting of 5 JOIN operations. Now what if each of those objects were nested views, which contained their own set of JOINs. And underneath those, more JOINs. It can go on and on and on.

How Can sp_helpExpandView Help Me?

This is where sp_helpExpandView comes into play. I wrote it to aid in identifying every single object that a particular view makes reference to. My environment has views that are nested 7 levels deep. Another colleague who tried my tool shared that he was de-tangling a nested view that has 31 JOINs, plus sub-queries and UDFs!!!

When you run sp_helpExpandView against a view, with horizontal output, you can quickly see how many layers of nesting your view contains. You’ll be able to identify all objects that are referenced.

Another thing to look for is whether any tables are referenced multiple times, due to being referenced in different sub-views. The Query Optimizer may not be able to consolidate the repeated references, which could result in an execution plan that repeatedly references the same object unnecessarily.

Can I Check My Entire Database’s Codebase At Once?

sp_helpExpandView works great against a single view, but what if you are not quite sure where to start? Maybe your database has dozens or even a couple hundred views?

To help tackle that, I’ve written a companion script that executes sp_helpExpandView against all views on a database.

Tool – sp_helpExpandView All Views

This script saves each individual view’s output into a single table, then dynamically pivots the entire resultset to present it in horizontal format. I then export the entire resultset into Excel, which allows me to quickly skim, filter, and analyze my entire database with ease!

How Did This Help Me?

When I ran my companion script in Dev, I found that 2/3’s of our views were nested views!  I was able to catalogue and prioritize which views’ complexity based on number of levels and number of objects referenced.  Visually showing this output to my developers also helped them to realize how much work SQL Server was being forced to do.  Because all of the other objects were abstracted away in the nested views, my developers didn’t fully realize and appreciate all of the objects they were querying when referencing one of these views.

Call To Action!

Does your environment contain nested views? Do you even know? Try out sp_helpExpandView and run the companion script (not in Production please). The results may surprise you!

Don’t Forget Everyone Else!

Be sure to read other posts by other #SQLNewBlogger participants!  Use this Twitter Search to help you find them quickly!

T-SQL Tuesday #68: Changing SSMS Keyboard Query Shortcut Defaults

Welcome everyone!

TSQL2sDay150x150
After being a long time read and occasional participant, I am honored and humbled to be your host for this month’s T-SQL Tuesday! In my Invitation to T-SQL Tuesday, I asked everyone to write a blog having something to do with “Defaults.”

For my contribution, I decided to blog a bit about SQL Server Management Studio. As a SQL Server Developer, I live my life within SSMS. As much as I like it, there are a variety of defaults that I like to change. And for today’s blog, I’m going to talk about Keyboard Query Shortcuts.

If you navigate to Tools -> Options -> Environment -> Keyboard -> Query Shortcuts, you will find that SSMS comes with a handful of defaults.

1_Options
Tools -> Options

2_Query Shortcuts - Default
Query Shortcut Defaults

These three starting shortcuts are indeed very handy, but SSMS gives us a number of other blank entries to add our own. Wouldn’t it be a waste if we did not take advantage of them, to add in some of our own?

2_Query Shortcuts
My Default Changes

As you can see above, here are ALL of the new Keyboard Query Shortcuts that I add. And here they are in full:

  • Ctrl+3 = sp_help
  • Ctrl+4 = sp_helptext
  • Ctrl+5 = sp_SQLskills_SQL2008_helpindex
  • Ctrl+6 = sp_SQLskills_SQL2012_helpindex
  • Ctrl+7 = EXEC sp_whoisactive @get_plans = 2, @get_transaction_info = 1, @get_task_info = 2, @get_avg_time = 1, @get_outer_command= 1
  • Ctrl+8 = EXEC tempdb.dbo.sp_help
  • Ctrl+9 = EXEC sp_helpExpandView @ShowObjectCount = 1, @ViewName =
  • Ctrl+10 = EXEC sp_whoisactive @get_plans = 2, @get_transaction_info = 1, @get_task_info = 2, @get_avg_time = 1, @get_outer_command= 1, @delta_interval = 10

Let’s explore how I make use of each of these!

sp_help & sp_helptext

I use these two SQL Server built-in stored procedures on an almost daily basis. They’re extremely useful while in the midst of code, to check aspects of objects. How do you make use of it? Simple! All you have to do is highlight the object in question, then hit your Keyboard Shortcut!

3_sp_help

Using sp_help, I can quickly peruse all available columns in a given object, double-check their datatypes, or take a quick glance at what foreign keys and constraints are in place.

4_sp_helptext

With sp_helptext, I can quickly scan the contents of a view or stored procedure, when I don’t want the hassle of finding it & script it out via the Object Explorer.

sp_SQLSkills_SQL20xx_helpindex

SQL Server comes with a built-in stored procedure: sp_helpindex. sp_help also returns index information about a table. However, they don’t return everything. When I’m query tuning and checking underlying indexes, I need to know about INCLUDE columns. That’s where Kimberly Tripp’s sp_helpindex rewrite comes in.

5_sp_sqlskills_helpindex

Used in the same fashion as sp_help & sp_helptext, I now have in-depth index information available at my fingertips!

sp_whoisactive

“Hey, why’s the server running slow right now?” Every time I hear that question, Adam Machanic’s sp_whoisactive is the very first thing I always run. If you don’t use this tool already, read more about it on Adam’s blog and install it NOW!

While you can run it without parameters, I like having some additional data points available to me, which require parameters. As you can see from the above command list, that’s a heck of a lot to memorize and re-type, so I’ve mapped that to Control-7.

tempdb.dbo.sp_help

If you’re like me and work with a temporary tables regularly, you’ll notice that the usual sp_help shortcut does not work. Not having sp_help information conveniently available for temporary tables is rather obnoxious, hence this particular Query Shortcut.

6_sp_help_temptbl-1
Executing sp_help against a temporary table, in a user database context, returns an error.

6_sp_help_temptbl-2
But with a modfication to execute from tempdb, we can get our sp_help data again!

Now I can have the same sp_help functionality and convenience with my temporary tables!

sp_helpExpandView

My final Query Shortcut executes my custom tool sp_helpExpandView. Used in the same way as sp_help, I can quickly reference underlying objects of a view. If you have to deal with untangling nested views, I would encourage you to check it out!

That’s it for my SQL Server Management Studio Keyboard Shortcuts. I hope you all found this insightful. If there are any Defaults that you like to change in SSMS, please feel free to share in the comments. Thanks for reading – until next time!

Free Community Tools: sp_helpExpandView

A few months ago, Brent Ozar called for bloggers to write about about Free Community Tools in the month of September. While it’s been a while since I’ve done any work with it, I’d like to offer up sp_helpExpandView.

Does Anyone Actually LIKE Nested Views?

A number of years back, I joined a company that happened to LOVE nested views. And they also happened to have an ETL/DW loading process that literally took SEVEN DAYS to complete.

“Wow Andy, SEVEN DAYS?!? That DW must have been several terabytes! (circa 2015)”

“Uhh, no… less than 250GB at most?”

Yeah, that thing was small. But the processing was a horrific case study in T-SQL worst practices. And the architect that created it LOVED nested views (and scalar functions… and MERGE… on Standard edition).

I spent a good amount of effort trying to unravel those as part of my efforts to improve performance, and as a result, decided to create my own community tool to help with unraveling them – sp_helpExpandView.

Andy – Why the Nested View Hate?

In a nutshell, after a number of layers of nesting, cardinality estimates in nested view operators go off a cliff. And cardinality estimates play a critical role in execution plan quality, which is why it is important to have good estimates. If you’ve dug deeper into your nested view performance, you’ll see exactly what I mean.

Want to dive deeper into that topic? Then check out my Exploring Why UDFs and Nested Views Hinder the Query Optimizer presentation.

Documentation For sp_helpExpandView?

I’ve written a few blog posts about sp_helpExpandView that you can read, which I’d strongly suggest that you do.

What About Nested Stored Procs?

Secret super-tip… sp_helpExpandView also works with Stored Procedures!

Go Forth and Unravel

I do sincerely hope that this tool helps you unravel your nested views. Sure wish there was a magic tool to rewrite T-SQL to just stop using them, but unfortunately that still requires human intervention. Hopefully you’re a consultant and can get paid by the hour for this work!

Thanks for reading!