Giter Site home page Giter Site logo

ktaranov / sqlserver-kit Goto Github PK

View Code? Open in Web Editor NEW
2.0K 205.0 641.0 185.16 MB

Useful links, scripts, tools and best practice for Microsoft SQL Server Database

Home Page: http://sqlserver-kit.org

License: MIT License

C# 0.05% HTML 2.19% PowerShell 5.50% Batchfile 0.01% R 0.22% TSQL 91.69% Visual Basic .NET 0.17% Vim Snippet 0.18%
sql-server powershell microsoft-sql r tsql sql-powershell trace-flag awesome-list biml-resources sql-server-database

sqlserver-kit's People

Contributors

abyxez avatar alextitenko avatar andreirby avatar brentozar avatar dbulic avatar denver-ua avatar deweter avatar eshcherbakova avatar fenixfx avatar gitter-badger avatar hanucodes avatar imweijh avatar jbelina-godaddy avatar ktaranov avatar lowlydba avatar mayorovsv avatar miketv avatar misterzeus avatar nedotter avatar osfancy avatar plr108 avatar pmasl avatar protiguous avatar pu-pavlenko avatar segovoni avatar semenenkov avatar sergogga avatar sqlquantumleap avatar thomasjbarrett82 avatar zenzeinet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqlserver-kit's Issues

Add to SQL Server People

New home for "Learning Extended Events in 60 Days"

It seems that the course "Learning Extended Events in 60 Days" has a new landing page (see here). The new link is sqlxevents.com.

That bothers me is the sentence: "... The old methods (bitly and subdomain) will still be valid to point to the old landing page – for a time. ... ". Maybe it is good to add also the new link to the repository.

SSMSBoost / SQL Search

I'm missing some of my favorite tools (all free):

Improve procedure sp_BenchmarkTSQL

Improve procedure https://rebrand.ly/sp_BenchmarkTSQL:

  • Add new column to Bencmark table - FinishBencmarkTime, rename column StartTimeStamp on StartBencmarkTime
  • Rename variable @startTime on @startBenchmarkTime
  • rename all variables to understandable names
    • @r
    • @rts
    • @dateTimeFun on dateTimeFunction
  • Add in Benchmark finished at print Duration of benchmark = @startBenchmarkTime - FinishBenchmarkTime

SQL Server 2012 Diagnostic queries - problem with query - latency per database file

Hi,
on SQL Server 2012 the query that collects the latency per database file, the governor metrics are not working in 2012.
I suggest just removing the following columns:

io_stall_queued_read_ms AS [Resource Governor Total Read IO Latency (ms)], io_stall_queued_write_ms AS [Resource Governor Total Write IO Latency (ms)]

Regards,
Hugo Alhandra

Imrove In-Memory OLTP diagnostic script

https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/In-memory_OLTP_diagnostic_script.sql

  1. Add @debug for printing dynamic sql statements
  2. Replace EXEC (@sql) on EXEC sp_executesql @sql;
  3. Replace DROP IF EXISTS ... on IF OBJECT_ID...
  4. Check and solve another SQL Server 2014 issues
  5. Test script on case sensitive SQL Server 2014 and SQL Server 2017
  6. Transform to stored procedure with parameters:
    • @instanceLevelOnly default value 1, only instance info
    • @dbName default value ALL, list of affected databases
    • @debug, default value 0, just print dynamic sql if @debug = 1
  7. Test script on SQL Server 2014 and SQL Server 2017

Add trace flag 876

From Slava Oks' tweet this week on SQL Server 2019:

https://twitter.com/slava_oks/status/1044257034361757696

With every new #SQLServer announcement it feels like Christmas time of the year, presents for everyone, @NikoNeugebauer with #SQLServer2019 CTP 2.0, could you please try trace flag -T876 instead of -T834 for some of key CS scenarios and check performance?

https://twitter.com/slava_oks/status/1044413511466512384

No -T876 is different beast... it turns 8k page allocations for CS segments into 2MB instead. -T834 does exactly that as well as it pre-commits the entire sql memory with large pages which many workloads don’t really need.

Benchmarking stored procedure

Start: https://blog.jooq.org/2017/03/29/how-to-benchmark-alternative-sql-queries-to-find-the-fastest-query/

Name: sp_BenchmarkTSQL

Parameters:

Trace flags to add

Add to uilities

Update info and add new utilities:

Compute transitive closure of sys.sql_dependencies, find missing objects

https://mssqlwiki.com/2012/05/04/copy-database-wizard-or-replication-setup-might-fail-due-to-broken-dependency/

