Skip to content

Instantly share code, notes, and snippets.

View kveratis's full-sized avatar

Daniel Petersen kveratis

View GitHub Profile
@kveratis
kveratis / Example.aspx.cs
Created December 14, 2012 00:13
Microsoft Unity dependency injection module.
using System;
using System.Data;
using Company.Repositories;
using Company.Repositories.Sql;
using Microsoft.Practices.Unity;
namespace Company
{
public partial class LeadAdd : WebPage
{
@kveratis
kveratis / SQL Process Monitor
Created August 16, 2013 20:32
When monitoring processes on MS SQL, you can generally use the sp_who2 to list all the processes running with relevant data. However, this script expands on that by mapping processes that are part of SQL Jobs back to their job name and step.
declare @who table(spid int,status varchar(50),login varchar(50),hostname varchar(50),blkby varchar(50),dbname varchar(50),command varchar(500), cputime int,diskio int, lastbatch varchar(50),programname varchar(255),spid1 int,requestid int)
insert into @who execute sp_who2;
WITH RawJob as
(
SELECT spid
,CAST(CONVERT(varbinary, SUBSTRING(programname, 30, 34), 1) as uniqueidentifier) as JobId -- Grab JobId and convert hex to uniqueidentifier
,CAST(REPLACE(SUBSTRING(programname, 72, 2), ')', '') as INT) as JobStepId -- Grab step id
FROM @who
WHERE programname LIKE 'SQLAgent - TSQL%'
@kveratis
kveratis / MissingIndexesByImprovement.sql
Created August 16, 2013 20:35
Gets a list of potential indexes ordered by how much help they would be in improving performance.
SELECT mid.database_id
,db.name
,migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure
,'CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle) + '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']' + ' ON ' + mid.statement + ' (' + ISNULL (mid.equality_columns,'') + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END + ISNULL (mid.inequality_columns, '') + ')' + ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement
,migs.*
,mid.database_id
,mid.[object_id]
FROM sys.dm_db_missing_index_groups mig
INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle
@kveratis
kveratis / IndexFragmentationQuery.sql
Last active December 21, 2015 04:59
Returns which indexes should be rebuilt and which can be reorganized to improve index performance
-- General Maintenance Notes on Indexes
-- Indexes should be rebuilt when index fragmentation is greater than 30%
-- Indexes should be reorganized when index fragmentation is between 5% and 30%
-- Index rebuilding is much more costly than reorganization.
-- Rebuild
DECLARE @database_id INT
SET @database_id = DB_ID();
SELECT ps.database_id, ps.OBJECT_ID,
ps.index_id, b.name,
@kveratis
kveratis / RequildOrReorganizeIndex.sql
Created August 16, 2013 20:38
Code to either rebuild or reorganize an index
--Rebuild Index
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee
REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON);
-- Reorganize Index
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee
REORGANIZE;
@kveratis
kveratis / MSSQLBackupRoutines.md
Created August 16, 2013 20:45
List of backup and restore routines for MS SQL

Backups

Full Backup

DECLARE @BackupLocation NVARCHAR(100);
SET @BackupLocation = 'c:\data\AdventureWorks2008_' +
CONVERT(NVARCHAR(8), GETDATE(), 112) + '.bak';
BACKUP DATABASE AdventureWorks2008
TO DISK = @BackupLocation
WITH INIT;
@kveratis
kveratis / SQLNotes.md
Created August 16, 2013 20:47
Some basic notes on SQL Joins and Wildcards including some notes on UNION and Table Variables

Wildcards

% - A substitute for zero or more characters (can be in the middle) _ - A substitute for exactly on character [charlist] - Any single character in the list [!charlist] - Any single character not in the list

JOIN (INNER JOIN)

Returns rows when there is at least one match in both tables.

@kveratis
kveratis / CreateDateDimension.sql
Last active November 14, 2022 01:50
Common Data Mart/Data Warehouse Code for creating Date and Time Dimensions. Note the Time Dimension is distinct from Date Dimension as it tracks time of day and time durations. The bucket fields are useful for analysis of data in blocks of time or duration and can be used as the buckets in a histogram.
DROP TABLE [dbo].[DimDate]
GO
CREATE TABLE [dbo].[DimDate] (
[DateKey] [int] NOT NULL PRIMARY KEY,
[FullDate] [date] NOT NULL,
[Year] [smallint] NOT NULL,
[Quarter] [tinyint] NOT NULL,
[MonthOfYear] [tinyint] NOT NULL,
[MonthName] [varchar](10) NOT NULL,
[WeekOfYear] [tinyint] NOT NULL,
@kveratis
kveratis / DefaultDatawarehouseSchema.sql
Created August 16, 2013 21:04
Basic data warehouse schema (SQLite code)
DROP INDEX IF EXISTS IDX_DimTimeZone_Conversion;
DROP TABLE IF EXISTS DimTimeZone;
CREATE TABLE DimTimeZone(
TimeZoneKey INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Description VARCHAR(150) NOT NULL,
StandardConversion INT NOT NULL DEFAULT 0,
DSTConversion INT NOT NULL DEFAULT 0,
ObservesDST TINYINT NOT NULL DEFAULT 0
);
@kveratis
kveratis / FileProcessor.tt
Created August 20, 2013 01:09
My T4 Templates for String and File Handling
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".generated.cs" #>
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#region TextBlock
public partial class TextBlock : List<string>