Geek City: Format STATISTICS IO Output

I was in Wrocław Poland a couple of weeks ago, delivering a precon and two regular sessions at the SQLDay conference. I also got to attend a couple of sessions while I was there and I learned some new things. 

One thing was how cool the new Spotlight Presentation Remote from Logitech is. Two different presenters were using it. It allows you to magnify a section of your screen even when you're not at your computer. Just point to the big display monitor, press a magic button, and whatever area you've pointed to is enlarged, so you can call out details. So I ordered one before I got home and it was waiting for me. The problem is, I don't have another conference scheduled until the end of July at SQL Saturday Sacramento, so I won't be able to play with this new toy in front of an audience for almost 2 months!

One other new thing I learned about was a cool utility called Statistics Parser, built and maintained by Richie Rump (blog | twitter) and the fine folks at Jorriss LLC.

The tool lets you paste the output from STATISTICS IO into a text box on the web page, and then it displays the information about each of the tables accessed in a very nice readable format.  It works with multiple queries, and will give you the STATISTICS IO from each query, and then a total for all the queries. The only thing I wished it did differently was to not show the totals section if there is only one query, because then the two output sections are just duplicates. 

Here's an example. I wanted a query with lots of tables, so I found the Sales.vIndividualCustomer view in the Adventureworks2016 database that is a join of 10 tables. When I query the view looking for email addresses for people named 'Jim' or 'Jimmy', all 10 tables are accessed. (This query also works with Adventureworks2014.) 

SET STATISTICS IO ON;
GO
SELECT  *
FROM    AdventureWorks2016.Sales.vIndividualCustomer
WHERE   EmailAddress LIKE 'jim%';
GO

The STATISTICS IO output in the results looks like:

Table 'PhoneNumberType'. Scan count 1, logical reads 53, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'PersonPhone'. Scan count 26, logical reads 52, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'AddressType'. Scan count 0, logical reads 52, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'CountryRegion'. Scan count 0, logical reads 52, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'StateProvince'. Scan count 0, logical reads 52, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Address'. Scan count 0, logical reads 61, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'BusinessEntityAddress'. Scan count 26, logical reads 72, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Person'. Scan count 0, logical reads 87, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Workfile'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Customer'. Scan count 1, logical reads 123, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'EmailAddress'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

So I pasted the above into StatisticsParser, and got the following: 

StatisticsParser.jpg

Actually, I got the above image twice, once for the query, and once for the total. The StatisticParser can also parse and format STATISTICS TIME output if you capture that as well. 

Way cool, right?

Have fun!

Ambigram.jpg
 

Here I GO again!

