Blog

Why I Don’t Use SQL Server’s Activity Monitor

I love free tools. I also love analyzing SQL Server’s wait statistics. But I’m not a fan of Activity Monitor, a free tool in SQL Server Management studio, which helps you look at wait stats.

Activity Monitor just doesn’t give you the whole truth.

I fired up a workload with HammerDB against a test SQL Server 2014 instance. My workload runs a query that’s very intensive against tempdb, and it’s really beating the SQL Server up by querying it continuously on seven threads.

Let’s look at our wait statistics. Here’s what Activity Monitor shows in SQL Server 2014:

ActivityMonitorSQLServer

“Buffer Latch” must be my problem.

Here’s what our free procedure, sp_AskBrent shows for a 10 second sample while the workload is running. I ran: exec sp_AskBrent @ExpertMode=1, @Seconds=10;

sp_AskBrent

Hmmmm, that top wait type doesn’t seem like it was in the first screenshot at all.

Here’s what Adam Machanic‘s free procedure, sp_WhoIsActive, shows for an instant in time during the workload. I ran: exec sp_WhoIsActive

spWhoIsActive

Here I get specifics on exactly what type of wait is is and the related query.

Let’s compare.

Activity monitor groups wait types. It took a whole lot of waits and rolled them up into ‘Buffer Latch’. This isn’t necessarily a bad thing, but I’ve never heard of documentation that explains what’s rolled up into which groups. By comparison, sp_AskBrent showed me the specific wait types PAGELATCH_UP, PAGELATCH_SH, and PAGELATCH_EX, with the amounts for each one. sp_WhoIsActive even showed me the type of page that is having the bottleneck (GAM) and the database and data file (tempdb’s data file 1).

Activity monitor leaves out wait types. sp_AskBrent showed that in aggregate, my #1 wait was CXPACKET over the sample. That tells me that a lot of my queries are going parallel. That’s not necessarily a bad thing, but it’s important for me to know that about my workload at the time. It helps me know that I want to learn more, and make sure that the right queries are going parallel. (In this case, the query that’s going parallel is pretty cheap, and is just as fast single threaded. My throughput goes up dramatically if I adjust the Cost Threshold setting up a bit.)

Friends don’t let friends use Activity Monitor. It may be convenient, but it doesn’t tell you the truth. Do yourself a favor and use a free tool that gives you wait statistics straight up.

 

How to Start Conversations With Your Favorite Authors at Conferences

How it feels to say the exact wrong thing.

How it feels to say the exact wrong thing.

It can be a tricky to introduce yourself to people at conferences when you read their blogs and watch their presentations and videos. You feel like you know them, but they don’t know you yet. What the heck do you say? So you take a deep breath, you head on over, and then… you say something really awkward.

I’ve totally been there too, with my foot right in my mouth. I am truly socially awkward, so much so that reviewing posts like this helps me get better at small talk before a big event.

Dodge the Backhanded Compliment

“Your presentations are great, even though ____.”

What it sounds like: “You’re not that good.”

What to say instead: “Your presentation on [folding paper towels] was great.”

It’s OK if you already knew how to fold a paper towel, they won’t assume you’ve never seen one before. Just stop before the “even though” or “but”. Less is more! And trust me, a simple concrete statement explaining that someone’s hard work helped you out will truly mean a lot to them. If you want to follow it up with a question to start a conversation, you can chase it with, “What inspired you to present on that topic?” (If you have real criticism, it’s valuable to share that too– after a few sentences. It’s kinda weird to lead with that.)

Sneak Past that Accidental Brush Off

“I wish I could come to your talk, but _____”

What it sounds like: “I’m not coming to your presentation.”

For newer speakers, it’s disheartening for them to hear this: they just hear “I’m not coming” and they immediately picture having to present to a room full of empty seats. That can have kind of a “sad Eeyore” ring because many speakers fear “what if nobody shows up?” (This is really hard to not say by accident, I’m still training myself out of it.)

