This past Tuesday, Oct 8th, 2014, I had the privilege of speaking for the DBA Fundamentals Virtual PASS Chapter. It was my biggest audience to date and the fourth biggest audience for the user group to date. There were 374 people listening in, and they weren’t all from the US. I was thrilled to see that I had at least one person listening in from the UK.
Since there were so many people on line, I couldn’t answer all the question…thus my post today. You can download the slide deck, demos, and my sample database. (All my presentations eventually find there way to my resources page, here.)
I will post the link to the presentation on this page when it becomes available.
1. What is the name of the Red Gate tool for source control you mentioned?
Red Gate’s tool is called SQL Source Control. It’s like a bridge between your source control repository and SSMS or Visual Studio. They have a trial version that you can download here
2. What to do when you come into a team and there was no SQL coding standards before and we do not want to spend time re-writing everything?
I would first get buy in on why standards are important to have and outline the standards you want to use. Then I would slowly change the code. What I mean by this is, update the code as you make changes to it. For instance, you need to modify stored procedure XYZ, I would add the standards to only that stored procedure or even only the part of the stored procedure that you change.
3. What version of SSMS are you using?
In the presentation I was using SQL 2012, but all the concepts I covered applied to SQL 2005, 2008 and 2008 R2. Even the Template Browser and Template Parameters I covered are available in those versions (Talk about a well kept secret!). Note: If you need the sample database in SQL 2008 or SQL 2008R2, please let me know and I’ll see what I can do.
4. Can you give an example of commenting within a stored procedure?
Sure. You can comment using two different ways. You can comment at the end of a line using two dashes. You can also comment any where in the code using /* and */ to encompass the comment. This allows for comments to spend multiple lines.
When I’m adding comments about my changes, I will include the date, my name and brief comment. If I’m lucky to have a work item or bug tracker application to keep track of my work, then I’ll reference that number as well. Here is an example.
Purpose: Returns list of Territory Groups.
(Change Date) (Author) (Description of Change)
10/10/2014 Mickey Stuewe #2345 Added All to the list of values as requested
By end user.
ALTER PROCEDURE List.GetTerritory
SET NOCOUNT ON;
,[Group] AS CountryRegion
,st.Name AS Territory
AdventureWorks2008R2.Sales.SalesTerritory AS st
CountryRegion; --Added the ALL to the list.
@ErrorMessage AS nvarchar(3000)
,@ErrorSeverity AS int;
SET @ErrorMessage = ISNULL(DB_NAME(DB_ID()) + N'.' + SCHEMA_NAME(SCHEMA_ID()) + N'.'
+ OBJECT_NAME(@@PROCID, DB_ID()), N'SQL Object Name Not Available')
+ N': Error: ' + CONVERT(nvarchar(10), ERROR_NUMBER()) + N' Line: ' + CONVERT(nvarchar(5), ERROR_LINE()) + N' - ' + ERROR_MESSAGE();
SET @ErrorSeverity = ERROR_SEVERITY();
RAISERROR(@ErrorMessage, @ErrorSeverity, 1);
SET NOCOUNT OFF;
5. Is there any configuration for the Template Browser? Can it integrate with any source control?
No configuration for the Template Browser is needed. Here are the generic steps to integrate your Template Browser folder with your source control repository. Each Source Control repository will be different, but these are the general steps.
1. Open the location of the Template Browser. Since I’m using SQL 2012, I found mine here:
C:\Users\<Your User Name>\AppData\Roaming\Microsoft\SQL Server Management Studio\11.0\Templates\Sql\<My Templates>
2. Import your templates into your source control repository.
3. Set your templates folder as the “working folder” location.
4. Test by getting the latest from your repository.
5. Connect the template browsers on your team’s computers the same way.
6. Let them know when you make changes to the templates so that they can do a “get latest” to download the changes to there computers.
6. Is there any kind of Freeware repository tool?
Yes, Subversion is an open source product and widely used repository tool. It has two parts. The first part is the repository which resides in a common location, usually on the network so that everyone can use it. Then each user needs to have a subversion client tool to connect to the repository. There are many different clients to pick from. I use Tortoise. Why? It was suggested to me and recommended by Red Gate. Since I was going to use SQL Source Control by Red Gate to connect to subversion, I decided to stick with there recommendation.
7. So, Row_Number() is non-deterministic?
Yes, that is correct.
8. Is there any difference or improvements in using a CAST vs CONVERT or vice versa?
There isn’t a different in using the two functions, in terms of performance. As Jeff Joy kindly pointed out, the difference between the two comes down to compliance.
Jeff Joy – CAST is ANSI – compliant while CONVERT is Microsoft-specific casting function that builds in additional formatting functionality. If you may ever need to convert to another platform you need to write code that is cross-platform compliant, use CAST.
The downside of writing all of your code to ANSI compliant standards, is you miss out on quite a few rich features that Microsoft has added to the SQL language. These rich features will help SQL statements perform faster or make your life easier.
9. Isn’t CTEs much better than subqueries? This is in regards to readability?
Yes CTEs (Common Table Expressions) are much easier to read. There are even patterns that can be accomplished with CTEs that can’t be accomplished with sub queries, but CTEs aren’t always faster. I have had a few queries I’ve had to re-write with sub queries because I needed every millisecond of performance.
10. Can I see the trace options selected?
Yes, here are the events I captured. I also isolated the database, to the demo database.
11. Is there a standards document about T-SQL Formatting?
I don’t have one I can give you. I can create a template for you if you would like. Just send me an email.
12. Can you e-mail that URL to us?
I’m not sure which URLs this is referring to, so I’ve listed the URLs that were mentioned in my presentation.
13. Is there better performance between CTE and Apply?
Comparing CTE and APPLY is like comparing apples and oranges. They are two different things. A CTE provides you the ability to create a query to be used within another query. APPLY is an operator like INNER JOIN, but it is specifically used to join table functions (in line and multi line) as well as sub queries and CTEs.
14. On the comparison of temporary, table variables, and materialized tables…Why was the second execution plan eligible for auto-parameterization and the others were not?
This will take a little bit of investigation since I don’t know off the top of my head. I will post this one in the next week or so.
15. What is the proper way to use DISTINCT with lots of columns in the SELECT without duplicate records? (Per our SDLC, avoiding using DISTINCT with lots of columns.)
The DISTINCT operator causes a SORT in the execution plan which is usually a very costly operator. Unfortunately sometimes you can’t avoid it. One thing that you can do, is see if there are other ways to filter the data so that there aren’t duplicate records. For example maybe there is a column that marks the latest record as active. You could filter on that field to provide uniqueness across the rest of the fields. Another example would be joining to another table that will help reduce the records, but not alter the data you need to return.
IMHO, your SDLC is trying to avoid unnecessary DISTINCT operators in the code. I had added that same requirements to my SDLC document when DISTINCT was being abused. I later added the comment, “If you can prove the need for the DISTINCT operator, then the use of DISINCT will be approved.”
16. About predicate and especially LIKE, we know that to put predicate more efficient, we shouldn’t start with a wildcard, but if we need to return data that end by defining string, it exists a way to improve that?
See # 17 below.
17. REVERSE string?
I wanted to answer 16 and 17 together. #18 is related, but I answered it separately. While all the string functions are deterministic, the particular pattern that needs to be implemented (looking at the end of string), does not allow it’s self to an efficient execution plan. This has to do with how indexes are built. The keys in the index are stored sorted. This means that finding records can only be efficient when looking from left to right, not right to left when comparing strings. You can see this by running the following sample on my demo database I provided.
I created an index on the field we are looking at, then I tried various techniques to get the last three characters. All three execution plans show an Index Scan.
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'dbo.TestData') AND name = N'IX_TestData_SomeDateText')
DROP INDEX IX_TestData_SomeDateText ON dbo.TestData WITH ( ONLINE = OFF )
CREATE NONCLUSTERED INDEX IX_TestData_SomeDateText ON dbo.TestData
dbo.TestData AS td
td.SomeDateText LIKE '%-09'
dbo.TestData AS td
REVERSE(SomeDateText) LIKE '90-%'
dbo.TestData AS td
LEFT(REVERSE(SomeDateText),3) = '90-'
When you run this sample, you do get an Index seek.
dbo.TestData AS td
SomeDateText LIKE '2004%'
With all that said, if a particular column is constantly being split into separate parts to be searched, then I would consider storing those separate parts in fields in the database. That way they can be indexed and improve performance.
18. Full Text index vs Like?
I have not had the opportunity to use or even investigate Full Text indexes, so I can’t comment on this at this time. (sorry)
19. Is it ok to manually create tables in the temp DB? Would that be the same as creating materialized tables, but will be wiped out after a DB server restart?
Temp tables and global temp tables are tables with in the TempDB. As far as creating materialized or permanent tables within in TempDB, that is not a good idea. TempDB can already be burdened by all the other queries that use temp tables, table variables, and worktables. It would be better to create materialized tables outside of the TempDB.
20. For the Template explorer…how do you share your templates to other users in SQL because when we create templates they seem to only be available to the user specifically that created them?
See Question #1.
21. What’s the best hosting solution for MSSQL server standard/enterprise?
This is outside the scope of this session and an area I don’t have a lot of experience with.
22. Does including Actual Execution plan also puts an overhead on the server?
No. The Actual Execution plan is what is created when a query is ran. The query itself can cause overhead, but not the plan. In fact, the engine allows a specific amount of time to find the best plan. If it can’t go through all the different possibilities, then it will do the best it can with the possibilities it ran through . You can see this in the execution plan. Click on the SELECT icon and look at the properties. For this query, it told me it found a “good enough plan”.
If you want to get the last execution plan that was generated for a query, then you can get it using sys.dm_exec_query_plan. See how to use this DMV here.
Last but not least…
23. What’s your favorite whisky?
Well in the immortal words of Grant Fritchey…that depends. It depends on where I am, what I’m eating, and how many people I’m with. (I know, that is a weird one. But I find I can handle the smokier Scotchs when I’m around a group a friends, but not when I’m home relaxing.) Also, I drink all my whiskey neat.
So, since I don’t have one favorite, I’ll answer the question by country in the order of my preference.
- Ireland – I don’t think I’ve met an Irish whisky I didn’t like, but my favorite (right now) is Redbreast.
- USA – I love 100% Rye whiskey! Mitcher’s Rye and Highwest Rendevous Rye are at the top of my list.
- Scotland – I like the Fruity and Spicy Scotch’s from Speyside, with Oban being at the top of list.
But wait there is more
Mark Finch mentioned a free formatting tool. I wanted to include it in my summary since I had never heard of it. You can find it at poorsql.com.
Thanks for all the fish
I want to thank everyone for attending my presentation, as well as for all the encouraging feed back I received.