Friday, 30 September 2011

SEQUENCE vs. IDENTITY

SQL Server Denali contains a new database object called SEQUENCE that delivers sequential numbers. It complements the existing IDENTITY construct that has a similar objective.
IDENTITY has been around as long as I can remember and comes from SQL Servers Sybase heritage. It is simple to use because all that is needed is an IDENTITY clause on the column:
CREATE TABLE Table1 (
    Column1 int NOT NULL IDENTITY(1,1),
    Column2 varchar(50) NULL)
After this INSERTs into the table will be automatically assigned the next number in the sequence.
IDENTITY is safe and easy to use. So why introduce a new and similar construct now?
The sceptical amongst us may be thinking that the reason SQL Server is getting SEQUENCE objects is because Oracle has them and Microsoft don’t want to be seen as second best. Another reason could be compliance with the ANSI/ISO SQL standards. Another is that IDENTITY does have some disadvantages that SEQUENCE objects do not have. Some examples of these are:
·         A column that uses IDENTITY cannot be changed with an UPDATE statement (except when the SET IDENTITY_INSERT statement is used)
·         IDENTITY cannot be added to an existing column using ALTER TABLE
·         IDENTITY allocates numbers for use in a single table and numbers cannot be shared across tables
·         IDENTITY specifically relates to new records and INSERT and cannot be used in other circumstances
An example SEQUENCE object is a defined as follows:
CREATE SEQUENCE dbo.MySequence
    AS int
    START WITH 1
    INCREMENT BY 1
    MINVALUE 1
    MAXVALUE 100000
    CYCLE
    CACHE 3

It is used to obtain values with the following clause:
NEXT VALUE FOR dbo.MySequence
This can be used in all kinds of circumstances and some of these are as follows:
·         Assigning the next value to a variable:
DECLARE @i int
SELECT @i = NEXT VALUE FOR dbo.MySequence
·         Inserting a value into two tables from the same sequence:
INSERT INTO Table2 (Column1, Column2)
VALUES (NEXT VALUE FOR dbo.MySequence, 'Data')
INSERT INTO Table3 (Column1, Column2)
VALUES (NEXT VALUE FOR dbo.MySequence, 'Data')
·         Defining a sequence as a default for a column, so that new values are assigned automatically on each INSERT:
CREATE TABLE Table4 (
    Column1 int NOT NULL DEFAULT NEXT VALUE FOR dbo.MySequence,
    Column2 varchar(50) NULL)
·         Using a sequence in a SELECT clause to number the rows returned:
SELECT NEXT VALUE FOR dbo.MySequence OVER (ORDER BY Name), Name
FROM sys.objects
Pretty powerful stuff!
Certainly SEQUENCE offers more scope than the IDENTITY alone. But it’s worth remembering that IDENTITY is not going to go away any time soon and so for simplicity and backwards compatibility we can keep on using it. When we need a little more, then we have SEQUENCE as well.
The best of both worlds

Sunday, 25 September 2011

Querying Data Streams in Real-Time

Processing and acting on high volume real-time data is a requirement for many organisations today including:
·         Financial: Processing real time trading information
·         Utilities: Monitoring power consumption, tracking supply vs. demand
·         Communications: Monitoring network usage, tracking failure rates, Processing Banner Ad clicks on web sites
However the traditional approach to data analysis is; to store the data in a database, run queries to extract and calculate the required data and then to produce output in the form of a report. But with 1000 new data records arriving per second this was clearly not practical, until StreamInsight came along that is.

StreamInsight is part of SQL Server 2008 R2 and is designed to accept and query real-time data streams with volumes of thousands of records per second and it is all based on familiar .NET and LINQ technology. There are two editions:
·         Standard edition for data volumes less that 5000 events per second
·         Premium edition for data volumes up to 100000 events per second
You need SQL Server 2008 R2 Data Centre Edition to get the Premium edition, while Standard edition comes with SQL Server 2008 R2 Standard and Enterprise Editions. So how does it work?
·         Firstly the data stream is read from the sources by an adaptor. StreamInsight comes with a set of standard Adaptors for typical data sources, but new custom ones are typical created from these
·         The data is then passed to the queries, which are written using the LINQ query language. These queries operate on the data as it arrives
·         The results of the queries are sent as a data stream to output adaptors which in turn route the results to the system that needs them. This may be a reporting solution or a trading application and so on
Architecturally it looks like this:
 StreamInsight typically runs as a Windows Service on a server, but can be embedded directly into a program if required.

All in all this is a pretty cool extension to SQL Server and real-time decision making on high volume data can now become a part of any organisations analysis and reporting capabilities.