cuzzo
cuzzo

Reputation: 27

Are SQL where conditions executed for each row?

If I have a query:

SELECT *
FROM products
WHERE (value & (POW(2, 24) - 1) > 0;

Will query engines generally execute (POW(2, 24) - 1) N times, for each row in the table (assuming value isn't indexed)? Or is it smart enough to know that value doesn't change and to cache it and use the cached value? Does it depend on the database implementation?

Upvotes: 0

Views: 650

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

Of course, it depends on the database engine.

In general, though, databases will evaluate constant expressions during the compilation phase. That means that a constant is inserted rather than the value being re-calculated for each row. Depending on the database and the expression, this may not always occur.

The where clause will be evaluated for each row using the constant (an index cannot be used on this expression).

Upvotes: 2

Related Questions