The very first SQL Server article I ever published, in the old SQL Server Professional journal from Pinnacle Publishing, was about the use of GO. Twenty-five years ago, there was confusion about just what GO was, and there still is confusion. So maybe I should just post something like this once a year, whether I think people need it or not. (There are other things I feel I need to repeat, or even shout from the rooftops, that I see over and over... like CASE is an EXPRESSION, not a STATEMENT, but this post really isn't the place for that. :-) )

So what is GO? What is it NOT? Is is NOT a Transact-SQL keyword or command. In the current documentation it even says "GO is not a Transact-SQL statement".  But how often do people look up the documentation for GO? If you did, you'd also see that GO can take an argument, an integer that indicates how many times the preceding batch is to be executed. According to an old blog post of mine, this integer <count>  didn't used to be documented, but now it is. But if you never read this documentation, what does it matter? 

So GO is a command to the tool that you are using, and not every tool recognizes GO. SQL Server Management Studio does, in the query editor window. It's used to separate batches. So you need to know what a batch is. I usually define 'batch' as a unit of communication from the client (SSMS, in this case) to your SQL Server instance. You can send one statement, or multiple statements, all in a single batch. And the GO is not ever seen by SQL Server. It just receives the commands and statements before the GO. But if there is a compile-time error (like a misspelled keyword or a missing parenthesis) anywhere in the batch, none of the statements in the batch will be executed.  And there are rules about what can and cannot be combined in a batch, but this post is not the place for all those rules.  

Since GO is only recognized by the tool you are using, the tool has some control. In SSMS, you can actually change the batch separator to not be GO. Under the tools menu, choose Options and you'll see this dialog. In the left hand list, choose Query Execution|SQL Server|General, and you'll see the place to replace GO with some other string of your choosing. Devious tricksters have even suggested replacing it with SELECT on someone else's SSMS installation, if you're not interested in having that person for a friend. 

GO.jpg

I'm sure there is more I could say about GO, but I'll say that for tweets, or next year's post on this same topic. 

~Kalen

Did You Know: Windows Fast Startup is not really a StartUp!

So you might already know, but I didn’t know, until I learned it, of course.

My first Windows 8 machine was my Surface Pro 3 and I LOVED the way it started up so FAST. Fast is good, right? I didn’t even bother to wonder WHY or HOW it was so fast. I just thought Moore’s Law was hard at work.

But then I noticed something very strange after I started doing most of my SQL Server testing on my Surface. Sometimes cache didn’t seem to be cleared. Sometimes temp tables would mysteriously be there right after starting up. Memory consumption was way too high. What was going on?

Then I found it. The reason Windows 8 (and now 10) can start up so fast is that they’re really not shutting down completely. There’s an explanation here:

http://blog.gsmarena.com/windows-8-to-have-a-hybrid-shutdown-boot-times-as-fast-as-8-seconds/ 

One of the things it says is:

Instead of saving everything, Windows 8/10 saves just the OS kernel in a file on the hard drive and then uses it to while booting up, speeding the whole processing considerably.

And then there is an illustration that indicates that one of the things that gets started in a cold boot that doesn’t get started in this fast boot is services. And SQL Server is a service. So when I think I’m shutting down Windows, which includes shutting down SQL Server, I’m really not. The SQL Server service, with all the temp tables, plan cache, data cache and memory, is saved and then restored.

Yeah, fast is good, but it’s not always what I want. If I’ve already started up and I really need to restart SQL Server, I can just restart the service from Management Studio. I even created a stored procedure to quickly tell me my approximate start time, so I can know if it’s been just a few minutes, or actually days since my SQL Server was last started:

USE master
GO
CREATE PROC dbo.sp_starttime as
SELECT create_date FROM sys.databases
WHERE name = 'tempdb'; 
GO 

Then I can just  execute sp_starttime to see when my last restart was.

As an alternative to stopping and restarting the SQL Server Service, I could do a Windows Restart. That option, as opposed to Windows Shut down, will actually stop everything, including all services.

At first, I was so shocked that Windows really wasn’t shutting down when I asked it to, I wanted a way to turn it OFF. So I found this article:

http://mywindowshub.com/fast-start-up-in-windows-8/

It says to go to Power Options and Choose What the Power Button Does.  You’ll get a screen like this one:

fast startup.png

 

The instructions just say to uncheck the box next to Turn on fast startup, but I found I wasn’t able to do that; the checkbox was inaccessible (greyed out). I finally found a really tiny message up near the top, that I have pointed to with a red arrow, that I needed to click on to enable the checkboxes. Then there is a Save changes button at the bottom, which I didn’t capture in the screen above.

I did that. And lived with no fast startup for a while. But then my Surface broke, and I had to get a new one (yes, it was covered under my service agreement.) But after setting up the new machine, which came with the fast startup as default, I realized I had missed it. So for now I’m leaving it on. I just remember to restart the SQL Server service before I start any testing.

Enjoy!

~Kalen

Geek City: Intent to Update

In the last couple of posts, I've told you about Intent Locks and UPDATE locks. I want to just provide a wrap up to those posts that talks about both of these aspects of locking.

You can think of both of these locking aspects as although SQL Server to indicate an intention. However, Intent Locks have to do with the unit of locking and UPDATE locks have to do with the type of lock. 

Here's how you might think about the 'intention':

If you get individual rows locks, you might have the intention of getting more row locks on the same page or table, so you might eventually want to lock the entire page or table. To make sure the page or table is available when you're ready to lock it, SQL Server acquires an Intent lock on the larger units. If you have a row lock, you'll get Intent locks on the page and the table containing that row. Intent locks can go with any type of lock: we can have Intent-Shared (IS), Intent-Exclusive (IX) and even Intent-Update (IU).

UPDATE locks are a way of SQL Server stating your intention to change the type of lock. If you are searching for data to modify, you might intend to eventually get an X lock. So you get U locks while you're searching (instead of S locks)  and then X locks when you find the data to modify.

You might also want to note that although you can get an IU lock on a page, at the table level an IU lock becomes IX. You can see that in the output I showed you in my UPDATE lock posting, which I repeat here, with a couple of extra callouts for emphasis:

figure 1.png

 

Session 64 is requesting a U lock on a key, and it already holds an IU lock on the page that contains the key, but an IX lock on the object.

I'm thinking that soon I'll write a post about interpreting some of the other information that shows up in the sys.dm_tran_locks view, for example the entity and the resource_description.  But not today…

Have fun!

~Kalen

P.S. And for LOTS more details on how to deal with and troubleshoot Locking and Blocking, I'm offering a half day class on June 14! 

https://www.sqlserverinternals.com/events/2018/3/28/online-class-sql-server-concurrency-control

Geek City: What gets logged for index rebuild operations?

This blog post was inspired by a question from a future student. Someone who was already booked for my SQL Server Internals class asked for some information on a current problem he was having with transaction log writes causing excessive wait times during index rebuild operations when run in ONLINE mode. He wanted to know if switching to BULK_LOGGED recovery could help.

Read More

Geek City: What do you intend with that lock?

Way back in the olden days, prior to SQL Server 7, I already knew that the lock manager was one of the most complex and resource intensive parts of SQL Server. Keeping track of every lock held, who was waiting for that lock, who could be granted the lock, and who was next in line for the data if the lock was released was a lot for SQL Server to keep track of. I admit, I was worried about the promised feature of row-level locking, thinking that it was going to be orders of magnitude more expensive. Without going into all the details of the complete rewrite of the locking implementation for SQL 7, let’s just say that my fears were quite exaggerated.

Read More

Geek City: sp_cacheobjects for SQL Server 2017

Several times over the last decade, I've posted a version of a view that lists all the compiled plans in SQL Server's plan cache. It's based on the old pseudo-table syscacheobjects, returning basically the same columns with extra filters added. Over the versions, Microsoft keeps showing me more and more internal operations in the plan cache, that usually just clutter up the output, so I keep having to add filters to get rid of the unwanted rows.

Read More

Geek City: Changing How To Change Your Database Properties — ALTER DATABASE

Long ago (in SQL Server years) many metadata changes were implemented with special one-off stored procedures. For example, we had sp_addindex and sp_dropindex, as well as sp_addtypesp_droptypesp_addlogin and sp_droplogin. For changing certain database properties, we had sp_changedbowner and sp_dbcmptlevel, to name a few.

Gradually, Microsoft has started replacing these procedures with the more generic DDL commands ADD, DROP and ALTER. This is both good news and bad news.

Read More