Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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 / 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: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 / gist:5548204
Created May 9, 2013 15:31
Show Service Bus Statistics
SELECT
q.name AS Queue_Name
, s.name AS Service_Name
, p.rows AS Row_Count
, (p.rows / 2) AS Estimated_Message_Count -- divide by two because our framework always sends an "END CONVERSATION" message with each real message sent
, COALESCE(f.Failed_Message_Count,0) AS Failed_Message_Count
-- other interesting things
--, q.is_enqueue_enabled
--, q.is_receive_enabled
@jdaigle
jdaigle / gist:5459517
Created April 25, 2013 13:02
query, spaced using in database for each table
SET NOCOUNT ON
DBCC UPDATEUSAGE(0)
-- DB size.
EXEC sp_spaceused
-- Table row counts and sizes.
CREATE TABLE #t
(
@jdaigle
jdaigle / gist:5459403
Created April 25, 2013 12:40
Show Index Stats based on Usage
SELECT
o.name
, indexname=i.name
, i.index_id
, reads=user_seeks + user_scans + user_lookups
, user_seeks
, last_user_seek
, last_system_seek
, user_scans
, last_user_scan
-- Note: querying sys.dm_os_buffer_descriptors
-- requires the VIEW_SERVER_STATE permission.
DECLARE @total_buffer INT;
SELECT @total_buffer = cntr_value
FROM sys.dm_os_performance_counters
WHERE RTRIM([object_name]) LIKE '%Buffer Manager'
AND counter_name = 'Total Pages';