Skip to content

Instantly share code, notes, and snippets.

View NickCraver's full-sized avatar
:shipit:
Shipping

Nick Craver NickCraver

:shipit:
Shipping
View GitHub Profile
@NickCraver
NickCraver / Orion-QueryPlan.sqlplan
Created November 20, 2015 04:12
Orion SQL Plan for SELECT Count(*) FROM InterfaceTraffic
This file has been truncated, but you can view the full file.
<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.2" Build="11.0.5343.0" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementCompId="1" StatementEstRows="1" StatementId="1" StatementOptmLevel="FULL" StatementSubTreeCost="162.511" StatementText="Select Count(*)&#xD;&#xA; From InterfaceTraffic" StatementType="SELECT" QueryHash="0x9DA8413D2DA45AE8" QueryPlanHash="0xDCB2FBECB646294A" RetrievedFromCache="true">
<StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
<QueryPlan DegreeOfParallelism="24" MemoryGrant="51280" CachedPlanSize="512" CompileTime="766" CompileCPU="765" CompileMemory="15912">
<ThreadStat Branches="2" UsedThreads="48">
<
@NickCraver
NickCraver / CurrentQuery
Created January 29, 2014 03:08
SQL 2014 Top Queries DB Filter Test
Declare @MaxResultCount int = 50;
SELECT AvgCPU, AvgDuration, AvgReads, AvgCPUPerMinute,
TotalCPU, TotalDuration, TotalReads,
PercentCPU, PercentDuration, PercentReads, PercentExecutions,
ExecutionCount,
ExecutionsPerMinute,
PlanCreationTime, LastExecutionTime,
SUBSTRING(st.text,
(StatementStartOffset / 2) + 1,
@NickCraver
NickCraver / SwitchTest.cs
Created January 22, 2016 13:58
LinqPad switch implementation test
void Main() { }
public class DbColumn
{
private readonly Dictionary<string, object> _customValues = new Dictionary<string, object>();
public virtual bool AllowDBNull { get; set; }
public virtual string BaseCatalogName { get; set; }
public virtual string BaseColumnName { get; set; }
public virtual string BaseSchemaName { get; set; }
public virtual string BaseServerName { get; set; }

Keybase proof

I hereby claim:

  • I am NickCraver on github.
  • I am nickcraver (https://keybase.io/nickcraver) on keybase.
  • I have a public key whose fingerprint is 9854 D2C3 4BEF 984F 7310 DC97 A163 F138 E6C1 87DF

To claim this, I am signing this object:

@NickCraver
NickCraver / DbColumnReflectionPerf.cs
Created February 8, 2016 23:00
Quick performance comparison of the DbColumn options performance-wise
void Main()
{
var c = new DbColumn() { ColumnName = "test" };
var sw = Stopwatch.StartNew();
for (var i = 0; i < 10000000; i++)
{
var test = c["ColumnName"];
}
sw.Stop();
sw.ElapsedMilliseconds.Dump();
@NickCraver
NickCraver / ForSyntaxHighlights.cs
Last active February 20, 2016 01:09
How to be a jerk using SQL and Dapper
var tb = AppDomain.CurrentDomain
.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run)
.DefineDynamicModule("DM")
.DefineType("Type with spaces in the name", TypeAttributes.Public);
tb.DefineField("Is💩Post", typeof(bool), FieldAttributes.Public);
var spt = tb.CreateType();
this.Connection.QueryFirstOrDefault(spt, "Select 1 as [Is💩Post]").Dump();
@NickCraver
NickCraver / DapperExample.cs
Last active February 21, 2016 20:53
Query Optimization: Literal Example
// Here's a normal query as you'd expect
db.QuerySingleOrDefault<PostCacheEntry>(@"SELECT
(Select Count(*) From Posts Where PostTypeId = @Question And DeletionDate Is Null) TotalQuestions,
(Select Count(*) From Posts Where PostTypeId = @Question And IsNull(IsAnswered, 0) = 0 And AcceptedAnswerId Is Null And ClosedDate Is Null And DeletionDate Is Null) TotalUnanswered,
(Select Count(*) From Posts Where PostTypeId = @Question And AcceptedAnswerId Is Not NulL And DeletionDate Is Null) TotalAccepted,
(Select Count(*) From Posts Where PostTypeId = @Answer And DeletionDate Is Null) TotalAnswers",
new { PostTypeId.Question, PostTypeId.Answer });
// In this case though, PostTypeId.Question and PostTypeId.Answer are values that never change.
// In this case they're constants, but this same trick works for enums.
@NickCraver
NickCraver / CSharp6GetOnlyOverride.cs
Created March 9, 2016 14:23
Example of get-only overrides in C# 6 and fun side-effects.
void Main()
{
var child = new Child();
child.Prop = false;
child.Prop.Dump();
var propertyInfo = typeof(Child).GetProperty(nameof(Child.Prop));
propertyInfo.GetGetMethod().Dump();
propertyInfo.GetSetMethod().Dump();
}
public class Base
@NickCraver
NickCraver / Int32ToBase64.cs
Created May 14, 2016 11:52
Base64 Header Example
void Main()
{
"99999999".Dump();
Int32ToBase64(99999999).Dump();
}
private static readonly char[] EqualsChar = { '=' };
private static string Int32ToBase64(int i)
{
try
@NickCraver
NickCraver / QueryPlan.xml
Created June 5, 2016 12:53
SQL 2016 RTM AG Regression Query Plan
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.5" Build="13.0.1601.5">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementText="CREATE function sys.fn_hadr_is_same_replica(&#xD;&#xA;&#x9;@lag_id uniqueidentifier,&#xD;&#xA; @lag_replica_id uniqueidentifier,&#xD;&#xA;&#x9;@ag_replica_id uniqueidentifier)&#xD;&#xA;returns bit&#xD;&#xA;as&#xD;&#xA;begin&#xD;&#xA; declare @ag_id uniqueidentifier&#xD;&#xA;&#x9; declare @local_replica_id uniqueidentifier&#xD;&#xA;&#x9; declare @ret bit&#xD;&#xA;&#x9; &#xD;&#xA; SELECT @ag_id = group_id &#xD;&#xA; FROM sys.fn_hadr_distributed_ag_replica(@lag_id, @lag_replica_id)" StatementId="1" StatementCompId="3" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="1.157e-006" StatementEstRows="1" SecurityPolicyApplied="false" StatementOptmLevel="FULL" QueryHash="0x48E1F82013EEA15A" QueryPlanHash="0x3443401473380A77" StatementOptmEarlyAbortReason="GoodEnoughPlanF