Does it make sence to use data loader for multiple counts?

Does it make sence to use data loader for multiple counts?


0

I’m implementing a Graphql resolver for complex datatype. I try to use a data loader to avoid N+1 problem. I have a datatype with a counter. Therefore, in one graphql query I need to perform user counting multiple times for defferent types. Currently, I’m trying to combine multiple COUNT(*) queries within one SQL query. See the pseudocode below.

Pseudo code

# My type
type MyType {
  users_count: number
}

# The query returns an array of "MyType" 
getMyType(id: ID!): [MyType!]!

# Graphql query
getMyType(id: ID!) {
    id
    users_count
    created_at
}

# This is how I'm trying to do multiple counts
# 
SELECT 
    (SELECT COUNT(*) FROM table_with_users WHERE my_type_id = 1),
    (SELECT COUNT(*) FROM table_with_users WHERE my_type_id = 2)

The problem is I may have dozens of counts in one query. Is this going to be efficient for DB? What is the most efficient way of counting in this case?


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *