I've raised this a couple months ago and even crea...
# mongodb
e
I've raised this a couple months ago and even created a reproduction repo to prove it but for some reason my reproduction seemed pretty performant so I abandoned it BUT am I the only one who seems to get some really slow queries when trying any slightly complicated things with relation queries? My latest example is:
1
Copy code
query.OR = [
        {
          notes: {
            contains: q.toString(), // TODO: try prisma full text search once mongo is supported
            mode: 'insensitive',
          },
        },
        // check against metadata/opengraph fields
        {
          resource: {
            is: {
              website: {
                is: {
                  metadata: {
                    is: {
                      title: {
                        contains: q.toString(),
                        mode: 'insensitive',
                      },
                    },
                  },
                },
              },
            },
          },
        },
        {
          resource: {
            is: {
              website: {
                is: {
                  metadata: {
                    is: {
                      description: {
                        contains: q.toString(),
                        mode: 'insensitive',
                      },
                    },
                  },
                },
              },
            },
          },
        },
        {
          resource: {
            is: {
              website: {
                is: {
                  metadata: {
                    is: {
                      ogData: {
                        is: {
                          title: {
                            contains: q.toString(),
                            mode: 'insensitive',
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
        {
          resource: {
            is: {
              website: {
                is: {
                  metadata: {
                    is: {
                      ogData: {
                        is: {
                          description: {
                            contains: q.toString(),
                            mode: 'insensitive',
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
        {
          resource: {
            is: {
              website: {
                is: {
                  metadata: {
                    is: {
                      ogData: {
                        is: {
                          siteName: {
                            contains: q.toString(),
                            mode: 'insensitive',
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
      ];
Adding the
OR
condition to an existing query increases query time from ~4s to ~15s. Whereas writing the whole query from scratch using
aggregateRaw
its only ~1s:
Copy code
{
          $match: {
            $or: [
              {
                notes: {
                  $regex: `.*${q}.*`,
                  $options: 'i',
                },
              },
              {
                'resource.website.metadata.title': {
                  $regex: `.*${q}.*`,
                  $options: 'i',
                },
              },
              {
                'resource.website.metadata.description': {
                  $regex: `.*${q}.*`,
                  $options: 'i',
                },
              },
              {
                'resource.website.metadata.ogData.title': {
                  $regex: `.*${q}.*`,
                  $options: 'i',
                },
              },
              {
                'resource.website.metadata.ogData.description': {
                  $regex: `.*${q}.*`,
                  $options: 'i',
                },
              },
              {
                'resource.website.metadata.ogData.siteName': {
                  $regex: `.*${q}.*`,
                  $options: 'i',
                },
              },
            ],
          },
        },
Makes me question how fast my other queries would be if i just wrote them using the raw API instead 🤔
Would be great to be able to have some visibility into what mongo queries prisma generates for comparison
s
In regards to the queries generated, you should be able to see the generated query using logging https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/logging
e
Thanks. Just realised, I am actually logging all queries out but its a bit of text spaghetti so never check it. Maybe there is a way to temporarily filter only for the specific query I want to check
p
have the same issue, Prisma generates huge aggregations for simple things, like checking what to do if the field is nullish while this field is _id so it simply can't be null
and unfortunately, that's not just about reading, but about creating/updating as well, so
aggregateRaw
can't help me for such cases