Based on my "How do you count the occurrences of an anchored string using PostgreSQL?"
The fastest way to count occurrences in a sub-string is this.
SELECT length(data) - length(replace(data, '3', ''))
/ length('3')
FROM foo;
If you're creating the string with array_to_string
, it makes sense to unnest
, but it won't be faster. Moreover, I would love to see the full query.
count(CASE WHEN some_column=3 THEN 1 END)
sounds the fastest, or if you upgrade to a new version of PostgreSQL..
count(*) FILTER (WHEN some_column=3)