0

I have the following query method.

findAllByProfile_UserIdContainingAndOrigin

There is an index for profile.userId but the query is extremely slow. I suspect that because of the Containing keyword the index is not being used efficiently since it has to check the profile.userId property of all documents for similar properties.

Is my suspicion correct? Is there a way to test this conclusively in a dev environment?

Clarification: The method has no body, it's a query method that is automatically converted into a query. https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html

Example of the executed query:

{ "profile.userId" : { "$regularExpression" : { "pattern" : ".12345.", "options" : ""}}, "origin" : "user"} fields: Document{{}}

10
  • 1
    How can one answer your question without knowing the content of your method? Commented Jan 18, 2024 at 12:21
  • @WernfriedDomscheit The method has no body, it's a query method. It is automatically derived from the method name. docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html Commented Jan 18, 2024 at 12:50
  • 1
    Check the query execution plan. I don't think $regularExpression, uses index at all Commented Jan 18, 2024 at 13:30
  • @CharchitKapoor I checked in MongoDBCompass and it increments the index usage count whenever this query is run. Could it be that this increases even if the index isn't being used? Commented Jan 18, 2024 at 20:05
  • Maybe. Can you check the output of explain, as mentioned here. mongodb.com/docs/manual/reference/method/db.collection.explain Commented Jan 19, 2024 at 8:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.