I have a query like this, that returns rows of text that match the given query:
SELECT content
FROM messages
WHERE created_at >= '2021-07-24T20:17:18.141Z'
AND created_at <= '2021-07-31T20:11:20.542Z'
AND content ILIKE '%Search Term%';
Right now, if I just count the rows, it just returns the number of messages with that search term. However, I'd like to count the number of Instances of the search term, rather than the number of Rows that contain the search term.
I thought of making a stored function that looped through the results of the above function and counted the instances. But it ended up being insanely slow. I’m okay with it being pretty slow, but is there a solution to this that either runs slightly faster or doesn’t require a function?