views:

48

answers:

2

Is there a big performance difference between those two sql count statements, when performing large counts (large here means 100k + records)

first:

SELECT count(*) FROM table1 WHERE <some very complex conditions>

second:

SELECT count(*) FROM (SELECT * FROM table1 WHERE <some very complex conditions>) subquery_alias

I know that first approach is right, but i want to know is this statements will perform similar ?

+2  A: 

The query optimizer will most likely transform your second query into the first one. There should be no measurable performance difference between those two queries.

halfdan
A: 

The answer depends on Database being used. for MS SQL the Query Optimizer will optimize the query and both will have similar performance. But for other database system it depends on the intelligence of the Query Optimizer.

Numenor
It's tagged PostgreSQL
halfdan
PostgreSQL does the same, executes both queries the same.
Frank Heikens