0

I need to find a fast way to determine if records exist in a database table. The normal method of IF Exists (condition) is not fast enough for my needs. I've found something that is faster but does not work quite as intended.

The normal IF Exists (condition) which works but is too slow for my needs:

IF EXISTS (SELECT * From dbo.SecurityPriceHistory Where FortLabel = 'EP' and TradeTime >= '2020-03-20 15:03:53.000' and Price >= 2345.26) 

My work around that doesn't work, but is extremely fast:

IF EXISTS (SELECT IIF(COUNT(*) = 0, null, 1) From dbo.SecurityPriceHistory Where FortLabel = 'EP' and TradeTime >= '2020-03-20 15:03:53.000' and Price >= 2345.26) 

The issue with the second solution is that when the count(*) = 0, null is returned, but that causes IF EXISTS(null) to return true.

The second solution is fast because it doesn't read any data in the execution plan, while the first one does read data.

4
  • I don't see how count(*) could be faster than the exists query -- Unless SQL Server is smart enough to know that an aggregation query with no GROUP BY or HAVING always returns one row, so EXISTS is always true. Commented Mar 23, 2020 at 19:01
  • This must be the case, IIF always return one row, so it can optimize the actual reading of data. Commented Mar 23, 2020 at 19:23
  • . . IIF() has nothing to do with it. It is the COUNT(*) that returns one row. Commented Mar 23, 2020 at 19:36
  • Under different conditions, the select returns either 1 or NULL as a resultset. Unfortunately, the NULL resultset EXISTS. Commented Mar 23, 2020 at 19:48

4 Answers 4

1

I suggested leaving the original code unchanged, but adding an index to cover one (or more) of the columns in the WHERE clause.

If I changed anything, I might limit the SELECT clause to a single non-null small column.

Sign up to request clarification or add additional context in comments.

1 Comment

There is an index on the columns in the where clause. The execution plan shows that an index seek is being used. Selecting a single column does not change the performance.
0

Switching to a column store index in my particular use case appears to solve my performance problem.

Comments

0

For this query:

IF EXISTS (SELECT * From dbo.SecurityPriceHistory Where FortLabel = 'EP' and TradeTime >= '2020-03-20 15:03:53.000' and Price >= 2345.26 ) 

You either want an index on:

  • SecurityPriceHistory(Fortlabel, TradeTime, Price)
  • SecurityPriceHistory(Fortlabel, Price, TradeTime)

The difference is whether TradeTime or Price is more selective. A single column index is probably not sufficient for this query.

The third column in the index is just there so the index covers the query and doesn't have to reference the data pages.

Comments

0

Try this:

IF (select count(*) From dbo.SecurityPriceHistory
Where FortLabel = 'EP' and TradeTime >= '2020-03-20 15:03:53.000' and Price >= 2345.26) > 0 begin ---your update statement end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.