What to say instead: “I like your title and abstract for [Cool Story, Bro].” It’s OK, you don’t have to attend. But you also don’t have to explain that you’re not going, unless they specifically ask. If you want to start a longer chat, it’s also great to ask them, “I’m thinking of attending Mladen’s session on [Security for Developers]. Do you know any other good sessions on that topic?” Speakers love to help you figure out what session to attend, even if the session isn’t their own.

Ooops, Did I Just Kinda Call You Ugly?

“You look so much _______  in person!”

What it sounds like: “You look short/dumpy/frumpy/bad in some context.” Even if you’re saying they look great now, this, uh, implies that’s not always the case.

What to say instead: “It’s great to meet you face to face.” Don’t worry, they think it’s great to meet you as well. If you’re in a place where you can have a chat, just ask, “How did you get started [publishing hilarious animated gifs on the internet]?”

Pack Your Bags For A Quick Guilt Trip

“Do you remember me? We met at ________.”

What it sounds like: “It makes me feel bad that you don’t remember me. And this might be a trick question and I’m totally trolling you, you won’t know till you answer.”

Some people are really good with names and faces. Oh, how I envy those people! For the rest of us, we do truly feel guilty if we met you before and you know who we are and we don’t remember your name. The real problem with this is that it’s hard for the conversation to not fall flat after this. It doesn’t go anywhere.

What to say instead: “I think we may have met at [a store selling panty hose in Texas] a few years back. It’s great to see you again!” Just adding a little “maybe” in there automatically puts the other person at ease if they don’t remember the situation for whatever reason.

Boy, My Tribe Sure Is Dumb!

“I love your blog posts. My developers are so dumb though! They always are doing _____.”

What it sounds like: “I don’t have anything nice to say about anyone. And maybe you’re a member of that club.”

I think some folks start like this because it’s a way of saying, “we must have this in common, right?” But it just doesn’t work so well. Leading with a negative statement gives the conversation a weird vibe, and you’re gambling that the person actually agrees with you. Sometimes they don’t!

What to say instead: “I can really relate to that blog post you wrote on [juggling chainsaws].” And if you don’t remember a specific post to talk about, but you can think of a topic, just mention that. Letting people know that you read their work is pretty darn exciting for them, all by itself.

I’M NOT REALLY INTERESTED IN YOU

“You talk to ___, a lot right? Where are they?”

What it sounds like: “You’re not that important to me, but your friends are.”

It’s totally normal to talk about what you’ve got in common, but don’t start with the people commonalities. Talk about what’s important to that other person – their work, presentation, family, or even just get coffee.

What to say instead: “What’s the #1 thing on your mind this week?” This lets the person share their excitement with something, and it’s contagious. You might learn something about a fun insider event too!

How to End It, Short And Sweet

Most initial conversations at conferences aren’t very long. You’ve got sessions to go to, there’s tons of people around saying hi to each other. But don’t be afraid to end it: you’ll probably run into one another again soon. Offer to trade business cards! That will help you remember each other– especially if your business card has your picture on it.

Small Talk is Just a Skill

You’ve got to start a conversation somewhere, and at a conference, you start it with small talk. We aren’t all naturally good at it, though.

Know this: even if you do end up with your foot in your mouth, it’s OK. Just smile and keep meeting new people. We’ve all been there, and it’s not nearly as big a deal as it feels like when your face turns red.

How to Find Wasted Memory in SQL Server [Video]

Tune in here to watch our webcast video for this week! To join our weekly webcast for live Q&A, make sure to watch the video by 12:00 PM EST on Tuesday. Not only do we answer your questions, we also give away a prize at 12:25 PM EST – don’t miss it!

Is your SQL Server wasting memory? Join Kendra to learn how to identify when memory is going to waste, and track down whether it might be due to licensing, schema problems, fragmentation, or something else. Register now.

Looking for the queries from the video?

There are two short queries that check out the sys.dm_resource_governor_workload_groups and sys.dm_os_nodes DMVs. For those, just read up on the topic linked in Books Online and write a very simple select.

