Tuesday, April 12, 2011

T-SQL Tuesday 17: Apply

Welcome to another T-SQL Tuesday!  This week is hosted by Matt Velic (blog|twitter), so Matt what is this week about?
APPLY Knowledge
Recently on Twitter, I heard the claim that “If you don’t understand the APPLY operator, your skills are somewhere around the 50th percentile at best.” While I believe that Adam was giving a warning to self-proclaimed experts (possibly one he might have been interviewing at the time…), I also believe that we could take it as a challenge as a T-SQL blogging community to learn more about APPLY and the ways in which we can use it in our work.
Please share how you use this wonderful feature. Maybe you know how APPLY works inside and out? Perhaps you’ve got a fantastic user defined function (UDF) to share? Or maybe your experience revolves around using Dynamic Management Functions (DMFs) in your never-ending quest for SQL Server performance? Let the community know as it is time to study!
All right, here we go then this is all about the Apply!


The Apply operator was added to SQL Server with SQL 2005.  Apply  is a logical join operator, it allows you to perform a join on a Table Valued Function.  I think the most common way that people use Apply is to get the SQL Text or Query Plan using DMV’s.

FROM sys.dm_exec_requests er
CROSS APPLY sys.dm_exec_sql_text(er.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(er.plan_handle) qp

The previous query will return any currently executing query on your system.  Using the CROSS APPLY is like using a left join, but on a Table Value Function, there is also an OUTER APPLY which is similar to the functionality as a right join.  In this case we are Using CROSS APPLY to return the Text of the SQL Statement being executed, as well as the Query Plan.

The Query Plan can have some over head associated with it, if you’re on a busy system I would recommend commenting out the Query Plan unless you’re truly looking for it.


So the DMV route is pretty well known, but how do you apply this in a non-troubleshooting capacity?  For this next example I’m using the AdventrueWorks2008R2 Database.

So let’s toss out a Case Scenario.  Our boss has come to us, there is an application that the company uses and as part of the expanded functionality we’ll be adding a lookup of Customer’s information.   We need to provide a list of Customer Names on demand, or the Name of a particular Customer, that are identified to us as People and not businesses.  (Not a complicated Scenario, but hey let’s start out practical).

If you Expand AdventureWorks2008R2 | Programmability | Functions| Table-valued Functions, you’ll see dbo.ufnGetContactInformation.  This Table Valued Function is exactly the type that we can use in the CROSS APPLY Statement.

Our Table Value Function accepts the @PersionID parameter, and we will be using the SALES.CUSTOMER table.   We take a quick look at the table to see what details we pull back.


We see that names are not stored in this table, but we know we can retrieve them through our function.  Okay we don’t know that, but if you right click on the function and script it out you can read through it and see what it is doing.

So let’s use our CROSS APPLY statement.
    SALES.Customer sc
    CROSS APPLY dbo.ufnGetContactInformation(sc.PersonID) ci

So now we can see that this can be used in a non-troubleshooting capacity.

 So let’s think about a couple other possibilities.  Let’s select an order being delivered and we want to know the last update in its location.  We have a customer looking at an item on a web page, and we want to display a coupon for the item.

There are a lot of possibilities for CROSS APPLY, I would suggest watching  the MCM Waits and Queues video by Paul Randal (blog|twitterthis link to the Demo will cover a great scripts by Glenn Berry (blog|twitter) using CTE’s & one by Joe Sack (blog|twitter) that makes a really good use of OUTER APPLY.

How do you clear the server's wait
DBCC SQLPERF('sys.dm_os_wait_stats', CLEAR);

BY: Glenn Berry & Paul Randal

CTE to get wait stats.

This CTE get's the wait stats as they are occuring on the
system and give's you the total seconds of wait, the
# of occurances, as well as the top percentage of where
your wait time is coming from. 

The Exclusions of certian wait types were recommendations
by Paul Randal in the MCM wait types video series

    wait_time_ms /1000.0 AS Waits,
    (wait_time_ms - signal_wait_time_ms)/1000.0 AS Resources,
    signal_wait_time_ms/1000.0 AS Signals,
    waiting_tasks_count AS WaitCount,
    100.0 * wait_time_ms /SUM(wait_time_ms) OVER()AS Percentage,
    (ORDER BY wait_time_ms DESC)AS RowNum
FROM sys.dm_os_wait_stats

    CAST(W1.Waits AS DECIMAL(12,2))AS Wait_S,
    CAST(w1.Resources AS DECIMAL(12,2))AS  Resources_S,
    CAST(W1.Signals AS DECIMAL(12,2))AS Signal_S,
    CAST(W1.Percentage AS DECIMAL(12,2))AS Percentage,
    CAST(AVG(W1.Signals)/ W1.WaitCount AS DECIMAL(12,8))AS AvgWait_S,
    CAST(AVG(W1.Resources)/ W1.WaitCount AS DECIMAL(12,8))AS AvgRes_S
    Waits AS W1
    INNER JOIN Waits AS W2
    W2.RowNum <=W1.RowNum
    W1.RowNum,W1.wait_type,W1.Waits,W1.Percentage, w1.Resources, w1.WaitCount,w1.Signals
    SUM(W2.Percentage) -W1.Percentage <99
BY: Joe Sack

This will give you the waits as they are happening
on a system, and give you the query text, as well
as the execution plans as they are occuring

I added the @@SPID to the query so we would not
see our own query plan come back while trouble

    DB_NAME(est.dbid) AS DatabaseName,
from sys.dm_os_waiting_tasks owt
INNER JOIN sys.dm_exec_sessions es ON
INNER JOIN sys.dm_exec_requests er ON
OUTER APPLY sys.dm_exec_sql_text(er.plan_handle) est
OUTER APPLY sys.dm_exec_query_plan(er.plan_handle) eqp
WHERE es.is_user_process=1
    AND es.session_id<> @@spid;
Here are the Scripts, but go watch to video to see how to use them!



Monday, April 11, 2011

Your Training is Approved! How Do You Get It Again?

With the upcoming SQL Rally there has been a lot of discussion amongst the community of how to help people ask for training. 

One of the first things to go in a tight economy is being sent to Training.  There is a lot of great information out there for free.  There are great blogs, videos, SSUGs (SQL Server User Groups) and not to mention SQL Saturdays.  As a DBA if you are looking to improve yourself there are a lot of great resources available to you.

“So Ball’s do you want me to go to Training or what?”

Glad you asked Dear Reader, yes I do.  The one thing that free training lacks that in-person training provides is social interaction.  Not only does this help you personally but professionally as well.


Two words, simple concept, Social interaction.  There are a lot of things that makes up a DBA’s job, and I’m not just talking Job Description.  What sets apart a really great DBA from others?  It’s not just what they know, don’t get me wrong you have to be knowledgeable in your field.  What sets a great DBA apart is something more fundamental than that, it is their social skills.

Social skills are just like any other that we have, if you do not utilize them then they rust.  If you practice them then they flourish.  When you are at a SQL conference, SQL Saturday, or SQL User Group this is your opportunity to stay sharp.  There is a lot of good information being passed around, ask questions, make points, discuss what you’ve done, and learn something.  This will help you when you get back to your job.

I would ask you to close your eyes and think of a meeting you’ve had at your company.  There is a complex technical issue, perhaps a production environment is down, and the Users/Sales are impacted.  How do people work together?  Do ego’s come out?  Is your opinion listened to and openly discussed?

 I cannot tell you how many MVP, Authors, and in-general SQL Legends that I’ve met that are nice, humble, and willing to listen in a complex technical setting.  If you do not work with a team of DBA’s, or if that team is not a cohesive team, then this is a refreshing experience.  Being in person with people from the SQL community is a treat and can help you use Technical  Social Skills that you can utilize in other areas of your job.

How To Ask

For great blogs on HOW to ask for training check out Kendal Van Dyke (blog|twitter) and Brent Ozar (blog|twitter)

If you need help on how to approach your boss, Kendal wants to help he is very available through his blog, or also on twitter.  Anything short of him having to pay your way, and he’ll be glad to help J.

Brent presents a lot of wonderful ways for you to approach your manager.  He also answers his comments, (as you can see by reading them), so if you have a situation that you don’t feel he covered ask him!  He is also readily available on Twitter.

***************UPDATE 4/11/2011**********************************************

Later in the day while at work I discovered that the Awesome people putting together SQL Rally had a great ROI section that was posted in March.  Not only are there wonderful arguments listed as to why you should go, but there is also a great form letter that can help you ask to big boss to be able to attend.  I had to include the links to this.




I want to focus on the idea that you have been APPROVED to go.  Your SUPER EXCITED, and you should be!  But through the haze of the excitement you need to consider a few tings.  You don’t want this to be a one shot deal.  You want to go to training again.  So how do you make that happen?  You need a plan.  So ask yourself the following:

1.       What am I going to Learn?
2.       How Will I apply it when I get back?
3.       How do I show my Boss the Training was worth it?


So your approved to go, now is the time to look at the track’s being offered and determine what your 1st, 2nd, and 3rd choices are for each time slot.  But as you decide I’d like you to think of any issues or new technologies that are being used by your company.  Try to set yourself up to be a returning superstar.

You should think about the projects your working on, or the upcoming one’s you know you’ll be roped into working on. 

If you are working on a project to get your arms around your SQL Environment then look for Sessions on Policy Based Management, Auditing, and Powershell. 

If you are looking at adding High Availability to your environment look for Sessions on Disaster Recovery, Mirroring, Replication, and Clustering.  If Performance is where you need to concentrate look for Sessions on Indexing, Wait Stats, and Compression. 

The point is knowing where you are going and what you NEED to learn to get there will help you achieve some WINNING in the long run.


So now it’s Monday and your back at the office.  Your inbox is not Zero, you’ve got meetings, and a to-do list a mile long.  What did we get out of our trip?

While things are fresh it is easy to be excited and ready to go, back at the office reality sets in.  Now is when I like to make a list.  You should do a top 5 things that you learned from training that you want to apply to your job.  If you have co-workers that went to training with you, you can talk to see what their top 5 things are, and align them to work together.

We attended some training a couple months back, and when we returned I had a list of what I thought the top 5 things we could apply were.  The number 1 thing that I wanted to do was pretty low on the list.  After talking with all the other DBA’s it turned out that was the number 1 thing on their list, which they thought could benefit the company right away.

So now that you’ve got your list, and your co-workers are on board, set up a road map of how you will present this to your boss.

Let’s take Policy Based Management for example.
  1. Find a Dev, Test, or local SQL Server Instance on a Laptop that can Server as your Central Management Server.
  2.  Register the instances you would like to run Policy Based Management Against. 
  3. Work with your co-workers, determine policy’s that are most beneficial to your environment. 
  4. Set them up and Run them.
  5. Demo it for your Boss
There are so many features in SQL Server to take advantage of, that it is rare to find a place that has it all figured out.  There is probably something that could benefit your work place that you are not currently using:  Snapshots, Mirroring, Backup Compression, Filtered Indexes, Sparse Columns, Policy Based Management, Data Compression, Central Management Server the list is long. 

If you can find something to apply, and actually get it in place, then future training should be easy to obtain.

What if you didn't find anything new?  What if you were the only DBA to go?  When you come back try to share the wealth.  You just spent some time watching a lot of people present on topics, now is your chance to try with your co-workers.  Work up some of the topics where you learned or gained a clearer understanding of how something works.

  1. Work up a Summary of the Topics and some Demos (grab the decks and scripts if they are available for download)
  2.  Set up a meeting with your co-workers
  3. Present the topics
  4. Get Feedback, go back to the top and start working on your top 5 list with your co-workers on board


Here’s the big payoff.  The next time you try to go to a conference, when your boss hems or haws you say “Hey boss remember problem xyz, and how I fixed that after training.”  Or even better “Here’s how we improved things after training.”

Businesses are all about Return On Investment, ROI, if you can show them that sending you to training is an investment they will get a return on then you’re already on your way to your next round of training.

I hope to see you at SQL Rally this year and next!



Monday, April 4, 2011

MeMe Monday

When restoring Master
If corrupt make certain you
Enter -m; for start up