• subscribe
February 10, 2010 12:00 AM

Calculating Concurrent Sessions, Part 3

A new set-based solution far exceeds previous solutions
SQL Server Pro
InstantDoc ID #103407

Listing 3: Code to Populate the Sessions Table with a Large Number of Rows

SET NOCOUNT ON;

USE tempdb;

 

TRUNCATE TABLE dbo.Sessions;

 

DECLARE @numrows AS INT;

SET @numrows = 10000; -- Change this value according to your needs

 

INSERT INTO dbo.Sessions WITH(TABLOCK)

    (keycol, app, usr, host, starttime, endtime)

  SELECT

    ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS keycol,

    D.*,

    DATEADD(

      second,

      1 + ABS(CHECKSUM(NEWID())) % (20*60),

      starttime) AS endtime

  FROM

  (

    SELECT

      'app' + CAST(1 + ABS(CHECKSUM(NEWID())) % 10 AS VARCHAR(10)) AS app,

      'user1' AS usr,

      'host1' AS host,

      DATEADD(

        second,

        1 + ABS(CHECKSUM(NEWID())) % (30*24*60*60),

        '20090101') AS starttime

    FROM dbo.GetNums(@numrows) AS Nums

    WHERE n <= @numrows

  ) AS D;

GO



ARTICLE TOOLS

Comments
  • Marcus
    7 months ago
    Oct 03, 2011

    Great article. Any idea how this could be adapted to support data that includes counts? For example:

    app1 user1 host1 4 2011-01-01 00:00:00 2011-01-01 12:00:00

    Where 4 is the number of sessions that all share the same app, user, host, and start / end times.

    Thanks!

  • R Barry
    2 years ago
    Feb 22, 2010

    Holy Moley! Itzik Ben_Gan is giving me props in his article?!?!? I am stunned that he even knows who I am, let lone that he's giving me a shout-out. Thanks a ton, Itzik. I thought this was a great article and series anyway, but this is the icing on the cake for me.

    Thanks Again!

You must log on before posting a comment.

Are you a new visitor? Register Here