The longer query that looks at memory usage by in the buffer pool is a simple adaptation from the Books Online page on sys.dm_os_buffer_descriptors. Check it out and customize it for your own needs!



          

Is it Cheating to Solve A Performance Problem By Adding Memory?

We had a comment recently on Five Things That Fix Bad SQL Server Performance that got me thinking. The comment came from a frustrated system administrator, who wrote:

Chucking resources at a problem is not solving that problem, it’s just temporarily masking the symptoms of that problem.

memoriesFunnily enough, I’ve heard the exact same thing from developers who are facing a tough problem on a dramatically undersized SQL Server. The basic gist of what they ask is:

Doesn’t it make me a bad developer if I can’t solve this problem with code?

Everybody’s worried that adding memory is somehow cheating.

Performance Problems and Memory

There are three general categories of performance problems relating to memory:

  1. Problems you can solve with memory OR code / indexes
  2. Problems you can solve only with code / indexes and NOT just memory
  3. Problems you can solve only memory and NOT just code / indexes

For category #1, solving the problem with memory is often cheaper than changing code and indexes: the operational cost of the hours of development and testing can be quite high.

But I specifically started thinking about category #3. This is the category that the frustrated sysadmin and proud developer think doesn’t exist!

Here are three examples of problems that you can solve with memory, and not simply with code and indexes:

1. Memory Reduces IO On a Read-Hungry Workload

You’ve got a critical OLTP application. You’re using shared storage. Most of the time, your storage throughput is pretty good. But sometimes, other users of the storage get busy, and your read and write latencies go through the roof. Your queries slow down, blocking increases, and timeouts start hitting the application. You’ve tuned your indexes, but parts of the application rely on realtime reports that need to scan large tables.

SQL Server is designed so that in this case you can add more memory and reduce read IO to the storage, giving you more stable performance. It’s absolutely not cheating to give the server more memory — speeding up the storage would be much more expensive.

2. Some Queries Need Lots of Workspace Memory

In another database, you do lots of reporting and aggregation. You’ve got large tables. Your queries frequently join them, do sorts, and use parallelism. All of these operations need memory, and the more data you’re aggregating and querying, the more memory each of these queries can need.

Before a query starts running, it figures out how much of this workspace memory it needs, and looks at what is available given other queries that are running. SQL Server has to balance the memory used for data pages, execution plans, and this workspace memory: it doesn’t want to let one query take over! If not enough memory is available, your query has to wait.

You’ve optimized your queries and indexes and made sure the memory estimates are realistic, but when lots of people run reports, your queries can’t even get started because of a memory crunch. Solving this problem by adding more memory isn’t cheating: it’s helping the SQL Server do what it’s designed to do.

3. Some Features Need Lots of Memory Periodically

You’re managing a data warehouse where you’re using table partitioning to implement a sliding window. This has helped your users a lot: you can add and remove large amounts of data with minimal locking and blocking.

But for query performance reasons, you’ve had to add one or two non-aligned indexes that are present during the day. To do partition switching, you have to disable these indexes to do your sliding window data load and archive each night. Creating the partitioned index can consume large amounts of memory.

Using Enterprise features can require investing in Enterprise levels of memory.

Adding Memory Isn’t Cheating

Memory is an important tool for performance in SQL Server: it isn’t just something that covers up bad code, it’s often a solution to many different types of problems. Memory isn’t the only tool you need to help your queries go fast in SQL Server, but it’s certainly NOT a tool you should ignore.

Top 5 Tips for Your First Presentation (Video)

Tune in here to watch our webcast video for this week! To join our weekly webcast for live Q&A, make sure to watch the video by 12:00 PM EST on Tuesday, September 16! Not only do we answer your questions, we also give away a prize at 12:25 PM EST – don’t miss it!

Curious how you can give a compelling technical presentation? Join Kendra to learn five important tips on how to select the right topic for your talk, write an effective abstract, construct a coherent presentation, and make it to the podium to give your first presentation.

Have questions? Feel free to leave a comment so we can discuss it on Tuesday!

