Skip to content

Instantly share code, notes, and snippets.

View kmatt's full-sized avatar
😐

Matt Keranen kmatt

😐
  • SE US
View GitHub Profile
@kmatt
kmatt / dm_execs.sql
Last active August 29, 2023 01:59
SQL Server execution status
SELECT R.session_id, DatabaseName = db_name(R.database_id), S.text, R.Status, R.Command,
R.percent_complete,
CAST(CAST(DATEADD(mi, R.total_elapsed_time/60000.0, 0) AS TIME) AS CHAR(5)) runTime,
CAST(CAST(DATEADD(mi, R.estimated_completion_time/60000.0, 0) AS TIME) AS CHAR(5)) remain,
R.reads, R.writes, R.cpu_time, R.status, R.wait_type
FROM sys.dm_exec_requests R
CROSS APPLY sys.dm_exec_sql_text(R.sql_handle) S
WHERE R.session_id <> @@SPID
@kmatt
kmatt / SQL.xml
Created February 21, 2011 20:39
SQL highlighting for PyCharm (and probably RubyMine, IntelliJ IDEA), with T-SQL specifics
<?xml version="1.0" encoding="UTF-8"?>
<filetype binary="false" default_extension="" description="SQL" name="SQL">
<highlighting>
<options>
<option name="LINE_COMMENT" value="--" />
<option name="COMMENT_START" value="/*" />
<option name="COMMENT_END" value="*/" />
<option name="HEX_PREFIX" value="" />
<option name="NUM_POSTFIXES" value="" />
<option name="HAS_BRACKETS" value="true" />
@kmatt
kmatt / sqlpostgres.vim
Created May 21, 2012 16:09 — forked from chanmix51/sqlpostgres.vim
SQL postgres vim syntax file
" Vim syntax file
" Language: SQL, PGSQL (postgres 9.1)
" Last Change: 2012 May 21st
" Maintainer: Grégoire Hubert <greg DOT hubert AT gmail DOT com>
" Based on the work of Paul Moore <pf_moore AT yahoo.co.uk>
" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
syntax clear
@kmatt
kmatt / pv_dump.sh
Created May 24, 2012 20:07
Monitoring dump and restore with pv
# dump
pg_dump testdb | pv -c -s $(psql -tc "SELECT pg_database_size('testdb')") -N dump | gzip > testdb.sql.gz
# restore
pv testdb_20120501.sql.gz | zcat | psql testdb
@kmatt
kmatt / gist:2848105
Created June 1, 2012 02:17 — forked from mikeyk/gist:1329319
Testing storage of millions of keys in Redis
#! /usr/bin/env python
import redis
import random
import sys
r = redis.Redis(host = 'localhost', port = 6389)
REDIS_SETGET = False
REDIS_HSET = False
@kmatt
kmatt / gist:2848112
Created June 1, 2012 02:18
The Zen of R
# I am a scientist who has been using R for about 2 years. Today I achieved a measure of enlightenment into
# the zen of R, and I want to share it with you.
# I was simulating a set of independent random walks, which are formed by a multiplicative process. Here is
# the code I had built up over a few months of working on it and modifying it on and off as research
# questions changed:
TimeSteps <- 1000
Walks <- 100
ErrorMagnitude <- 0.03
@kmatt
kmatt / mySplitDump.sh
Created June 8, 2012 19:45
Split MySQL dumps
file=$1
nextDB=""
nextStart=0
nextEnd=0
lastDB=""
lastStart=0
for i in `grep -n "^CREATE DATABASE" $file | awk '{print $1":"$7}' | sed "s/CREATE://g"`
do
@kmatt
kmatt / redis_chunker.py
Last active September 17, 2024 21:15
Example file chunker and Redis loader
import multiprocessing, os, sys, time
from itertools import izip
import redis
rr = None
def process_chunk(chunk):
global rr
if rr is None:
rr = redis.Redis() # Create one connection per process
print 'PID %d' % os.getpid()
@kmatt
kmatt / RestoreLastBak.ps1
Created June 21, 2012 17:07
Restore SQL Server backups from wildcard path
param ($bakpath, $dbname, $instance, $datapath, $logpath, [switch]$norecovery, [switch]$replace, [switch]$rollback, [switch]$standby, [switch]$test)
# Restore latest backup from wildcard path, optionally forcing out open connections
# Used to restore backups copied to a standby or test server
""
# SQL 2008 PS module imports
#[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
#if ((get-pssnapin sqlserverprovidersnapin100 -ErrorAction "SilentlyContinue") -eq $NULL) { add-pssnapin sqlserverprovidersnapin110 }
#if ((get-pssnapin sqlservercmdletsnapin100 -ErrorAction "SilentlyContinue") -eq $NULL) { add-pssnapin sqlservercmdletsnapin110 }
@kmatt
kmatt / gist:3351502
Created August 14, 2012 18:28 — forked from karlseguin/gist:2992426
sample code used by ranking post
# http://openmymind.net/Paging-And-Ranking-With-Large-Offsets-MongoDB-vs-Redis-vs-Postgresql/
lids = ['4fe907f1210b2e9e3080f001', '4fe907f1210b2e9e3080f002', '4fe907f1210b2e9e3080f003', '4fe907f1210b2e9e3080f004', '4fe907f1210b2e9e3080f005']
insert the data
open('data.csv', 'w') do |f|
6000000.times do |i|
f.puts "#{lids.sample},user_#{i},#{(rand() * 10000000).to_i}"
end
end