score:2
adding cacheable before the count will cause the aggregate count results to be cached. this can be seen by the sql generated from my example below.
domain and mapping code
public class entity
{
public virtual long id { get; set; }
public virtual string name { get; set; }
}
public class entitymap : classmap<entity>
{
public entitymap()
{
id(x => x.id).generatedby.identity();
map(x => x.name);
cache.readonly();
}
}
the test code itself.
using (var session = nhibernatehelper.opensession())
using (var tx = session.begintransaction())
{
session.save(new entity() { name = "smith" });
session.save(new entity() { name = "smithers" });
session.save(new entity() { name = "smithery" });
session.save(new entity() { name = "smith" });
tx.commit();
}
string name_constant = "smith";
using (var session = nhibernatehelper.opensession())
using (var tx = session.begintransaction())
{
var result = session.query<entity>().cacheable().count(e => e.name == name_constant);
}
using (var session = nhibernatehelper.opensession())
using (var tx = session.begintransaction())
{
var result = session.query<entity>().cacheable().count(e => e.name == name_constant);
}
the sql generated from the above code can be seen below. as you can see, there are four insert
statements, one for each session.save
. whereas there is only one select
despite the two queries, each performing under a separate session. this is because the result of the count has been cached by nhibernate.
nhibernate: insert into [entity] (name) values (@p0); select scope_identity();
@p0 = 'smith' [type: string (4000)]
nhibernate: insert into [entity] (name) values (@p0); select scope_identity();
@p0 = 'smithers' [type: string (4000)]
nhibernate: insert into [entity] (name) values (@p0); select scope_identity();
@p0 = 'smithery' [type: string (4000)]
nhibernate: insert into [entity] (name) values (@p0); select scope_identity();
@p0 = 'smith' [type: string (4000)]
nhibernate: select cast(count(*) as int) as col_0_0_ from [entity] entity0_
where entity0_.name=@p0;
@p0 = 'smith' [type: string (4000)]
the possible scenarios which will cause nhibernate to ignore cacheable
and go back to the db are when:
- the second level cache is not enabled in the session factory configuration.
- the entity has not been marked as cacheable.
the only other scenario i know of that will cause nhibernate to perform two select
queries is when the entity has been evicted from the cache, either explicitly using sessionfactory.evict
or by the cached entity becoming expired between the two calls.
Source: stackoverflow.com
Related Query
- How to select the least count of items in fluent nHibernate via a linq query?
- How to Count Duplicates in List with LINQ
- How are people unit testing code that uses Linq to SQL
- How to count the number of elements that match a condition with LINQ
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How do I count the number of child collection's items using LINQ Method Syntax?
- Getting count with NHibernate + Linq + Future
- NHibernate Second Level Cache With NHibernate Linq Provider 1.0
- How to do a case-insensitive string where in NHibernate Linq query?
- If it's bad to use inline SQL, how does using LINQ to perform queries differ in practice?
- How can I directly execute SQL queries in linq
- NHIbernate (3.1) - Linq group by then order by count issue
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to Union two Queries in LINQ to Fluent NHibernate?
- How do I combine these two linq queries into a single query?
- How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?
- How do I combine multiple linq queries into one results set?
- How and When LINQ Queries are Translated and Evaluated?
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- How do I append LINQ queries to each other?
- How do I use LINQ to count number of objects in largest group?
- How to set Nhibernate LINQ Command Timeout using Session.Query
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- Linq Paging - How to incorporate total record count
- How can I combine this code into one or two LINQ queries?
- How to use group by and having count in Linq
More Query from same tag
- Task.Any() Call task twice
- trying to seed my database using code first migrations
- Multiple outer Linq join
- Select quoted data from query without referencing a table?
- How to sort string Array based on Number?
- How do I use LINQ's LIKE function for invalid character validation?
- How to get 'named' tuple components inside Linq queries?
- Trying to get a random sort order
- How to check if a char array contains every element in another char array?
- EF Core 3.0 conditional order by in LINQ when the condition is validated in DB
- Filtering results by user name
- Entity Framework nesting variables
- forcing LINQ-to-Entities + SQL Server 2014 to conditionally run a subquery
- Getting all record by using a reference number in LINQ
- String or binary data would be truncated. The statement has been terminated.(Linq to SQL)
- Get children and grand-children at all levels for specified parent
- Linq to XML Add element to specific sub tree
- Linq add new variable in query
- LINQ distinct on class item?
- Cannot iterate through two different collections simultaneously c#
- Linq to objects - Search collection with value from other collection
- CosmosDB - DateTimeOffset Issue with UTC Querying
- Parse a XAML positions string into a list of Point3D
- How to scan XElement and put all the element ( with value ) in Dictionary?
- Max(x=>x.Time) vs OrderByDescending(x=>x.Time).First().Time
- Generate missing list values in c# list using Linq
- Figure out max number of consecutive seats
- How to use LINQ with ServiceModel.Channels.Message
- LINQ aggregating multiple IEnumerables into one?
- Compiled LINQ queries - NHibernate