FerretDB icon indicating copy to clipboard operation
FerretDB copied to clipboard

Support limit pushdown when filter and sort can apply pushdown entirely

Open cvbhjkl opened this issue 8 months ago • 4 comments

What should be done?

Current limit pushdown is applied only when filter and sort is not set. This could be loosen so that limit pushdown can apply to more find commands. If both filter and sort can apply pushdown entirely, then limit can also pushdown without causing errors.

There might be two options: 1.implement a function which accepts params.Filter and params.Sort as inputs, determines if there are any parameters that can‘t apply pushdown. If params.Filter and params.Sort can both apply pushdown entirely, then limit can also apply pushdown 2.reuse current logic and make some changes to function prepareWhereClause() and prepareOrderByClause(). Determine whether limit should be pushed down after these two funcitons

Any thoughts?

cvbhjkl avatar Oct 25 '23 08:10 cvbhjkl

@cvbhjkl what is your use case for that?

AlekSi avatar Oct 25 '23 08:10 AlekSi

For example, considering an instant message app, its user in-box looks like:

type Inbox struct {
	MessageID   int64     `bson:"messageID,omitempty"`
	Target      string    `bson:"target,omitempty"`
	Message     string    `bson:"message,omitempty"`
}

and we want to get one new message for user1, Golang code is:

filter := bson.D{{"target", "user1"}}
Coll.FindOne(context.TODO(), filter)

In this case, filter pushdown is applied but limit pushdown is not applied due to filter is set. But limit pushdown could be applied since filter pushdown is applied entirely, which will make find command much faster when in-box is quite large.

In fact, in production environments, it is rare to use limit alone. In most cases, limit is used in combination with filter and sort. If limit can also be pushed down when filter and sort can apply pushdown, it will help a lot in terms of performance.

cvbhjkl avatar Oct 25 '23 09:10 cvbhjkl

Okay, let's do it, but very carefully, with tests and probably in a few smaller PRs

AlekSi avatar Oct 25 '23 09:10 AlekSi

@cvbhjkl any news? Anything we could help you with?

AlekSi avatar Oct 30 '23 08:10 AlekSi