Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to specify no-tracking to reduce caching on demand #524

Merged
merged 11 commits into from
Jan 26, 2024

Conversation

MikeAlhayek
Copy link
Collaborator

Fix #523
Fix #522

@hishamco
Copy link
Contributor

Can we add it as an extension method AsNoTracking or WithNoTracking instead of passing a context in each method

@MikeAlhayek
Copy link
Collaborator Author

@hishamco I thought about it originally. The problem if we do that, we would have to track the state whether to track the entities per session not per query. The idea is to be able to not track the results per-query not per-session. Like what if the users wants to first query non-trackable documents, and in the same scope, they want to query other trackable documents? If they don't remember to set the state before the second query, both queries will not be tracked.

I simplified things by adding WithNoTracking() method of the IQuery<T> so if you are building a query, you can use this method to pass instruction to not track the entries.

But, in the Session, I think it is better to provide QueryContext that would change the behavior of the query on demand.

@MikeAlhayek MikeAlhayek changed the title Ma/add query context Add a way to specify no-tracking to reduce caching on demand Dec 27, 2023
Copy link
Contributor

@hishamco hishamco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now looks much better for me, waiting for Seb comment

src/YesSql.Abstractions/IQuery.cs Outdated Show resolved Hide resolved
src/YesSql.Abstractions/IQuery.cs Outdated Show resolved Hide resolved
@MikeAlhayek
Copy link
Collaborator Author

MikeAlhayek commented Dec 29, 2023

Now looks much better for me, waiting for Seb comment

@hishamco If you spent time to review a PR, you may want to consider approving what you think is ready. This way, any changes you request will be to eventually to approve the PR which is more helpful than just leaving comment :)

@hishamco
Copy link
Contributor

Just fixing a small comment then I will approve

LGTM

@sebastienros
Copy link
Owner

Internal caching can be very expensive when dealing with a large data set.

I don't think it adds much more overhead since it's only adding the items to a collection, and "dealing with a large dataset" means you will also do that. You should just get rid of the session when you are done with it and you don't want to keep the references around.

However I can see why you would not want to have to create a new Session for every page of data you load, by convenience, and you came up with the notion of QueryContext. I think it should still be in the Session and not at the query level. This way you create a session that doesn't save the state and can reuse it to do all your queries. So I would suggest to just add a property to the session to disable tracking altogether. And another advantage is that the object won't even be added to the identity map at all, so potentially even faster processing. This would be an optional bool in CreateSession. I don't think it matter to be able to change the state of the session to enable/disable tracking dynamically. Just create a new session if you need different characteristics.

Also can you rename DetachRange to Detach(IEnumerable<object>) instead? Unless you have an argument that makes it better.

@MikeAlhayek
Copy link
Collaborator Author

@sebastienros I made the requested changes.

@sebastienros sebastienros merged commit 11a51bf into main Jan 26, 2024
1 check passed
@sebastienros sebastienros deleted the ma/add-query-context branch January 26, 2024 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants