Sql-Server

SQL: Adding many single column SQL Server indexes is usually counterproductive

I’ve just finished delivering a bunch of presentations across New Zealand, and one of the sessions that I’ve delivered in several places is Things I Wish Developers Knew About SQL Server. In that session, I mentioned briefly that most single column SQL Server indexes that people create are at best pointless, and at worst counterproductive.

I often see people making a poor attempt at indexing, and when they don’t know what they need to do, they often add individual indexes on many columns in their tables. This isn’t a good idea.

2018-08-27

Shortcut: Change connection in SQL Server Management Studio

I commonly run into a few connection-related scenarios:

  • I’m working on a large query and need to run it against several servers, not concurrently, but one after the other.
  • I’ve just made a database connection, and got my query ready, only to discover that I’ve connected to the wrong server.

Either way, what I’ve seen people do in these scenarios is to:

  • Select all the text in the current query
  • Copy it
  • Open a new query window
  • Paste the code

That’s all good but SQL Server Management Studio (SSMS) has had a simpler way of doing this for quite a while.

2018-08-23

SDU Tools: Extract trigrams from strings in T-SQL

Fellow MVP Paul White and others have discussed the indexing of small segments of strings, to make fast indexes that I think are somewhat like full-text indexes but potentially more efficient. Three character substrings are common, and are known as trigrams.

I’ve experimented with these at a number of site and seen really interesting (good) results.

In our free SDU Tools for developers and DBAs, we added a table-valued function ExtractTrigrams to help with this. You can pass it a string, and it will pull it apart for you, and return the set of trigrams. This would make it much easier for you to experiment with these types of indexes.

2018-08-22

Opinion: Don't have blind faith in hardware

There was a discussion the other day (on one of my lists), where someone pointed out that over many years, they had made tape backups, sent them via secured transport, and then paid for high-tech storage. And when they went to restore one of the tapes recently, there was nothing on the tape.

Over the years, I’ve lost count of the number of times I’ve heard stories like this. Long ago, I realized that you must never trust hardware.

2018-08-21

SQL: Make sure to use ORIGINAL_LOGIN when auditing

I regularly see code where SQL Server DBAs and developers are trying to log which user/login took a particular action within the database. There are many functions which appear to return the information needed, but there’s only one that should normally be used: ORIGINAL_LOGIN().

A login is the way that a connection is authenticated to the server ie: it’s the “who are you?” at the server level.

Most times, a user is a mapping of that login to a particular database.  The login and user will often have the same name (and I’d recommend that you do that to avoid confusion) but they do not have to be. A login Terry could be a user Mary in one database and a user Nga in another database.

2018-08-20

Shortcut: Viewing client statistics in SQL Server Management Studio

While SQL Server is quite fast at executing queries, when you are connecting from a client application like SQL Server Management Studio (SSMS), you might wonder how much time SQL Server spent executing the query, as opposed to how long the communication with the server took.

This type of information is available in the Client Statistics.

Let’s see an example. If I connect to a server in an Azure data center, I’ll have higher latency than for one in my own site. That will affect the wait time for a server response.

2018-08-16

SDU Tools: Extract Trimmed Words from T-SQL Strings

Occasionally I’ve needed to take a string, and extract all the words out of it. For example a string like ‘hello        there     greg’ might lead me to want the three words ‘hello’, ’there’, and ‘greg’. Note that I usually want them trimmed, not just extracted.

In our free SDU Tools for developers and DBAs, we added a table-valued function ExtractTrimmedWords to help with this. You can pass it a string, and it will pull it apart for you, assuming that you have whitespace separating the words.

2018-08-15

SQL: The T-SQL SIGN function and what's in a return type?

When you’ve worked with a product like SQL Server for a long time, and more importantly, are one of the odd people who’ve read a great amount of the documentation simply for interest, it feels really strange to come across a basic function that you’d never noticed before. That’s how I felt when someone mentioned the T-SQL SIGN function.

I thought, “the what function??”.

Now it works pretty much as you’d expect. It returns:

2018-08-13

New online on-demand SQL Server courses from SQL Down Under

Hi Folks,

We have a whole series of online and on-demand courses coming. The first two of these are available right now.

The good news? The first one is free and the second one has a big introductory discount.

The first course 4 Steps to Faster SQL Server Applications is a short course for developers, new DBAs, and testers, etc. who don’t know anything much about tuning SQL Server applications. It focuses on finding and fixing the most problematic queries, either in terms of index tuning, or removing repetitive queries, all using free tools.

2018-08-10

Shortcut: Adding additional parameters to connections in SSMS

When I am writing my own code using a .NET (or other) language, I have a great deal of control of how the connection string that my application uses to connect to SQL Server is configured.

In particular, I might need to add another parameter or two.

As a simple example, you might have a multi-subnet Availability Group, spread across a production site and a disaster recovery site. It’s common to then have an Availability Group Listener in both subnets.

2018-08-09