Log Shipping Magic: Using A Differential Backup to Refresh a Delayed Secondary

Log: It's better than bad, it's good!

Log: It’s better than bad, it’s good!

Let’s say you’re a DBA managing a 2TB database. You use SQL Server transaction log shipping to keep a standby copy of the database nice and warm in case of emergency. Lots of data can change in your log shipping primary database: sometimes it’s index maintenance, sometimes it’s a code release, sometimes it’s just natural data processing.

And when a lot of data changes, your warm standby sometimes is a lot less warm than you’d like. It can take a long time to restore all those log files!

Here’s a trick that you can use to help “catch up” your secondary faster. A quick shout-out to my old friend Gina Jen, the SQL Server DBA and log shipper extra-ordinaire who taught me this cool trick years ago in a land far far away.

Log shipping Secret Weapon: Differential Backups

Through lots of testing and wily engineering, you’ve managed to configure nightly compressed full backups for your 2TB database that are pretty darn fast. (No, not everyone backs up this much data every night, but stick with me for the sake of the example.)

  • Log shipping primary had a full backup last night at 2 am
  • Log shipping secondary has transaction logs restored through 7 am
  • It’s 3 pm, and you’d really like to have everything caught up before you leave the office

Here’s an option: run a compressed differential backup against your log shipping primary. Leave all the log shipping backup and copy jobs running, though — you don’t need to expose yourself to the potential of data loss.

After the differential backup finishes, copy it over to a nice fast place to restore to your secondary server. Disable the log shipping restore job for that database, and restore the differential backup with NORECOVERY. This will effectively catch you up, and then you can re-enable the log shipping restore and you’re off to the races!

But Wait A Second. Aren’t Those Full Backups A Problem?

Running a full backup without the COPY_ONLY keyword will reset the “differential base”. That means that each differential backup contains changes since the last full backup.

But here’s the cool thing about log shipping: restoring a transaction log brings the new differential base over to the secondary.

So as long as you’ve restored transaction logs past the point of the prior full backup, you can restore a differential to your log shipping secondary.

This Sounds Too Good To Be True. What Can Go Wrong?

This isn’t foolproof. If you haven’t run a full backup in a long time, your differential backup may be really big, and taking that backup and restoring it may take much longer than restoring the logs. (Even if you’re using log shipping, you should be doing regular full backups, by the way.)

And like I mentioned above, if your log restores are so far behind that they haven’t “caught up” with the last full backup taken on the primary, you’re not going to be able to restore that differential backup to the secondary.

What If a Transaction Log Backup File Is Missing?

A technique like this could work for you, as long as a full backup hasn’t run since the transaction log backup file went missing. (If it has, you need to re-setup log shipping using a full).

But a word of warning: if you have missing transaction log backup files, you have a “broken” log chain. You should take a full backup of your log shipping primary database to get you to a point where you have functioning log backups after it, even if you’re going to use a differential to bridge the gap on your log shipping secondary. (And keep that differential around, too!) Keep in mind that you won’t have point-in-time recovery for a period around where the log file is missing, too.

Log shipping is Great

I just love log shipping. It’s quick to set up, it’s relatively easy to manage, it’s included in Standard Edition, and it’s got these surprising little tricks that make it easy to keep going. You can learn more about log shipping from Jes, and join us in person in 2015 in our Senior DBA training, which includes an advanced module on log shipping.

Teaching Tips: Help Your Audience Be Active Learners

Brent Ozar doing his thing

Brent uses handouts to get his audience to optimize queries  as part of his presentation

We teach a lot of courses. In addition to our online training, free webcasts and sessions at major conferences, we also teach mult-day events.

It’s incredibly valuable to keep your audience active. Even when you’re working in a lecture format, you should try to help give your audience moments where it’s natural for them to mentally wake up and shake themselves out of a more passive listening mode.

Don’t worry: this doesn’t require changing much as a speaker. You don’t have to make your audience square dance.

There’s two big benefits to building habits to keep your audience active:

Benefit 1: Active listeners learn more