This script is pure gold. I've repeatedly reached out to Karthik asking permission to host it on GitHub, but he has never responded. It does contain bugs, some of which I've fixed in the comments and others which were left with my former employer when I left.

Some things I fixed:

  • The original script assumed stored procedures could be refreshed in any order. This is only true for trivial stored procedures that don’t reference objects that can’t be refreshed in any order. My fix handles that scenario.
  • My fix explicitly handles stored procedures and/or triggers.
  • Fix the scenario where a schema-bound view is transitively referencing a broken view.

Some fixes not contained in my patch, off the top of my head:

  • Common Table Expressions variables erroneously flag as false positives in this script. At my former employer, we would maintain a lookup table of Common Table Expression variables and generally exclude object names under 3 characters as bogus.
  • Session-scoped temp tables and global temp tables erroneously flag as false positives in this script.

While Karthik uses it to find broken dependencies in the context of copy database wizard or replication setup, I used it after running database migrations to automatically rollback all migrations if the migration broke references/didn't clean up after itself. Some people naively call sp_refreshview or sp_refreshsqlmodule over all sys.objects, but this will fail if you have circular references, which is what Karthik's original script mostly resolves.

Add trace flag 8759

Trace flag 8759 will detect and write part of the query to the error log when it has been autoparameterized.

TF 8726 and 8727

I came across these in an answer from Paul White on TopAnswer.xyz and just realized they are not in your list (which I reference frequently, so thanks for compiling it!).

I've included the info, and repros for both flags. Both repros use the StackOverflow2010 database, and require these indexes to be present:

CREATE INDEX IX_UserID ON dbo.Comments (UserID, PostID);
CREATE INDEX IX_PostID ON dbo.Comments (PostID, UserID);

Trace Flag: 8726
Undocumented trace flag
Function: Disables the heuristic that discourages the optimizer from producing an index union plan
Link: Why are (seemingly) suitable indexes not used on a LEFT JOIN with OR

Repro

Execution Plan: https://www.brentozar.com/pastetheplan/?id=B1mSzXGv8

-- No index union (seek on Users, multiple scans on Comments)
SELECT *
FROM Users u
	LEFT JOIN Comments c
		ON u.Id = c.UserId 
		OR u.Id = c.PostId
WHERE u.DisplayName = 'alex';

-- Gets the index union plan
SELECT *
FROM Users u
	LEFT JOIN Comments c
		ON u.Id = c.UserId 
		OR u.Id = c.PostId
WHERE u.DisplayName = 'alex'
OPTION (QUERYTRACEON 8726);

Trace Flag: 8727
Undocumented trace flag
Function: Can be used to prevent unwanted index union plans
Link: Why are (seemingly) suitable indexes not used on a LEFT JOIN with OR

Repro

Execution Plan: https://www.brentozar.com/pastetheplan/?id=HkwhlQGwL

-- Gets an index union plan normally
SELECT *
FROM dbo.Users u
OUTER APPLY 
(
	SELECT *
	FROM dbo.Comments c
	WHERE 
		u.Id = c.UserId 
		OR u.Id = c.PostId
) c
WHERE u.DisplayName = 'alex';

-- No index union (seek on Users, multiple scans on Comments)
SELECT *
FROM dbo.Users u
OUTER APPLY 
(
	SELECT *
	FROM dbo.Comments c
	WHERE 
		u.Id = c.UserId 
		OR u.Id = c.PostId
) c
WHERE u.DisplayName = 'alex'
OPTION (QUERYTRACEON 8727);

Trace Flag 9288

I don't see it in your list. While it's not documented much anywhere, it does have known effects around local and global aggregates. Here's a demo:

CREATE TABLE tf_9288
(
    Id INT IDENTITY(1, 1) PRIMARY KEY,
    SomeNumber INT
);

INSERT dbo.tf_9288 WITH ( TABLOCK ) ( SomeNumber )
SELECT TOP 1000000 x.n
FROM   (   SELECT ROW_NUMBER() OVER ( ORDER BY @@ROWCOUNT ) AS n
           FROM   sys.messages AS m
           CROSS JOIN sys.messages AS m2 ) AS x;

SELECT SUM(x.c) AS sum_c
FROM   (   SELECT COUNT(*) AS c
           FROM   dbo.tf_9288 AS t
           WHERE  t.SomeNumber < 500000
           UNION ALL
           SELECT COUNT(*) AS c
           FROM   dbo.tf_9288 AS t
           WHERE  t.SomeNumber > 500000 ) AS x;

