In my database (mongoDB) I have a model called exam and each instance of the modal has a somewhat large json object (500k).

I'm using strapi cms to make the query, using graphql plugin.

However when fetching all the ids of the model, the fetching is extremely slow even though i'm only interested in the ids and the json object inside each instance (using graphql):

query {
                    exams {

This is in comparison with fetching of other models which is very fast.

Does the database read through the whole content of each instance of a model? Is there a way to change this behaviour?

This is not a bug, just asking for help.

  • I would expect that yes, a document database needs to load the document even when only extracting a part of the document. The best way to deal with this might not be to change the database but to stop querying data from all documents in a collection. Wouldn't the top 50 results do? – amon Feb 17 '19 at 17:08
  • Thanks amon yes but the problem is that even on fetching other instances of a model that is realted to the exam model, it's also very slow, just because they have relationships. In that case I cannot query only the top 50, because for example I have only two models that have relationships with this Exam model, so qurying just the ids of these two models, is also slow – sir-haver Feb 17 '19 at 17:26
  • 3
    Since you have a few layers involved (Strapi CMS and GraphQL), I would try to figure out what actual queries are being sent to MongoDB. If these are slow queries, they should be included in the MongoDB log based on the slowms threshold (100ms by default). You could also enable the Database Profiler to collect details about queries being executed. You may be missing an ideal index to support your queries. – Stennie Feb 18 '19 at 10:01
  • 3
    Large documents will add some overhead on server memory, since documents to be returned will need to be read into memory unless you are able to create a covered query. However, if you have suitable indexes and use projection to limit fields returned from the query, performance should be significantly better than a naive collection scan. A suggested first step would to be find and explain() your queries in order to understand index usage. – Stennie Feb 18 '19 at 10:01
  • Thanks a lot Stennie! That's a lot for me to grasp, I'll dive into it too see where the problem is – sir-haver Feb 18 '19 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.