When the role of a student is limited to passive absorption, it’s easy to get bored and sleepy. The student has to constantly refocus themselves, and that takes effort on their part. By giving students built-in opportunities to be mentally active, you effectively give their brain less work to do.

Benefit 2: Active learners are More Fun to teach

Imagine standing in front of two rooms of people: in one of them, people are slumped over with glazed eyes. In the other, they’re alert, leaning forward slightly, following you with their eyes, and taking notes.

If you’re a very beginning speaker, both rooms may terrify you. That’s OK. But if you’ve got a few sessions under your belt, the room of alert people is probably much easier to work with. You get natural feedback about what they understand and it adds meaning to your experience. It helps you do a better job. You’re less tired at the end and more energized.

That’s why it’s worth it. Here’s how to get it done.

Warm up your audience

Costumes are an option to get audience participation

Be careful wearing costumes, unless you ARE a Jedi Knight (or Doug Lane at a team dinner)

Don’t stand at the front of the room in silence before you get started. Chat with the people who are already there.

It’s OK if this doesn’t come naturally to you. I am very shy and nervous around strangers and small talk is quite difficult for me. You can overcome it! Write down a list of simple questions to ask the audience and even glance at it from time to time so you don’t have to remember:

  • Where are people from?
  • What other sessions have people been to? What was good? (If you’re at a conference)
  • What made them want to attend the session / is there anything they’re looking forward to learning?

Remember to smile. Drawing a smiley face on a post it note somewhere and sticking it on the desk helps, strangely enough. (People won’t even know it’s yours.) Welcome people in as they come into the room casually and let them know you’re glad they’re there. If you’re making eye contact, you’re already helping your audience.

Identify your audience’s job role

One easy question to build into the beginning of your presentation is to ask your audience to identify their job title or job role by raising their hands. Build a simple slide that says, “What’s your job role?” at the top and lists a bunch of options, including “Other”. Ask people to raise their hands when you say their role name out loud. (I usually tell people it’s OK to vote twice if they do more than one thing.)

This is a nearly foolproof method to get most audiences in the United States to interact with you. The question is designed to have no “wrong” answer. It also gives you insight into the background of your audience and their interests.

How many of you would like us to stop stuffing you with desserts?

“How many of you want the email address of the developer who wrote transactional replication?”

It’s possible that in some settings the audience will have a hard time even answering this question. Be ready for that, and understand that it’s not you. This gives you an early tip that you may have an quiet group for cultural or situational reasons, which is very useful for you to know!

Ask questions during your presentation

When you first start speaking, audience participation may be scary. Know that you can get past that: questions and comments from the audience are one of the most fun and rewarding things you can work with as a presenter. They help you work in real-world advice and information and make your presentation relevant.

See this as something to build up to gradually over time. Some easier questions to start with:

  • How many of you have worked with this feature?
  • How many folks have heard of this?
  • I’m about to show a big gotcha where I’m going to do ___. Do any of you think you may know what’s coming?
  • Who thinks they might try this out?

For all of these questions, you need to be comfortable with the fact that nobody may raise their hand. That’s OK! You can say something like, “Great, it sounds like this is going to be new info for most of you.” Take that as useful information. If nobody says they might try it out, ask why in a friendly way.

Here’s a few pitfalls to avoid:

  • If you’ve got a super quiet audience, don’t feel that you have to force the questions or make them interact with you. It’s OK. Go with what feels more natural to you.
  • Avoid the question, “Does this make sense?” I’ve had to train myself out of this one. It’s heard as a rheutorical question by many people and may just fall flat.
  • Also avoid, “Is anyone confused? / Does anyone not follow me?” Unless you’ve got a super-comfortable, confident, close knit group, most confused people will be shy to raise their hands.

I try to be very open about areas that have been very confusing to me in the past, or which may have stumped me for a while. Don’t force yourself to do this, but if you can get comfortable sharing with your audience what has been hard for you, this may help them get over the fear of “being the one to ask the dumb question”.

Give people an activity