SELECT SUM(x.c) AS sum_c
FROM   (   SELECT COUNT(*) AS c
           FROM   dbo.tf_9288 AS t
           WHERE  t.SomeNumber < 500000
           UNION ALL
           SELECT COUNT(*) AS c
           FROM   dbo.tf_9288 AS t
           WHERE  t.SomeNumber > 500000 ) AS x
OPTION ( QUERYTRACEON 9288 );

Add Security Audit Report

http://stackoverflow.com/a/7059579/1387418

Script source found at : http://stackoverflow.com/a/7059579/1387418

Security Audit Report
1) List all access provisioned to a sql user or windows user/group directly 
2) List all access provisioned to a sql user or windows user/group through a database or application role
3) List all access provisioned to the public role



Columns Returned:
UserName         : SQL or Windows/Active Directory user account.  This could also be an Active Directory group.
UserType         : Value will be either 'SQL User' or 'Windows User'.  This reflects the type of user defined for the 
                  SQL Server user account.
PrinciaplUserName: if UserName is not blank, then UserName else DatabaseUserName
PrincipalType    : Possible values are 'SQL User', 'Windows User', 'Database Role', 'Windows Group'
DatabaseUserName : Name of the associated user as defined in the database user account.  The database user may not be the
                   same as the server user.
Role             : The role name.  This will be null if the associated permissions to the object are defined at directly
                   on the user account, otherwise this will be the name of the role that the user is a member of.
PermissionType   : Type of permissions the user/role has on an object. Examples could include CONNECT, EXECUTE, SELECT
                   DELETE, INSERT, ALTER, CONTROL, TAKE OWNERSHIP, VIEW DEFINITION, etc.
                   This value may not be populated for all roles.  Some built in roles have implicit permission
                   definitions.
PermissionState  : Reflects the state of the permission type, examples could include GRANT, DENY, etc.
                   This value may not be populated for all roles.  Some built in roles have implicit permission
                   definitions.
ObjectType       : Type of object the user/role is assigned permissions on.  Examples could include USER_TABLE, 
                   SQL_SCALAR_FUNCTION, SQL_INLINE_TABLE_VALUED_FUNCTION, SQL_STORED_PROCEDURE, VIEW, etc.   
                   This value may not be populated for all roles.  Some built in roles have implicit permission
                   definitions.          
ObjectName       : Name of the object that the user/role is assigned permissions on.  
                   This value may not be populated for all roles.  Some built in roles have implicit permission
                   definitions.
ColumnName       : Name of the column of the object that the user/role is assigned permissions on. This value
                   is only populated if the object is a table, view or a table value function.

Add Trace Flag 9198 to doc

Referenced here

Undocumented trace flag 9198 can be used to disable lazy index performance spools specifically. You may still get a lazy table or row count spool instead (with or without sort optimization), depending on costing.

Author incorrect

I (Brett Shearer) did not write the article attributed to me.

usp_BulkUpload: The module 'usp_BulkUpload' depends on the missing object

Do you want to request a feature or report a bug? BUG

What is the current behavior? Problem during installation of the Stored Procedure

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via http://sqlfiddle.com/ run the usp_BulkUpload.sql on a new server installation

What is the expected behavior? the stored procedure should install without problems

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?

SQL Server 2016
Windows10

dbo.sp_DBPermissions.sql third result set ambiguous return order

I have been at my wit's end trying to understand an issue in production where I'm getting a SELECT DENIED error on an UPDATE statement, and it works fine in BETA environment.. so I'm using Ken Fisher's wonderful sp_DBPermissions to try to compare one by one each permission to see if somehow something got deployed incorrectly.

While doing this, I noticed that it's ordered by grantee_name and object_name but NOT permission_name, leading to situations where VIEW DEFINITION and EXECUTE permissions on two different servers might be output in different order.

For my use case, I was copy-pasting the output into EXCEL and running a simple =I3=S3 column sanity check operation.

SSMS Locking up on explorer operations

Do you want to request a feature or report a bug?
Question regarding potential SSMSBoost issue relating to a specific query.

What is the current behavior?
With SSMS Boost installed with SSMS version 17.4, we keep seeing SSMS locking up and a "sp_whoisactive" run shows us that its locked and sitting on this specific query (will be at thee bottom).

The query is essentially the SQL found here below the proc:
https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Table_Parameters.sql

I'm trying to figure out if this query is an SSMS thing, or part of SSMSBoost.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via http://sqlfiddle.com/

What is the expected behavior?

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?
SQL Server 2016, Windows 10. Not sure about previous versions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.