Posts

Showing posts from 2010

Replication fun: setting NFR on all objects

If you ever tried to set up replication on the large environment with numerous servers, hundreds of databases then you, for sure, spend some time searching/writing scripts to set flag NOT FOR REPLICATION on thousands of identities, constraints and triggers. I want to share here my scripts and I hope it will save some readers hours of headache. Feel free to use them, just add comments below - there is nothing better than feedback. Here we go. ******************************************************************************** 1. Identities. The resutset is a list of identity columns on all tables in all databases where NOT FOR REPLICATION option is not set and fixing script: DROP TABLE #IdentityNFR CREATE TABLE #IdentityNFR (ServerName varchar(250), Dbname varchar(250),TableName varchar(250), IdentityColName varchar(250), IsNFR varchar(10),FixingScript varchar(4000)) exec sp_MSforeachdb ' IF ''?'' NOT IN (''tempdb'',''master'',''mo...

WITH (NOLOCK) still places locks !

Did you know, that when you use READ UNCOMMITTED isolation level or WITH (NOLOCK) hint inside the query, it is still places Sch-S (schema stability) locks during compilation and execution and can be blocked by any concurrent trancastion that that holds a Sch-M (schema modification) lock on the table! So, when using READ UNCOMMITTTED isolation level, keep in mind that READUNCOMMITTED and NOLOCK hints apply only to the data locks and there is nothing you can do with Sch-S lock.

Back to the future: SQL Profiler Trace Replay

Are you familiar with Trace Replay feature SQL Profiler? It's not really popular but actually can do a lot for you. Replay is the ability to save a trace and replay it later. This functionality lets you reproduce activity captured in a trace. When you create or edit a trace, you can save the trace to replay it later. Replaying trace is useful when - you need to troubleshoot production issues then you can run recorded trace against fixed application to make sure all bugs were fixed. - you need to simulate production load on QA/dev environment for stress testing - test new code that is written on development server by simulating real DML load. The process of recording and replaying trace is really easy. - Use preconfigured replay template (TSQL_Replay) because, if you do not capture all required data, SQL Server Profiler will not replay the trace (!) - Save the trace using any format: table or trace file. If you want to use rollover files with limited size take into consideration the...

See all locks that were issued by your query

If you ever wondedered what king of locks your selct query ever issues and for how long, run it in QA environment with isolation level Repeatable Read, which holds all locks you select issues till the end of transaction. Default isolation level Read Committed keeps query locks only till select query has finished running so in most cases you are not able to catch them in sp_lock if your select works fast enough. So, add"SET TRANSACTION ISOLATION LEVEL REPEATABLE READ" inside transaction, then open another window and check out EXEC sp_lock and in some cases you will be surprized how many locks sql server places and removes during simple queries.

Nice SQL jokes

Q: Why do you never ask SQL people to help you move your furniture? A: They sometimes drop the table -------------------------------------- A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?' -------------------------------------- Is you closet is ordered by size, then colour, then style? -------------------------------------- Q1: What did the DBA say to the Developer? A: It doesn’t matter, he wasn’t listening anyway. Q2: What did the Developer say to the DBA? A: It doesn’t matter, the answer was no. ---------------------------------------