Finding the most active 15 minute time period


A guy over at sqlteam.com wanted to find the most active 15-minute period for a series of different stores his company runs and I thought this would be a nice topic for a blog post. I modified the problem a little to fit a sample table I had lying around and the result wasn’t too hard to come by.

Basically I have a table with sample call records for a telephone company and I want to find the most active 15-minute period where the most calls are started. First we have to generate a table with some sample data:

create table calls (
	call_id int identity(1, 1) primary key clustered,
	a_number bigint,
	b_number bigint,
	start_time datetime,
	duration int)
 go

--> Function to generate random numbers, used to create sample data
create function randomnumber (@min bigint, @max bigint, @rand float)
returns bigint
as
begin
	return CAST(((@Max + 1) - @Min) * @Rand + @Min AS bigint)
end
go

--> Generate 500  rows of sample data
declare 
	@end int = 500,
	@current int = 1
	
while (@current <= @end)
	begin
		insert into calls (a_number, b_number, start_time, duration)
		select 
			dbo.randomnumber (22000000, 22999999, RAND()),
			dbo.randomnumber (22000000, 22999999, RAND()),
			DATEADD(MINUTE, dbo.randomnumber (1, 300, RAND()),GETDATE()),
			dbo.randomnumber (3, 3600, RAND())
			
		select @current = @current + 1
	end
go

Now once the test table with sample data has been established, we can run the actual query:

DECLARE @dummydate datetime = '2010-06-23 00:00:00'

;WITH cte AS (
	SELECT 
		[Mod] = DATEDIFF(MILLISECOND, @dummydate, start_time) % (15 * 60 * 1000), --&gt; 15 mins to milliseconds
		*
	FROM calls
	)
SELECT TOP 10 
	Period = DATEADD(MILLISECOND, -[Mod], start_time), 
	CountOfTransID = COUNT(call_id)
FROM cte
GROUP BY DATEADD(MILLISECOND, -[Mod], start_time)
ORDER BY COUNT(call_id) DESC
Advertisements

Last backups in a glance


As most other DBA’s I’m lazy and I try to make things as easy as possible for myself without missing out on anything. And the most important task of my day is to check the latest backups on all servers and I don’t think it can be done any easier than this. Unfortunately I don’t have the luxury of having all my servers registered in Management Studio because of security regulations but if you do, running this as a multi-server query should make things *reeeally* easy for you:

--> Latest backups
USE master
GO

SET NOCOUNT ON
GO

SELECT
  database_name,
  backup_finish_date = CASE
    WHEN db_backup_finish_date &gt; log_backup_finish_date THEN db_backup_finish_date
    ELSE log_backup_finish_date
    END
FROM (
  SELECT
    database_name = a.[name],
    db_backup_finish_date = 
      COALESCE(MAX(db_backup.backup_finish_date), '1900-01-01 00:00:00'),
    log_backup_finish_date = 
      COALESCE(MAX(log_backup.backup_finish_date), '1900-01-01 00:00:00')
  FROM sys.databases a
    LEFT OUTER JOIN msdb..backupset AS db_backup
      ON a.[name] = db_backup.database_name
        AND db_backup.[Type] = 'D'
    LEFT OUTER JOIN msdb..backupset AS log_backup
      ON db_backup.database_name = log_backup.database_name
        AND log_backup.[Type] = 'L'
  GROUP BY a.[name]
  ) AS dt

Poor mans partitioning


There is absolutely nothing new about what I’m writing here, this is a really old trick back from the SQL 2000 days but there are quite a few people who are not aware of the powers of partitioning your data and this is why I’m posting. Unfortunately, native partitioning is for large corporations and companies with thick wallets where Enterprise version is the only way there is, while the rest of us will have to tweak that standard version until our eyes pop out. Well…for those of you that are not aware: there is a way to partition even with standard version and I’m gonna show you how. It will take a bit more effort than the native way and it doesn’t do everything you would hope for but it’s still pretty darn powerful.

And this little script here illustrates the whole concept; you create a few tables with some check constraints, you create a union all view and VOILA! There is your partitioning! Before you run the script make sure to turn on “Show actual execution plan” in your Management studio.

CREATE TABLE part_table1 (
  ID INT NOT NULL PRIMARY KEY CLUSTERED,
  SomeValue VARCHAR(20)
  )
