How to optimise the fauna query

Hello everyone :wave:

I am working on a project where I need to integrate fauna with a serverless design using AWS lambda.

My main challenge is efficiently querying a large dataset without hitting performance bottlenecks.

Could anyone provide the best method for optimise fauna queries in a serverless setting?

Thanks in advance :pray:

Hi @Romana!

“the best method for optimizing fauna queries in a serverless setting” is a pretty broad question. If you have specific query patterns you would like help optimizing, then please share some more details about your data and how you need to work with it.

In general, though, optimizing a query is going to primarily involve setting up indexes appropriate for your query patterns. Make sure that your indexes include covered values when possible, so that you can avoid reading each and every individual document. Indexes - Fauna Documentation

There shouldn’t need to be anything special you do to configure your client with AWS lambda. We do have one important recommendation to create your client within the handler to avoid issues with Lambda freeze/thaw operations (this is in the v4 docs but applies to v10 as well): Known issues - Fauna Documentation

Thank you for your response. :smiling_face_with_three_hearts:

I work with User activity logs are included in the dataset, and it can get bigger. Filters by user ID; activity type, and time ranges are common query structures.

I am going to be sure to set up indexes properly based on your advice. Could you give examples for building indexes for these kinds of query patterns?

any advice on effectively managing scrolling in Faunadb would be very valued.

Indexes

The pager here has some examples of setting up indexes. The short answer is:

  • define index terms to query for exact match
  • define index values for sorting and for making range queries
  • add additional index values to “cover” those values and avoid fetching the documents to get more fields.

If it’s common to do all of those things at once, then you might have an index like this

// FSL
collection Activity {
  index by_user_type__time_asc {
    terms [.user, .type]
    values [.time, .title]
  }
}

and use it like this to get all activities for a give user, of type “sports”, and having a time recorded in the first week of June. I’ve also included .title as a value so we can read that from the index instead of fetching the documents.

let user = user.byId("1234")!

Activity.by_user_type__time_asc(
  user, 
  "sports", 
  { from: Time("2024-06-01T00:00:00Z"), to:  Time("2024-06-08T00:00:00Z")}
) {
  id,
  title,
}

Scrolling

Do you mean fetching additional results as you reach the bottom of a list?

Check out our docs on pagination here. Fauna’s cursor-based pagination is perfectly suited for this task.

1 Like

Thank you for providing such a helpful solution
i appreciated your efforts :slightly_smiling_face:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.