Just do the following:
SELECT COALESCE( SUM(capacity), 0) FROM objects WHERE null IS NOT NULL;
By the way, COALESCE inside the SUM is redundant, even if the capacity is NULL, it will not make the total value null.
In particular:
create table objects ( capacity int null ); insert into objects(capacity) values (1),(2),(NULL),(3); select sum(capacity) from objects;
This will return a value of 6, not null.
And sharing within an aggregate function is also a killer of performance, since your RDBMS engine cannot just iterate over all rows, it must evaluate each column of rows if its value is null. I saw a little OCD request where all aggregated requests have coalescence inside, I think that the original developer has the Cargo Cult Programming symptom, the request is very stupid. I removed the coalesce inside the SUM, then the request quickly.
Michael buen
source share