All posts by Jon Culp

Helpful SQL Server Links

icrosoft White Papers:
https://msdn.microsoft.com/en-us/library/ee410014(v=sql.105).aspx

Microsoft Books Online:
https://technet.microsoft.com/en-us/library/ms130214(v=sql.105).aspx

Pragmatic Works Training Resources:
http://pragmaticworks.com/Training/Resources

SQL Server Database Engine Permission Posters:
http://social.technet.microsoft.com/wiki/contents/articles/11842.sql-server-database-engine-permission-posters.aspx

TechFunda SQL Server Tutorials:
http://techfunda.com/howto/sql-server

Microsoft SQL Server Tutorials:
https://msdn.microsoft.com/en-us/library/ms167593

Microsoft SQL Server Community Projects & Samples:
http://sqlserversamples.codeplex.com/

SQL Pass DBA Fundamentals – Web Presentation Archives
http://fundamentals.sqlpass.org/MeetingArchive.aspx

Blogs and Other Great Resources:
https://blogs.msdn.microsoft.com/sql_server_team/ – MSSQL Tiger Team
https://www.mssqltips.com/
http://blog.sqlauthority.com/
http://www.brentozar.com/
http://www.edwinmsarmiento.com/
http://kevinsgoff.net/
https://www.sqlskills.com/
http://blog.datainspirations.com/
 – BI blog by Stacia

A better list put together by New Horizons:
http://nhlearningsolutions.com/Blog/TabId/145/ArtMID/16483/ArticleID/1186/Free-Resources-for-SQL-Server.aspx

SQL Injections:
http://www.unixwiz.net/techtips/sql-injection.html

Show SP_Who and Rollback Status for All SPIDS Rolling Back

We had a session we had to kill and roll back and wanted some more information about the status of the roll back.  If you only have one SPID to check then you can run the kill commend with an option:

I wanted to expand this by dynamically looking for all sessions in rollback status and then provide some information about the session as well as the kill status.  This way, I do not need to first manually review sessions and look for records in rollback status, then check each kill status.

In my test there was only one SPID in rollback status but it will work if there are more than one.

List SQL Servers On Network

A list of servers can be obtained using windows command prompt (cmd) or powershell (ps).  CMD and PS both provide Server Name and Instance Name, however, powershell provides two additional fields: IsClustered, and SQL Server Version.  One challenge I have noticed is that running these commands from my local machine only returns a short list with names that look like computer names and not server names.  I had to run the commands from a production server, a QA server, and a development server to obtain the server lists from those environments.  There must be some network separation between them, which I am still investigating.

CMD: OSQL -L or SQLCMD -L

PS: [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources()

Check if SQL Server is Clustered

For more information on SERVERPROPERTY(): https://msdn.microsoft.com/en-us/library/ms174396.aspx

Search for Column Name in All Tables in All Databases

Use a text wild card to search for a column name in all tables in each database.  Stored procedure sp_msforeachdb is used to cycle through each database.  The sys tables are used to obtain table and column information.  The output will include Database Name, Schema Name, Table Name, Column Name, and code to easily select the top 10 from a table returned in the result.

Random n% Records – Fastest Method

Method Source: https://msdn.microsoft.com/en-us/library/cc441928.aspx

Row Count on Large Tables Fast

There are ~550 million records in a table I am trying to get a row count on.  Typically I have always used the “COUNT(*)” method to get a row count. I was curious if there is a faster way.  I had read in some forums that “COUNT(1)” or “COUNT(Primary_Key)” could be theoretically faster but there was some argument on the topic.  I decided to run execution plans on all three to see how the query engine is handling it.  According to the execution plan all three operations are treated the same.

row count execution plan

I decided to then prove they are treated the same by running each one three times and taking the average execution time.  Although the results show small differences , this only caused by fluctuations on server load from other processes.   If I took enough measurements they would  converge.

Results:
COUNT(*) = 15.0 seconds
COUNT(1) = 16.3 seconds
COUNT(PK) = 18.0 seconds

Change in Direction

For my purposes I am trying to quickly obtain a count for all tables and databases on a server.  This a large production data warehouse with many databases and hundreds of tables.  I want to minimize the impact to the server and in this case that is more important to me than accuracy.  An alternative method to reading the table directly is to get the statistics from sys.dm_db_partition_stats. This is a Dynamic Management View (DMV) that reads the information about the partition, and by summing it up for a given object you can obtain the row count.  This row count is an estimation.  Any transactions that are occurring would not be captured.

dm_db_partition_stats execution plan

Althought the execution plan looks more complicated, it does not have to read a large table and instead only reads 1 or a handful of records via the system DMV.

Considerations

To stress the point; this is a fast but not necessarily accurate method to get row count.  If you are doing some validation, such as comparing tables, then I recommend using count() and possible checksum().

More Information About dm_db_partition_stats: https://msdn.microsoft.com/en-us/library/ms187737.aspx

VirtualBox

https://www.virtualbox.org/

VirtualBox is a cross-platform virtualization application. What does that mean? For one thing, it installs on your existing Intel or AMD-based computers, whether they are running Windows, Mac, Linux or Solaris operating systems. Secondly, it extends the capabilities of your existing computer so that it can run multiple operating systems (inside multiple virtual machines) at the same time. So, for example, you can run Windows and Linux on your Mac, run Windows Server 2008 on your Linux server, run Linux on your Windows PC, and so on, all alongside your existing applications. You can install and run as many virtual machines as you like — the only practical limits are disk space and memory.

VirtualBox is deceptively simple yet also very powerful. It can run everywhere from small embedded systems or desktop class machines all the way up to datacenter deployments and even Cloud environments.

The following screenshot shows you how VirtualBox, installed on a Mac computer, is running Windows 8 in a virtual machine window:

vm-vista-running