Skip to content

Instantly share code, notes, and snippets.

@jdaigle
jdaigle / gist:5781341
Created June 14, 2013 12:10
drop all user objects from a database
declare @n char(1)
set @n = char(10)
declare @stmt nvarchar(max)
-- procedures
select @stmt = isnull( @stmt + @n, '' ) +
'drop procedure [' + name + ']' from sys.procedures
-- check constraints
@jdaigle
jdaigle / shared shared
Last active August 29, 2015 14:01
SSH Local and Remote Forwarding

#Example 1: Local Forwarding

ssh -L *:8081:host:3389 root@gateway -N

This command will listen locally on port 8081 which will forward all connections to host:3389 from the gateway server. This basically lets us access a host or port that only "gateway" can access.

#Example 2: Remote Forwarding

ssh -R *:8081:host:3389 root@gateway -N
@jdaigle
jdaigle / gist:19daf1cd7d1cd4f1a78b
Created July 16, 2014 17:12
get page count and rows per page in each partition
SELECT TableName = object_name(i.object_id)
, IndexName = i.name
, i.type_desc
, p.data_compression_desc
, [Partitions] = Max(p.partition_number)
, [Rows] = Sum(p.rows)
, [Pages] = Sum(au.data_pages)
, [RowsPerPage] = Sum(p.rows) / Sum(au.data_pages)
FROM sys.indexes AS i
INNER JOIN sys.partitions AS p
@jdaigle
jdaigle / gist:2fbfb232475f31b6f393
Created September 24, 2014 15:16
Evict SQL Query Plan
SELECT [text], cp.size_in_bytes, plan_handle
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE cp.cacheobjtype = N'Compiled Plan'
AND cp.objtype = N'Adhoc'
AND cp.usecounts = 1
ORDER BY cp.size_in_bytes DESC;
DBCC FREEPROCCACHE(plan_handle)
@jdaigle
jdaigle / gist:c21fbcc30f771bd676ac
Last active August 29, 2015 14:09
replace project reference paths for Visual Studio projects/solutions
find . -type f -name *.csproj -exec sed -b -i "s/\(ProjectReference.*\)\(\\\\Shared\\\\\)/\1\\\\src\\\\/ig" {} \;
find . -type f -name *.sln -exec sed -b -i "s/\(Project.*\)\(\"Shared\\\\\)/\1\"src\\\\/ig" {} \;
find src -type f -name *.csproj -exec sed -b -i "s/\(ProjectReference.*\)\(..\\\\..\\\\src\\\\\)/\1..\\\\/ig" {} \;
@jdaigle
jdaigle / gist:bdf55889b0935b3f6775
Last active November 1, 2021 22:31
Stats Aggregation and SQL Time Series Database
  • We can implement something like https://github.com/etsy/statsd. This will collect and aggregate metrics.
  • At pre-defined intervals (say... every 10 seconds), these metrics are flushed to a backend store
  • We could use SQL Server to store the metrics:

#SQL Schema

  • Each series is a seperate table containing a timestamp epoch (bigint) and a value (float)
  • Rows are inserted, timestamp is the PK

#Example Query

@jdaigle
jdaigle / gist:281c331e1265425cd1d1
Last active August 29, 2015 14:15
calc worker time for queries
select
qs.creation_time,
qs.last_execution_time
,qs.execution_count
,(CAST(qs.execution_count as decimal(18,4)) / (SUM(qs.execution_count) OVER (PARTITION BY 1))) AS perc_execution_count
,qs.total_worker_time -- microseconds
,CAST(qs.total_worker_time as decimal(18,4)) / CAST(qs.execution_count AS float) as avg_total_worker_time
--,SUM(qs.total_worker_time) OVER (PARTITION BY 1) AS total_total_worker_time
,(CAST(qs.total_worker_time as decimal(18,4)) / (SUM(qs.total_worker_time) OVER (PARTITION BY 1))) AS perc_total_worker_time
@jdaigle
jdaigle / gist:c4e2931e30b0e6c7a839
Created March 30, 2015 13:39
simple tsql counter table
-- CREATE TABLE [dbo].[Counter](
-- [id] [bigint] IDENTITY(1,1) NOT NULL,
-- [date] [date] NOT NULL,
-- [key] [varchar](100) NOT NULL,
-- [count] [int] NOT NULL,
-- CONSTRAINT [PK_Counter] PRIMARY KEY CLUSTERED ([id] ASC)
-- );
BEGIN TRAN
MERGE dbo.[Counter] as t
@jdaigle
jdaigle / gist:c2842e9005c33e5be9ed
Created May 18, 2015 15:34
change line endings for source files
Get-ChildItem -file -Recurse -Filter *.cs | foreach {unix2dos.exe $_.FullName}
@jdaigle
jdaigle / gist:2cbc5517784328b58f4b
Created June 9, 2015 01:43
query tables in mirth database by size
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
, SUBSTRING(relname FROM '[0-9]+')
, d.*
, ch.name
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
LEFT JOIN d_channels d ON cast(d.local_channel_id as varchar(5)) = SUBSTRING(relname FROM '[0-9]+')
LEFT JOIN channel ch ON ch.id = d.channel_id
WHERE nspname NOT IN ('pg_catalog', 'information_schema')