I know that the data stored in Session is alive until the user closes the browser while the data stored in Items is alive until the request/response is processed.
I also know that if I call Session.Abandon() it will make the Session behave like Items.
I made some tests where I've put an Entity Framework DbContext (to keep the connection to the sql server alive) in Session in the first case and in the second case in Items, so if it does no exist I instantiate it otherwise I get it from one of these storages.
I saw that the queries on the context from the Session are faster than the queries on context from the Items.
It's weird because I inspected the Dispose() method from the context in both cases and it's called as expected - at the end of Session or Items.
Now, if I'm calling Session.Abandon() then the queries are the same for both cases in terms of execution speed.
What is happening here, why am I facing this situation?
Since the context is already there, and the query has been executed already, it is probably cached, which in turn would improve execution time.
You should read about cold vs warm queries, and what impact it can have on your application.
The very first time any query is made against a given model, the Entity Framework does a lot of work behind the scenes to load and validate the model. We frequently refer to this first query as a "cold" query. Further queries against an already loaded model are known as "warm" queries, and are much faster.