Monday, March 03, 2008

Until recently, Xenomorph has been using a proprietary model to store data. However, reading in the latest Wilmott magazine (January 2008 - "Spreading Wings"),

(article should appear here at some point)

http://www.xenomorph.com/news/

CEO Brian Sentance talks about how Xenomorph are working with Microsoft to store the data on SQL Server. It will be interesting to see what performance can be achieved in the relational world (assuming that is how it is implemented).

On a related note, SQL Server is falling down the rankings on the TPC-C league

http://www.tpc.org/tpcc/results/tpcc_perf_results.asp?resulttype=noncluster

if SQL 2008 offered any tangible performance improvements I would have expected to see an entry by now

There are new records in other areas

http://blogs.msdn.com/sqlperf/archive/2008/02/27/sql-server-2008-launched-today-with-great-performance-amp-scalability.aspx

http://www.tpc.org/tpce/tpce_perf_results.asp

but could this be down to hardware improvements? Furthermore, the absence of competing databases implies that other vendors don't take this as seriously

Monday, March 03, 2008 9:08:07 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Monday, June 25, 2007
Monday, June 25, 2007 5:36:48 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, March 27, 2007
Monday, November 20, 2006
Wednesday, November 08, 2006
Wednesday, November 08, 2006 8:20:28 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, October 24, 2006

http://support.microsoft.com/?kbid=916287

Nothing new, but it appears that this may solve some of the problems with SP4. I became aware of it after it was recommended by Tony Rogerson

http://sqlblogcasts.com/blogs/tonyrogerson/

We are looking at getting SQL 2005 running rather than upgrade from SQL 2000 SP3 to the above hotfix

Tuesday, October 24, 2006 9:05:30 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Monday, October 09, 2006

I've been reading Part 2 of the Ben-Gan SQL 2005 books

http://www.amazon.co.uk/Inside-SQL-Server-2005-Programming/dp/0735621977/sr=8-2/qid=1160398841/ref=sr_1_2/026-8787779-9446068?ie=UTF8&s=books

and in one of the chapters he discussed temporary tables

vs. table variables

http://support.microsoft.com/kb/305977

Ever since table variables were introduced in SQL 2000 I have used them by default rather than temporary tables. This decision was made after team members at the company I was working with at the time had experienced blocking when using temporary tables within transactions in SQL 7.0. Their solution was to use "permanent"  temp tables with the connections SPID as part of the primary key. When they migrated to SQL 2000, they couldn't replace permanent temp tables with table variables as the data was required by more than one procedure in the same transaction (the stored procedures were very granular to avoid using syntax such as "WHERE @ip_Paramter1 IS NULL OR Field1 = @ip_Parameter1). However, for vast majority of the work I have done subsequently I have used table variables.

In his book, Ben-Gan made the interesting point that statistics are not kept for table variables so the execution plan may be less than optimal. I thought I would test this for the longest running stored procedure in the application I am currently working on.

If I had been using SQL 2000 client tools, it would've been more difficult to compare result, however, SQL 2005 makes it very easy -

Notes:

  • Inserts, deletes and updates are shown as zero as NOCOUNT was on
  • The temporary table holds around 500 rows and is updated 5 times
  • I am running against a SQL 2000 server
  • The first four tests are for the table variable and the last four for temporary tables

So, for my test, table variables are faster than temporary tables, but the book taught me that you must constantly challenge your preconceptions with technology advancing as quickly as it does.

 

Monday, October 09, 2006 1:22:16 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Friday, September 01, 2006

