Pete's Windows, Office, VB & SQL Blog

Problems I have solved (or not) and good ideas I've found

How to get an empty table from a sql query in VB 2005 (.net) without timeouts

leave a comment »

Its simple – write a select that guarantees the return of nothing.  You will need to know your data.  In my case this is for adding rows to a log table.  The rows are timestamped when they are entered, so I had:

SELECT [Time], [Message] FROM [LogTable]

where [Time] > {fn Now() }

It worked.

Then it began to time out.

Every time the SELECT was run a table scan occurred.  The log now has just over 500,000 rows.

I removed some to a history table, down to 100,000, but still no good.

The execution plan says table scan, so that is the obvious cause.  I soon discounted indexes, the table is rarely read, and I’m prepared for the table scan when it is, as the select is likely to include [Message] Like ‘%something%’

Then I tried

SELECT [Time], [Message] FROM [LogTable]

where 1 = 2

the execution plan says ‘constant scan’, and the query analyser reports 00:00:00 as the execution time.

FIXED!

Written by fisherpeter

2009 November 21 at 14:51

Leave a comment