Query that was taking 14s now taking 13 mins

Yesterday while working on a project that includes a next.js front end and an express backend/api.

I have 4 collections in my DB. I am simply trying to query the products collection and get all products. There are ~15k documents in the collection. I did not change anything to do with the query itself, just changes to the font end. One second the query was working fine, the next it was taking minutes to finish.

I have no clue where it went wrong. If anyone has had this happen before let me know.

here is my query:

query Products {
  products {
    category
    code
    id
    name
    sku
  }
}

here is my resolver:

products: async () => {
      return await Product.find().lean(true)
    },

here is my shcema:

 type Product {
    id: ID!
    name: String
    code: String
    sku: String
    category: String
    stores:[ProductStore]
    vendors: [ProductVendor]
  }

  type ProductStore {
    name: String
    price: Float
    onHand: Int
    onOrder: Float
    sales: Float
  }

  type ProductVendor {
    name: String
    cost: Float
    quantity: Float
  }

I went back and tested the queries in Apollo Server and ran into the same issue. All other calls to my other collections are working normally.

Any help would be appreciated.

Thanks

That’s a lot of documents to load all at once! I would recommend adding some pagination into this field and returning a subset. A typical UI won’t be able to display 15k records at once, so I would start there. This would bring the 14s down a lot.

As for it taking minutes now vs 14s, it’s really hard to say without knowing:

  • The DB you are using,
  • The configuration you have of the DB
  • How the express backend is hosted
  • If the services you are using have any caching factors.

15k rows is a lot of data a difference of seconds to minutes could easily be some cache being involved.

We also started up an Apollo Discord server that you can join and we can talk about all this more. I would be happy to dig more into the pagination aspects!