I am a huge fan of challenges, quizzes, and interactive activities in our training classes. I’m always trying to think of new ways that I can engage learners to actively think through problems, because I believe that most people learn better when they get to try to solve a problem.

Kendra Polling the Audience

“Who would like to buy a vowel?”

If you’ve got some presenting experience, you can include quizzes and design activities into your sessions. This does involve some risk taking, because you need to have a way to get people comfortable working together. I like to keep group activities short and give people a clear mission, then meet up again right away as a group to dive back into the class learning, but there’s many ways to do this.

Take breaks. No really, take breaks.

As a presenter, you need breaks. So do your attendees. Getting up and moving around on a regular basis helps people focus. Don’t feel like people are better off if you blast them with learning non-stop: they aren’t!

If you’re presenting with a laptop, you can make it easier for your attendees to relax and laugh during the break. We like to put DBA reactions up on the screen in auto refresh mode during breaks.

Alternate Speakers (if you Can)

We often have multiple speakers for our longer training sessions. This is helpful to us as speakers, but it also helps the audience. Changing up between different people with different presentation styles, manners of speaking, and movement helps people learn actively if you can do it.

Humor is an advanced feature

One of the best ways to keep your audience active is to make them laugh. Laughing wakes people up, gets some fresh air to their brain, and gives the audience a sense of togetherness.

But, uh, this ain’t easy. I’ve had to accept that I’m the funniest when I don’t mean to be. If I want to get good laughs out of the audience, I just need to use some of the methods above to help the audience learn actively. If that’s happening and I’m comfortable being my geeky self, I’ll get some great laughs out of them, and we’re all happy.

You can do this!

Whatever stage of presenting you’re at, you can improve your skills at helping your audience learn actively. If you’re just starting out, it’s ok: first just get comfortable being on stage, then start adding these in gradually.

And if you’re sitting in the audience, wondering what it’s like to be on stage, why not take a chance and try it out?

How to Tell If You’re Good At SQL Server Transactional Replication (Quiz)

Let’s play the quiz! We’re using the honor system: be honest with yourself.

Five Simple Questions

  1. When I say the first name “Hilary”, you immediately know his last name is ____.
  2. Have you successfully set up replication so that you can add and remove articles without running a snapshot for everything, and done this before? (Yes or No). _____
  3. Do you have monitoring set up that notifies you immediately if a subscriber is behind? (Yes or No) ____
  4. How many books on SQL Server replication do you own (which you have read)? _____
  5. Do you know exactly how long it would take to re-initialize all your publications? (Yes or No) ____
Lego builder shutterstock_119038624

This is totally stable!

Score Your Answers

  1. One point if you thought of this man.
  2. One point for Yes
  3. One point for Yes
  4. One point if the answer is 1 or higher.
  5. One point if your answer is “I know exactly how long, and I wish I didn’t know.”

Here’s how to interpret your score:

  • 1-3 points: You’re just beginning, but you cared enough to take the quiz. You can get there.
  • 4 points: You’re good at replication, AND you’re honest ;)
  • 5 points: You’re an expert

Learn More

Ready to learn more about replication? We’ve written a lot about it.

Should I Run sp_recompile After I Create An Index?

Making index changes in SQL Server is tricky: you immediately want to know if the new index helped your performance and if it’s being used, but SQL Server execution plans and their related statistics can be are insanely confusing.

It can be useful to run sp_recompile after you create an index, but not necessarily for the reason you might think.

It’s easier to show this than just write it out.

Let’s Fix a Slow Query By Creating an Index

Let’s say the biggest, baddest query on our SQL Server is from a stored procedure called dbo.kl_RecentlyCreatedAnswers. Our free tool, sp_BlitzCache, calls this procedure out for being our #1 CPU user:

exec sp_BlitzCache @top=5, @results='narrow';
GO

blitzcache before

I can fix that. I design and test this index, and I deploy it to production. It’s totally going to fix this bad execution plan!

CREATE NONCLUSTERED INDEX itotallyfixedit
ON [dbo].[Posts] ([CreationDate],[PostTypeId],[Score])
INCLUDE ([Id])
GO