GO
CREATE TABLE part_table2 (
  ID INT NOT NULL PRIMARY KEY CLUSTERED,
  SomeValue VARCHAR(20)
  )
GO
CREATE TABLE part_table3 (
  ID INT NOT NULL PRIMARY KEY CLUSTERED,
  SomeValue VARCHAR(20)
  )
GO

ALTER TABLE part_table1 WITH CHECK ADD CONSTRAINT CK_part_table1 CHECK (ID >= 0 AND ID <= 10) 
ALTER TABLE part_table1 CHECK CONSTRAINT CK_part_table1 

ALTER TABLE part_table2 WITH CHECK ADD CONSTRAINT CK_part_table2 CHECK (ID &gt;= 11 AND ID <= 100) 
ALTER TABLE part_table2 CHECK CONSTRAINT CK_part_table2 

ALTER TABLE part_table3 WITH CHECK ADD CONSTRAINT CK_part_table3 CHECK (ID &gt;= 101)
ALTER TABLE part_table3 CHECK CONSTRAINT CK_part_table3
GO

CREATE VIEW part_table
AS
SELECT * FROM part_table1
UNION ALL
SELECT * FROM part_table2
UNION ALL
SELECT * FROM part_table3
GO

INSERT INTO part_table SELECT 9, 'sqlteam'
INSERT INTO part_table SELECT 24, 'sqlteam'
INSERT INTO part_table2 SELECT 25, 'sqlteam'

SELECT * FROM part_table WHERE ID = 25

Notice in the execution plan when you do the inserts and the select. The insert is a clustered insert but as you can see it scans all the tables to find the right one which is somewhat annoying. But when you look at the final select you see where the “magic” is happening: when you do a select based on the “partitioning column” the query optimizer actually knows which data is where so it only has to search one of the tables! Pretty cool stuff…at least somewhat cool hehe. Using this method you can utilize quite a few of the partitioning techniques that are built in the native partitioning. You can for example have each partition on a different disk and you can add and remove partitions from the view as you like, just remember to do changes in the view first before you delete or rename any tables permanently…

Using sqlcmd from Powershell


I’ve been doing some Sharepoint 2010 stuff lately and one of the tasks I had to accomplish was to automate creating some databases using Powershell. Never having used Powershell before I was somewhat intrigued by the task, but it proved to be a little more hassle than I expected, and included voting up a bug in the sqlcmds “-Variable”-setting!! Check the details here: connect.microsoft.com/sqlserver/…/

What I basically needed to do was to create a database using Powershell, sqlcmd and a .sql script-file that accepted parameters. Sounds easy enough? Well…I might not me the smartest guy in the world but I didn’t expect to spend as much time as I did on it (the bug in invoke-sqlcmd taking most of the time to find out). Here’s what I ended up with:

The powershell script create_database.ps1

#Parameters that must be set:
$DBServer = "192.168.1.10"
$DBDataPath = "D:\MSSQL\Data\"
$DBLogPath = "E:\MSSQL\Log\"
$DBScriptFile = "c:\create databases.sql"

#Add snap-ins and create parameters in the correct format
add-pssnapin sqlserverprovidersnapin100
add-pssnapin sqlservercmdletsnapin100
$DBParam1 = "DBDataPath=" + $DBDataPath
$DBParam2 = "DBLogPath=" + $DBLogPath
$DBParams = $DBParam1, $DBParam2
Invoke-Sqlcmd -InputFile $DBScriptFile -Variable $DBParams -Serverinstance $DBServer -E

And here is the file create_databases.sql

CREATE
DATABASE [SharePoint_Config] ON PRIMARY
(NAME = N'SharePoint_Config', FILENAME = N'$(DBDataPath)SharePoint_Config.mdf' , SIZE = 100MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10% )
LOG ON
(NAME = N'SharePoint_Config_log', FILENAME = N'$(DBLogPath)SharePoint_Config.ldf' , SIZE = 100MB , MAXSIZE = UNLIMITED , FILEGROWTH = 10%)
COLLATE Latin1_General_CI_AS_KS_WS
GO

Create as many parameters as you want, just keep in mind not to have spaces around the powershell parameters when defining them!!

This works:
$DBParam1 = “DBDataPath=” + $DBDataPath

This will not work:
$DBParam1 = “DBDataPath = ” + $DBDataPath