Skip to content

Instantly share code, notes, and snippets.

View peschkaj's full-sized avatar

Jeremiah Peschka peschkaj

View GitHub Profile
@peschkaj
peschkaj / match_queries_by_hash.sql
Created March 24, 2014 01:24
This will match SQL Server statements to the appropriate portion of the execution plan based on the query_plan_hash
SELECT QueryPlanHash,
CASE WHEN QueryType LIKE '%Stored Procedure%' THEN
QueryPlan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple/@StatementSubTreeCost)', 'float')
ELSE
QueryPlan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryPlanHash, 3)) = xs:hexBinary(sql:column("QueryPlanHash"))]/@StatementSubTreeCost)', 'float')
END AS cost, *
FROM #procs
Begin selectivity computation
Input tree:
LogOp_Select
CStCollBaseTable(ID=1, CARD=19614 TBL: Person.Address)
ScaOp_Comp x_cmpEq
@peschkaj
peschkaj / find-the-cost.sql
Last active August 29, 2015 14:06
Find the cost and memory grant of queries in the plan cache
SELECT qs.query_plan_hash ,
query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryHash, 3)) = xs:hexBinary(sql:column("qs.query_hash"))]/@StatementSubTreeCost)',
'float') AS cost ,
query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryHash, 3)) = xs:hexBinary(sql:column("qs.query_hash"))]/p:QueryPlan/p:MemoryGrantInfo/@SerialDesiredMemory)', 'float') AS serial_desired_memory,
query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004
/07/showplan";
sum(//p:StmtSimple[xs:hexBinary(substring(@QueryHash, 3)) = xs:hexBinary(sql:column("qs.query_hash"))]/p:QueryPlan/p:MemoryGrantInfo/@SerialRequiredMemory)', 'float') AS serial_required_memory,
*
WITH rando
AS ( SELECT 1 + ABS(CHECKSUM(NEWID())) % ( 4 ) AS RandomNumber
FROM master.dbo.spt_values sv
WHERE sv.type = 'P'
AND sv.number < 4
)
SELECT r.RandomNumber ,
d.number
FROM rando AS r
INNER JOIN master.dbo.spt_values d ON r.RandomNumber = d.number

Keybase proof

I hereby claim:

  • I am peschkaj on github.
  • I am peschkaj (https://keybase.io/peschkaj) on keybase.
  • I have a public key whose fingerprint is FC22 6FC0 6E2F CBA7 2E59 1716 9BDB 3B7E 5BDC F2F8

To claim this, I am signing this object:

if [[ `uname` == 'Linux' ]]
then
export LINUX=1
export GNU_USERLAND=1
else
export LINUX=
fi
if [[ `uname` == 'Darwin' ]]
then
@peschkaj
peschkaj / fabulous-query.sql
Last active August 29, 2015 14:18
A faboulously awful query
IF OBJECT_ID('tempdb..#recent_votes') IS NOT NULL
TRUNCATE TABLE #recent_votes;
ELSE
CREATE TABLE #recent_votes (
PostId INT PRIMARY KEY,
UserId INT,
VoteWeight INT
);
IF OBJECT_ID('tempdb..#posts') IS NOT NULL
@peschkaj
peschkaj / fk.sql
Created May 22, 2015 16:04
Testing foreign key effects on insert performance
USE testkeys;
GO
CREATE TABLE parent (
id INT NOT NULL,
filler CHAR(96) DEFAULT (REPLICATE('A', 96))
);
ALTER TABLE dbo.parent
ADD CONSTRAINT ParentPK PRIMARY KEY CLUSTERED (id);
We couldn’t find that file to show.
tags.each { |tag| tag_list << Tag.first_or_create(:name => tag.strip) }