Latest Posts

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…)

Precon – Query plans

The second day pre-conference that I attended was by Kalen Delany, all about query plans.

The first part of the session was an overview of the various methods of getting a query plan, from the showplan options for estimated plans, to the profile options for actual execution plans, the graphical options and the usage of SQL Profiler to get both actual and estimated plans.

She then briefly covered sub-optimal plans, without going into detail on query tuning. Stuff like cardinality estimation errors and potentially slow operators (scans, sorts, hashes)

After lunch we delved into details on the plan cache, including what constitutes a plan, how to view them and what conditions there are around plan reuse. This covered adhoc plans, prepared plans and object plans (stored procedures), as well as recompiles and the downsides of plan reuse.

Finally there was a section on query hints and plan guides, for use when the optimiser just won’t do what you want it to do.

The evening was a great deal of fun, with the opening reception and the SQLServerCentral party. I had the opportunity to take part in the quiz bowl. Got eliminated in the first round (damn movie questions) but was still good fun. Won a couple books. More reading material is always a good thing.

Preconference – Performance Toolset workshop

Spent the first day of the conference at the PSS Bootcamp. The PSS guys always put on a good show as they take people through what they do to solve customer’s problems.

The first part of the day was devoted to a performance tuning methodology. What do you do when the users are complaining that the server’s slow. The presenter went through the methodology that the PSS engineers use when presented with a performance problem.

Most of the process is aimed at finding the problem query or identifying a resource bottleneck on the server.

If the problem is currently occurring, one of the main tools is the performance dashboard, a new report introduced into Management studio with SQL 2005 SP2

If the problem is not currently occurring, then it’s necessary to use SQLDiag, profiler, perfmon or a combination of them. A very interesting new tool that they introduced is a data aggregation and reporting tool for performance data – SQL Nexus. The updated version is supposed to be available by end November.

The session finished with a brief look at some of the new features of SQL 2008 that would help out with performance issues. One of the big ones, at least for me, is the performance warehouse. SQL can be configured to collect performance related data continuously in the background and save that into a data warehouse. There are a collection of reports built into management studio that report off this data. Used properly, that should make finding performance problems much easier than currently.

The other feature in 2008 that looks fantastic – a dependency checker that actually works. Sounds great 

Off to PASS

Well I’m off to PASS tonight. total of 18 hours of flying and 7 or so hours sitting around in London Heathrow airport. What fun.

Looking forward to the conference. Hopefully I’ll get a chance to chat with some people I met last year.

I’ll probably be reporting on some of the sessions while I’m there. If there’s anyone who reads this blog that’s going to be at pass, look me up and say hi. Just look for someone wearing a nametagĀ  with the name ‘Gail’ and country ‘South Africa’

On a haunted house

The second session of the haunted house adventure went down far better than I could have ever hoped. In fact, the players asked to stay late so that they could finish it, they were having so much fun.

They survived the haunted house and uncovered the reason behind all the strange occurrences. they couldn’t prevent a thug from making off with the knife that had been the focus of all the strange events, but that’s fine. It adds possibilities for the future.

Everyone was enthusiastic, interested and most importantly, involved in the story. I’m still on a bit of a buzz from the game and I’m very psyched for the campaign.

Next up, depending on the players, either investigating the happenings at the cathedral, visiting a museum exhibit, or attending the cultural festival.

Shrinking databases

Or “Order the pages, shuffle the pages.

Do you ever shrink your data files? I’ve personally never been fond of it, especially for production databases. After all, they’ll simply have to grow again and, especially if the data files are on independent drives, there’s little difference between space free on the drive or space free in the data file. There is also a more insidious reason for not shrinking a database.

Let’s take a very simple database (The creation code is at the end of the post). I have two tables, both with a tens of thousands of rows. Both tables have a clustered index on a uniqueidentifier and are heavily fragmented (>99%).

DBCC SHOWCONTIG(LargeTable1) -- 99.30%
DBCC SHOWCONTIG(LargeTable2) -- 99.21%

To fix the fragmentation, rebuild both indexes. That fixes the fragmentation, but now the data file is using almost twice the space necessary.

DBCC ShowFileStats -- 3363 extents total, 1697 used (215 MB total, 106 MB free)

So, shrink the database to release the wasted space back to the OS

DBCC SHRINKDATABASE (TestingShrink, 10) -- Shrink to 10% free

That’s fixed the space issue. But now, have another look at those two indexes that were just rebuilt.

(more…)