I’m excited to see how awesome things are, so I immediately run sp_BlitzCache again. But here’s what I see:

blitzcache after

Wait a second. But I just… It hasn’t even changed… What the heck, SQL Server?

Why Is My Terrible Plan Still In Cache?

Creating the index doesn’t cause SQL Server to find related plans in the cache right away and flush them out. My execution plans will hang out in the cache until one of these things happens:

  • The query runs again. SQL Server sees that the schema on the table has changed and decides it needs to reconsider what to do, it recompiles the execution plan*.
  • The old plan gets “aged” out of cache if it isn’t used again (maybe pretty fast if there’s a bunch of memory pressure)
  • The plan is cleared by DBCC FREEPROCACHE, taking the database offline, a SQL Server restart, or a settings change that impacts the plan cache

*The fine print: Books Online lists a list of causes of recompilation here– note that creating an index on the table isn’t necessarily guaranteed by the list. However, the amazing Nacho Portillo recently blogged on this after looking at the source code and indicates that creating an index does flip a ‘schema changed’ bit that should reliably always trigger a recompile. He also mentions that there’s really no way to query all the plans that are still in the cache but are basically ‘invalidated’ due to the metadata change. Sorry, rocket scientists.

But My Plan Is STILL In Cache. Sort Of. Remember When I Said This Was Confusing?

Once the query runs again, I see something different. It did automatically decide to use my new index!

blitzcache after after

Wait a second. Something’s weird. Compare the average executions and CPU for the stored procedure (line 1) and the statement in it (line 2). They don’t seem to match up, do they?

Here’s what happened: the stored procedure ran again. The statement detected the schema change and recompiled. But the *whole* stored procedure didn’t recompile, and it’s showing me stats for 13 executions (not just the 10 since the index change). So my old performance metrics are all mixed up with my new performance metrics. I’m not loving that.

sp_recompile Can Help

Confusing, right? Because of this issue, you might want to run sp_recompile against the stored procedure after making an index change, even if it decided to use it. This forces the whole procedure to get a fresh plan and start collecting fresh execution statistics the next time it runs.

You could also take a heavier hand and run sp_recompile against the whole table, but do that with care: it requires schema level locks and can cause long blocking changes if lots of queries are reading and writing from that table.

Remember: even with sp_recompile, the execution plan stays in cache until it runs again (or is evicted for other reasons). The benefit is just that it will give you a “fresher” view of the execution stats for the whole stored procedure.

Fact: It’s a Little Messy

The main thing to know here is that creating indexes won’t drop or flush plans out, so don’t be surprised if you see old plans in execution plan analysis after you add indexing changes. This isn’t a completely tidy process, sometimes things are a little bit messy.

If you’re actively looking at execution plans in your cache, then running sp_recompile after you create an index can help ensure you’re looking at consistent data. But use it with care and monitor for blocking– don’t leave it unattended.

This example used a downloaded copy of the StackOverflow database. Learn how to get your own here.

Watch This Week’s Webcast Today (and Win a Prize Tomorrow)

We always like to innovate — not just with the solutions we design for our consulting customers and in how we teach, but in our free videos, too.

Our YouTube channel has become super popular. Lots of folks watch the recordings of our live webcasts. We stopped recently and asked, “How can we make this even better for the folks who attend our live event?” And we realized: we can give you more time to ask questions about that week’s training topic!

Here’s your mission:

  1. Watch the video below today. We won’t be presenting this live this week or re-covering the material from the video, we’re doing more advanced QA for the folks who’ve already watched it.
  2. Note down questions or comments you have on this post. (This is totally optional, but it means you won’t forget your question and it’s more likely we have time to talk about it with you.)
  3. Attend the live webcast on Tuesday at the normal time (11:30 am Central). Register here.
  4. During the first 10 minutes of the webcast, we’ll give away a prize– but you must be present to win!

The live discussion of the video and Q&A won’t be recorded and published, and you also need to be present to win the prize. See you on Tuesday!

css.php