This approach is evidently very inefficient since it introduces a classic n+1 problem, where we fetch the attributes for the selected category and then loop over each of them and again fetch the count for each attribute option.
I can't seem any other solution other than writing a raw SQL query for this kind of problem. Or am I missing something?