Latest Posts

Execution plan operations – scans and seeks

Another post in my ongoing series on reading execution plans. I know I’m jumping around a bit. I hope it makes some kind of sense.

I thought I’d quickly go over the seek and scan operations that can be seen in execution plans. There are 6 main ones. There’s a fair bit that I’m glossing over in this. I’ll get into some details at a later date.

Scans

  • Table scan. This operation only appears for a heap (table without a clustered index). The first page in the heap is located based on info in the system tables, and then the pages are read one by one, using the next and, if necessary, previous pointers in the page headers. This is generally an expensive operation and should be avoided where ever possible
  • (more…)

DateTime Manipulation

The date time data type and the date time functions within SQL are things that I see coming up time and time again in news groups and forums. Questions on how to get rid of the time, how to get the first day of the week, the last day of the month and so on. With the new Date and Time data types coming in SQL 2008, things will get easier, nut until then we have to do things the hard way.

In systems I’ve worked on I’ve seen several implementations of functions to find the first and last day of a week, a month or a quarter. Some have worked well, some have worked and others, well, haven’t

(more…)

Become a better ….

So…

I issued some of my colleagues a challenge this morning.In the vein of the ‘Become a better developer in 6 months’ idea that was going around the net a few months back, I asked them what they were going to do in what’s left of this year to become better DBAs.

Following the idea of ‘make something public and you’ll go through with it’ idea, here are my intentions for the remainder of the year:

  • Finish 2 database books by the end of the year. The two I’m looking at are ‘Guru’s guide to SQL architecture and internals’ and ‘Practical troubleshooting’, The first is written by Ken Henderson, the second is edited by him
  • Write at least 1 blog post a week here. This is the one I’m most likely to have trouble with.
  • Listen to at least 1 SQL related podcast a week. I’m open to suggestions for good SQL-related podcasts.

Memory and SQL 2005 SP2

Or “Why are all my processes waiting on memory. There’s tonnes of memory

It’s probably not new news that there was a fairly nasty memory-related bug in SQL 2005 RTM and SP1 that was related to the relaxing of limits on cache size. Specifically the TokenAndPermUserStore cache.

On systems with large amounts of memory (20GB+) and frequent ad-hoc queries or significant usage of dynamic SQL, the cache can grow quite large, and by quite large I’m talking upwards of 2GB. I think I saw the cache at close on 8GB at one time on one of my servers.

The problem with this is that is takes quite a bit of time to search through several GB of cache to find the required tokens. Making matters worse, access to that cache is synchronised, so only a single thread may have access at a time.

The main symptom of that problem is lots of CMEMTHREAD waits without an apparent wait resource and a higher than normal CPU usage.

But that problem was fixed in SP2 with a change to the caching behaviour. Right?

(more…)

Indexes for aggregates

It’s well known that indexes on columns used in where clause and for joins is a good thing in SQL, but what about other places. How about on aggregates?

Consider a simple table with an amount and a customerID. It’s a common requirement to calculate the total amount that each customer has paid. No conditions are enforced, so this would seem like a place where an index won’t help. Well, let’s see. (sample code at end)

The clustered index (and hence the physical order of the rows) is on the identity column.Take the following query.
SELECT CustomerID, SUM(Amount) FROM Payments group by customerID

(more…)

Execution plans from Profiler

There is another way to get hold of query execution plans than through query analyser/management studio. In SQL 2005, Profiler has a large number of plan-related events. It had some in SQL 2000, but they were quite hard to work with.

The interesting ones in SQL 2005 are as follows, all found under the performance collection in profiler.

  • Showplan XML
  • Showplan XML for query compile
  • Showplan XML Statistics Profile

As soon as one of these is selected, a third tab appears on the trace setup screen, asking for the file to save the xml plans. You can choose to save the plans within the profiler trace file, however the file tends to get very, very large when that is done.

TheĀ  Showplan XML for query compile event fires every time a query is compiled, and it produces an estimated execution plan.

The showplan XML event fires every time a query runs, and it produces an estimated execution plan. When I tested it, it looked as though it was the actual plan, however all the run-time information (actual rows affected, etc) were 0

The Showplan XML statistics profile event fires every time a query runs and produces an actual query plan, with the run-time information in it.

Be careful when running profiler with these events on a busy system as they are quite large, and you can end up with very large output files very quickly. Also, on a busy server they can be very frequent events and capturing them with the profiler front end could result in performance degradation.

Final day

By the last day I was feeling like my head was ready to explode. So much information in so little time.

Kevin Kline’s session kicked off the day with a discussion of benchmarking, baselining and monitoring. Some decent ideas, not really any new information for me. That’s probably more because I’d been at just about every monitoring and performance presentation for the entire conference, than any lack on his part.

After lunch the number of attendees seemed to drop substantially. The best of the afternoon sessions that I attended was Kevin Kline’s interview with Ken Henderson. It wasn’t a technical session, more to do with been an author, a respected sql expert.

The flight home is this evening. I’m not really looking forward to that, I only get back to JHB tuesday morning. Not fun at all.

Day 4 – Monitoring, availability and tough problems

I skipped the keynote on Thursday to spend some time in the SQL Lounge. One of the people there did a demo of a set of scripts, jobs and reports called DMVStat. It’s up on the net somewhere. I don’t have the link right now, but I’ll see if I can dig it up in a day or so.

The first session was on analysing the plan cache. It wasn’t a particularly deep session, just covering how to get execution plans in SQL 2005 (the plan cache DMVs).

The SQL CAT team did a presentation on high availability in the afternoon. Not as good as the session on MySpace, but that would be hard to top.

Bob Ward ran the only level 500 session of the conference, covering debugging difficult problems. The kind of problems that he sees as a senior escalation engineer at PSS. He discusses latch waits, slow IOs, corrupt databases, access violations, memory problems and unexpected shutdowns. It felt something like standing under a waterfall, but it was a brilliant session.

The afternoon wrapped up with a discussion on practical performance monitoring by Andrew Kelly. He went over perfmon, profiler, wait stats, disk stats and showed some techniques for managing the load of data.

All in all, that was a very successful day. One more day to go… 

Day 3

Wednesday at PASS is the first day of the real conference. The day started off with the usual keynote. Ted Kummert of Microsoft went through the data vision that microsoft has, complete with a whole lot of demos.

The part that most caught my eye was the demo of some new SQL 2008 features, including the resource governor with its ability to restrict resource usage depending on properties of the connection (eg application name, host name, login name, etc). The policy-based management should make policy enforcement much easier now, especially since policies can be applied across multiple servers in one operation.

The new spatial data types look cool. I can’t see immediate uses for them myself, but I do like them.

Finally, something that had the entire audience cheering, intellisense in management studio. About time. Something that I also saw but wasn’t mentioned was what appeared to be syntax checking as you type, much like visual studio has. Not sure how far that goes (to objects or just to key words) but it does look interesting.

 

(more…)