I installed SQL 2005 developer edition on a server for evaluation purposes yesterday. After installation, I had to enable remote connections (doesn't apply to other versions)

The first piece of new functionality I wanted to look at was asynchronous commands

The connection string changes slightly from pre AD0.NET 2.0

Integrated Security=SSPI;Initial Catalog=xxxx;Data Source=xxxx;Async=true;MultipleActiveResultSets=true

with "Async=true" signifying asynchronous behaviour and "MultipleActiveResultSets=true" stating that we want multiple result sets on the same connection.

My test involved retrieving around 250 resultsets - synchronously this took 3.2 seconds whereas asynchronously this took 1.2 seconds. This was undertaken on a twin processor machine with hyperthreading (hyperthreading and SQL may not always be a good idea)

http://blogs.msdn.com/slavao/archive/2005/11/12/492119.aspx

so effectively a 4 proc machine. Hogging all the processors for the code I had in mind should not be a problem as the database is not heavily used, but the improved response using asynchronous execution may come in handy.

A good article for asynchronous commands is listed below

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/async2.asp

Asynchronous commands are a feature of ADO.NET 2.0 so will work against SQL 2000, whereas MARS requires SQL 2005

http://msdn2.microsoft.com/en-us/library/h32h3abf.aspx

 | 
Friday, September 01, 2006 3:46:41 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Friday, June 09, 2006

http://support.microsoft.com/gp/lifesupsps#Servers

Seems quite aggressive, and people seem to be have had issues with SP4.

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=66737

Friday, June 09, 2006 7:55:30 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Friday, June 02, 2006

I'm not using SQL 2005 yet, but will have a look at this when we upgrade.

http://blogs.msdn.com/queryoptteam/archive/2006/04/06/570176.aspx

Friday, June 02, 2006 8:44:47 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, April 25, 2006

I am attempting to update rows in a table - some of the parameters will be null and I need to update the row field to reflect that.

I assumed that (simplified SQL to demonstrate)

DECLARE @Bid DECIMAL(8,7)
DECLARE @Result INT

SELECT @Result = 0

SELECT @Result =
CASE @Bid
 WHEN NULL THEN 1
 ELSE 2
END

SELECT @Result

would give me a value of 1 if it follows the same logic as when testing with an INT (I am using the IS to compare rather than equals)

DECLARE @Bid DECIMAL(8,7)
DECLARE @Result INT

SELECT @Result = 0

SELECT @Bid = 4

SELECT @Result =
CASE @Bid
 WHEN 4 THEN 1
 ELSE 2
END

SELECT @Result

However, it doesn't, so I have to rewrite the SQL as

DECLARE @Bid DECIMAL(8,7)
DECLARE @Result INT

SELECT @Result = 0

SELECT @Result =
CASE
 WHEN @Bid IS NULL THEN 1
 ELSE 2
END

SELECT @Result

which works.

Tuesday, April 25, 2006 4:12:12 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Thursday, April 06, 2006

After my Excel rounding problem

http://www.noelwatson.com/blog/PermaLink,guid,cb3f5ff0-3b1f-4569-bb91-34a2f0e09034.aspx

I came across a SQL rounding issue. I had been using ISNULL() in a stored proc

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_9dph.asp

and discovered that the numbers coming out weren't quite what I was expecting. After reading the above link, I assume that the second (decimal) argument in my ISNULL statement had its level of accuracy converted to the first. The solution was to use COALEASCE()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_9dph.asp

To recreate problem:

DECLARE @Value DECIMAL(4,3)

SELECT @Value = 8.463

DECLARE @Arg1 DECIMAL (2,1)
DECLARE @Arg2 DECIMAL (5,4)

SELECT @Arg2 = 6.3863

SELECT @Value * @Arg2 --CORRECT
SELECT @Value * ISNULL(@Arg1, @Arg2)  --INCORRECT
SELECT 8.463 * 6.4 --INCORRECT
SELECT @Value * COALESCE(@Arg1, @Arg2) --CORRECT

Thursday, April 06, 2006 10:33:14 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, April 05, 2006

http://support.microsoft.com/?kbid=913090

The fixes seem to centre around the non-core functionality such as MDX/Analysis services. I last did MDX using ASP 2.0 several years ago

http://members.microsoft.com/CustomerEvidence/Search/EvidenceDetails.aspx?EvidenceID=2346&LanguageID=1

Wednesday, April 05, 2006 8:19:08 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Thursday, March 30, 2006
Thursday, March 30, 2006 1:27:13 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Friday, March 17, 2006

http://www.microsoft.com/downloads/details.aspx?familyid=39ebef74-1c29-49fe-8221-c94f2b52f6c6&displaylang=en

and Express edition

http://www.microsoft.com/downloads/details.aspx?familyid=57856cdd-da9b-4ad0-9a8a-f193ae8410ad&displaylang=en

more info here

http://blogs.msdn.com/sqlexpress/archive/2006/03/16/Express_CTP.aspx

This service pack was always expected - MS posted a TPC entry with SQL 2005 at the end of last year.

http://www.noelwatson.com/blog/PermaLink,guid,82af9e3b-d08f-4b2b-85fc-9797aa55a567.aspx

MS are now moving away from traditional betas and releasing more frequent CTP's. It's good to see MS improving the functionality of their free offering.

Friday, March 17, 2006 8:32:12 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, March 15, 2006

Write something with SQL Express and win $10k

http://www.madeinexpresscontest.com/

Wednesday, March 15, 2006 4:17:28 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Thursday, March 02, 2006

http://msdn.microsoft.com/sql/default.aspx?pull=/library/en-us/dnsql90/html/sqlruleengine.asp

Interesting article. We do similar things in some of our stored procedures. Rather than writing dynamic SQL, you can write

(@ip_parameter IS NULL OR Column = @ip_parameter).

The advantage of doing this is that you should only have one execution plan rather than a number of plans for the dynamic SQL, and the code should be easier to maintain.

http://msdn2.microsoft.com/en-us/library(d=robot)/ms187815.aspx

However, care must be taken which parameters are used to generate the execution plan. I've had problems with the past with this and wouldn't use either method for performance critical sections - instead I'd break down the stored procedures into separate sp's

Thursday, March 02, 2006 9:40:25 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback

http://searchsqlserver.techtarget.com/tip/1,289483,sid87_gci1165052_tax301336,00.html?track=NL-464&ad=541873&adg=301324&bucket=ETA

Surely a lot of this is common sense.

  • Use Windows Authentication and even then don't leave the sa password blank

http://www.noelwatson.com/blog/PermaLink,guid,bb890cab-3f5b-40d2-b7a8-8854683b993e.aspx

  • Use stored procedures, and set CommandType = CommandType.StoredProcedure, and use the parameters collection -  setting types (explicitly setting max length on chars and varchars).
  • Block SQL ports if you have to have SQL on your web server. The only port I leave open on the firewall is 80 for IIS

http://msdn2.microsoft.com/en-us/library/ms161953.aspx

Thursday, March 02, 2006 9:18:18 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Monday, February 20, 2006

This went very smoothly. After backing everything up, I uninstalled the framework and SQL 2005 beta 2. Installing SQL 2005 installs the 2.0 framework by default, The only 2 gotchas were

 

allowing the extension for the correct version of .NET

setting ASP.NET to run under the correct version. Windows 2003 comes with Framework 1.1 installed as standard and I don't believe you can "officially" uninstall it.

 | 
Monday, February 20, 2006 9:06:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
  • Retake offer on selected exams

http://www.microsoft.com/learning/mcp/offers/2ndchance/thankyou/default.asp

  • Three free beta exams available

Exam 71-547 Pro - Designing and Developing Web-based Applications by Using the Microsoft .NET Framework (promo code: PRO547)
Exam 71-548 Pro - Designing and Developing Windows-based Applications by Using the Microsoft .NET Framework (promo code: BTA548) 
Exam 71-549 Pro - Designing and Developing Enterprise Applications by Using the Microsoft .NET Framework (promo code: 549BTA)

This follows on from some exams released for beta recently (552-554)

http://www.noelwatson.com/blog/PermaLink,guid,8f62f25d-6d3c-4cdc-b73b-3ae7cbf3b72b.aspx

  • Three exams are Live

http://www.microsoft.com/learning/exams/70-528.asp

http://www.microsoft.com/learning/exams/70-431.asp

http://www.microsoft.com/learning/exams/70-441.asp

 

I am probably doing the CQF this year, so will leave the MS upgrades until next year.

 |  | 
Monday, February 20, 2006 12:15:29 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Thursday, February 16, 2006
Wednesday, February 15, 2006

http://www.sleepycat.com/

http://www.businessweek.com/technology/content/feb2006/tc20060209_810527.htm

As the second article states, on first impressions, this doesn't appear to make much sense.

I can't see the appeal of open source databases -  MS SQL offers all I need, and is free in basic form

http://msdn.microsoft.com/vstudio/express/sql/default.aspx

With this version, there is no governor (unlike 2000), and it can support 1GB RAM and 4GB database size

 | 
Wednesday, February 15, 2006 11:41:40 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, February 08, 2006

25% less code - new features include

  • ASP.NET membership provider
  • Master pages
  • Generics

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdasamppet4.asp

 |  |  | 
Wednesday, February 08, 2006 8:26:31 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, January 31, 2006

I read the SQL 7 and 2000 version several years ago (the 2000 version was very similar to 7)

http://www.microsoft.com/MSPress/books/4297.asp

and was interested to see that the new version will be broken down into three sections

http://www.microsoft.com/MSPress/books/8564.asp

http://www.microsoft.com/MSPress/books/9615.asp

http://www.microsoft.com/MSPress/books/7436.asp

The first two are written by Itzik Ben-Gan - more details can be found here

http://www.sql.co.il/books/insidetsql2005/

All books will be available in the first half of 2006

 | 
Tuesday, January 31, 2006 3:02:27 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Wednesday, January 18, 2006

http://www.eweek.com/article2/0,1759,1912228,00.asp?kc=EWRSS03119TX1K0000594

http://www.red-database-security.com/advisory/oracle_cpu_jan_2006.html

82 patches sounds like a high number - seems as though they may be following a trustworthy initiative similar to MS and reviewing the codebase for obvious security holes.

http://www.microsoft.com/mscorp/twc/default.mspx

Wednesday, January 18, 2006 4:58:20 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, January 17, 2006

Not new news, but haven't seen an in-depth explanation before

http://blogs.msdn.com/slavao/archive/2005/11/12/492119.aspx

Tuesday, January 17, 2006 1:32:19 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Monday, January 16, 2006

Had the latest quarterly update of the Action pack delivered. It now includes

  • SQL 2005 Standard edition
  • Visual Studio 2005 Express edition

You can get this free from here - MS have waived the first year free of $49.

http://msdn.microsoft.com/vstudio/express/support/faq/default.aspx#pricing

  • Visual Web Developer

http://msdn.microsoft.com/vstudio/express/vwd/

More details can be found here (when they get round to updating it!)

http://www.microsoft.com/uk/partner/sales_and_marketing/actionpack/contents.aspx

 |  | 
Monday, January 16, 2006 7:57:54 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Sunday, January 15, 2006

http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx

Actually refreshed over a month ago, but only just noticed

Sunday, January 15, 2006 4:27:40 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, January 10, 2006

This event is being held in London on the 6th Feb 2006. Registration cost £69 and all attendees get free Microsoft Software Package which includes SQL Server 2005 Standard Edition, Visual Studio 2005 Professional Edition and BizTalk Server 2006 Beta.

http://www.windowsitpro.com/roadshows/sqlservereurope/index.cfm?action=dynamic&v=5008&p=5043&code

Note that the website doesn't mention the software but the newsletter does

 |  | 
Tuesday, January 10, 2006 3:30:17 PM (GMT Standard Time, UTC+00:00)  #    Comments [2]  |  Trackback
Saturday, January 07, 2006
Friday, January 06, 2006

http://www.tpc.org/tpcc/results/tpcc_result_detail.asp?id=105112801

http://www.windowsitpro.com/Article/ArticleID/48886/48886.html

This hasn't been publicised much, and MS still list the previous value on their site - although this 1,082,203 tpmC result no longer appears on TPC for some reason.

http://www.microsoft.com/sql/prodinfo/compare/tpcc.mspx

These kind of results are irrelevant to all but the few companies that demand this much power. Of more interest to most is the price/performance results - SQL is the only entrant with a sub dollar per transaction result.

http://www.tpc.org/tpcc/results/tpcc_price_perf_results.asp?resulttype=noncluster

Friday, January 06, 2006 3:27:38 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Tuesday, December 13, 2005

http://download.microsoft.com/download/1/3/4/134644fd-05ad-4ee8-8b5a-0aed1c18a31e/TShootPerfProbs.doc

WIll read it when we move to 2005 - hopefully in the next 6 months

 

Tuesday, December 13, 2005 4:48:51 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback

Recently one of the developers was profiling an application to see where improvements could be made to the code. He was using  JetBrains profiler,

http://www.jetbrains.com/profiler/

and this was indicating that retrieving a string using from a SqlDataReader using reader.GetString() was taking a long time.

To recreate the problem, I firstly populated a similar database – the script in the previous entry is sufficient.

http://www.noelwatson.com/blog/PermaLink,guid,b8d1d44e-0058-43d7-8635-25513e9bc86e.aspx

although you will have to add an extra zero to the loop – 1 million records wasn’t enough to recreate the problem.

DECLARE @DateSubtract DECIMAL(9,4)
DECLARE @Counter INT

SELECT @Counter = 0

WHILE @Counter < 10000000
BEGIN
 SELECT @DateSubtract = RAND() * 100000
 INSERT INTO LOG (LogDateTime, Details)
 VALUES (DATEADD(mi, - @DateSubtract, GETDATE()), 'TEST')
 SELECT @Counter = @Counter + 1
END

A simplified version of the stored procedure is shown below

ALTER        PROCEDURE p_LogSearch_s
(
@ip_StartDate DATETIME,
@ip_EndDate DATETIME
)

AS


SELECT  Details, LogDateTime FROM LOG
WHERE (@ip_StartDate IS NULL OR LogDateTime >= @ip_StartDate) AND (@ip_EndDate IS NULL OR LogDateTime < @ip_EndDate)

Test code is shown below. This is called 20 times

private void RetrieveData()
  {
   SqlConnection sqlConn = null;
   SqlCommand sqlCmd = null;
   SqlDataReader reader = null;
   SqlParameter param = null;
   string strSqlConn;
   string logDetails;
   DateTime logTime;
  
    strSqlConn = @"Data Source=(local); Initial Catalog=Log;Integrated Security=SSPI";
    sqlConn = new SqlConnection(strSqlConn);
    sqlCmd = new SqlCommand();


    sqlConn.Open();

    sqlCmd.CommandText = "p_LogSearch_s";
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlCmd.Connection = sqlConn;

    param = new SqlParameter("@ip_StartDate",SqlDbType.SmallDateTime);
    param.Value  = new DateTime(2005,12,7,21,30,0,0);
    sqlCmd.Parameters.Add(param);
    param = new SqlParameter("@ip_EndDate",SqlDbType.SmallDateTime);
    param.Value = new DateTime(2005,12,8);
    sqlCmd.Parameters.Add(param);
   
    
    reader = sqlCmd.ExecuteReader();

    while (reader.Read())
    {
     logDetails = reader.GetString(0);
     logTime = reader.GetDateTime(1);
     
    }
  }

We are retrieving 2.5 hours worth of data - 15161 rows


Looking at the results, it is interesting to see that GetString() takes almost 20 times as long as GetDate(). However, this is because GetString() is the first call to retrieve the next row of the data, so will take longer and is a red herring. If we were to swap GetDateTime() above GetString(), the orders would reverse. 303220 is the number of hits we are expecting (20 iterations * 15161 rows), the Read method reads the extra time when the end of the recordset is reached.

Note that some of the calls (e.g FPushMessageLoop) can be ignored as they are related to form population

Solutions

1. Modify Isolation level

One way to improve performance is to set the isolation level to read uncommitted – this is acceptable when reading log data as we are not too fussed if the occasional record is uncommitted. This improves performance by around 20%, although gives an even more misleading result in JetBrains!

2. Modify the stored procedure

The stored procedure can be rewritten

ALTER         PROCEDURE p_LogSearch_s
(
@ip_StartDate DATETIME,
@ip_EndDate DATETIME
)

AS

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT  Details, LogDateTime FROM LOG
WHERE (LogDateTime >= @ip_StartDate) AND (LogDateTime < @ip_EndDate)

and the results are dramatic

However, this stored procedure as it stands may be unacceptable as it may be the case that null variables must be passed to the stored procedure.

3. Improve performance reading from the datareader

With reference to the following link

http://www.sqljunkies.com/WebLog/amachanic/archive/2005/04/06/10462.aspx

it is possible to improve performance when accessing the datareader

while (reader.Read())
    {
     logDetails = reader.GetString(0);
     logTime =reader.GetDateTime(1);
    }

becomes

while (reader.Read())
    {
     logDetails = (string)reader[0];
     logTime =(DateTime)reader[1];
    }

 

This improves performance marginally, get_item taking 59064ms is less than the combined total of GetString (58803ms) and GetDateTime (389ms) in test 1 (UNCOMMITTED).

4. Use a dataset

I did this just out of interest, as I didn't believe performance would be improved

while (reader.Read())
    {
     logDetails = reader.GetString(0);
     logTime =reader.GetDateTime(1);
    }

becomes

DataSet ds = new DataSet();
   SqlDataAdapter da;
   da = new SqlDataAdapter(sqlCmd);
   
   da.Fill(ds);
   foreach(DataRow dr in ds.Tables[0].Rows)
   {
    logDetails = (string)dr[0];
    logTime =(DateTime)dr[1];

   }

 

65312 milliseconds compares favourably with test 1, (note that in the real world, I wouldn't iterate through the dataset but instead would bind directly to a grid) which has

GetString           58803

Read                 3825

ExecuteReader   1398

GetDateTime      389

=64415ms

Dataset vs. DataReaders can be found here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdadotnetarch031.asp

Conclusion

Modifying the stored procedure would be the obvious choice. Using code such as

(@ip_StartDate IS NULL OR LogDateTime >= @ip_StartDate)

Doesn’t allow the index to be used correctly (uses a scan rather than a seek)

An alternative is to use a parent stored procedure, and call individual child stored procedures dependant on the parameters passed to the parent sp. Maintaining these can be a headache, but are necessary if performance is critical.

 

 

 |  | 
Tuesday, December 13, 2005 11:12:24 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Friday, December 09, 2005

Many times I have wanted to ignore the time portion of a datetime field in a database when selecting records for a given date. One rudimentary way would be to store date and time in separate fields. There is another alternative

  • Scenario

Logging database with 1 million records

  • Configuration

Parallelism: disabled (compare methods more acurately)

Recovery mode: simple (reduces time when adding indexes)

  • Scripts

CREATE TABLE [dbo].[Log] (
 [LogID] [int] IDENTITY (1, 1) NOT NULL ,
 [LogDateTime] [smalldatetime] NOT NULL ,
 [Details] [char] (10) COLLATE Latin1_General_CI_AS NOT NULL
) ON [PRIMARY]
GO

CREATE  CLUSTERED  INDEX [LogDateTime] ON [dbo].[Log]([LogDateTime]) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Log] ADD
 CONSTRAINT [PK_Log] PRIMARY KEY  NONCLUSTERED
 (
  [LogID]
 )  ON [PRIMARY]
GO


SET NOCOUNT OFF

DECLARE @DateSubtract DECIMAL(9,4)
DECLARE @Counter INT

SELECT @Counter = 0

WHILE @Counter < 1000000
BEGIN
 SELECT @DateSubtract = RAND() * 100000
 INSERT INTO LOG (LogDateTime, Details)
 VALUES (DATEADD(mi, - @DateSubtract, GETDATE()), 'TEST')
 SELECT @Counter = @Counter + 1
END


We will be selecting all records for 7th December 2005 (14188 records)

The first four examples attempt to select records for a given date

1.

SELECT * FROM Log WHERE DATEPART(yy, LogDateTime) = 2005
AND DATEPART(mm, LogDateTime) = 12
AND DATEPART(dd, LogDateTime) = 7

2.

SELECT * FROM Log WHERE DATEADD(d,DATEDIFF(d,0,LogDateTime),0) = DATEADD(d,DATEDIFF(d,0,GETDATE()),-2)

3.

SELECT * FROM Log where DATEDIFF(dd,LogDateTime,GETDATE()-2) = 0   

4.

SELECT * FROM Log WHERE CONVERT(CHAR(8), LogDateTime, 112) = CONVERT(CHAR(8), GETDATE()-2, 112)

The first three have identical performance (4235 logical reads), whereas the 4th is a lot slower (45188 logical reads) - it is unable to use the clustered index on LogDateTime

The next three tests select records where the date is between two dates i.e before 8th December and after 6th December.

I could've used BETWEEN rather than "greater than or equal to" and less than"


5.

SELECT * FROM LOG WHERE LogDateTime >=  CAST(CONVERT(VARCHAR(12),GETDATE() - 2,112) + ' 00:00:00' as DATETIME) AND LogDateTime <  CAST(CONVERT(VARCHAR(12),GETDATE() - 1,112) + ' 00:00:00' as datetime)

 

6.


SELECT * FROM Log WHERE LogDateTime >= DATEADD(d,DATEDIFF(d,0,GETDATE()),-2)
AND LogDateTime < DATEADD(d,DATEDIFF(d,0,GETDATE()),-1)

 

7.

SELECT * FROM Log WHERE LogDateTime >= '20051207' AND LogDateTime < '20051208'

 

These last three methods all cost 63 reads and are by far the best solution. If you have added and index and the index is being used with a scan rather than a seek, or the index is not being used at all, there is a problem!

Execution plan info can be found here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/optimsql/odp_tun_1_5pde.asp

Friday, December 09, 2005 6:39:28 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Monday, November 28, 2005

Microsoft have released a case study of how an investment bank (BarCap) has used SQL 2005 to developed a fixed income solution.

http://members.microsoft.com/customerevidence/search/EvidenceDetails.aspx?EvidenceID=13659&LanguageID=1&PFT=Microsoft%20SQL%20Server%202005&TaxID=20363

It's impressive how much resource MS throws on these early adopters - 11 consultants would normally cost around £15k a day.  

I notice that the report mentions that readers and writers no longer block

http://msdn2.microsoft.com/en-us/library/ms130975.aspx

It will be interesting to see how much of a performance hit snapshot is - prior to 2005 alternatives such as "READPAST" or "NOLOCK" hints could be used - although this took a large amount of testing to ensure you got what you expected. 

A few years ago I worked on a product using MS tools in beta

http://members.microsoft.com/customerevidence/search/EvidenceDetails.aspx?EvidenceID=1850&LanguageID=1

When I joined, the company had already been using .NET in beta 1 for around six months, and we went live with beta 2. Looking back, it was a brave decision, and for us developers lucky enough to be involved, gave us a chance to get ahead on the technology curve.

I am currently working on a similar system to the BarCap solution, although using SQL 2000 rather than SQL 2005. SQL 2005 has been in beta for donkeys, so I think the stability would be there, but I personally don't believe there would be a compelling case to upgrade.

 

 | 
Monday, November 28, 2005 9:54:49 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Sunday, November 27, 2005
 |  | 
Sunday, November 27, 2005 4:36:54 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  |  Trackback
Monday, October 31, 2005

Coinciding with the release of next generation SQL and Visual Studio, Microsoft have released three new certifications

http://www.microsoft.com/learning/mcp/newgen/

 

This requires one or two exams - similar to passing one exam to attain MCP

  • Technology Specialist: .NET Framework 2.0 Web Applications