Read uncommitted when using Rob Conery's Massive - isolation-level

Is there a way to do a read uncommitted when using Rob Conery's Massive without writing your own query?
It is for a site that is mostly read-only. A CMS type of a site.

The cheat way to do this is when you are doing your declaration for the table. Pass in a (nolock) hint:
var dirtytbl = new DynamicModel("northwind", tableName:"dbo.Products (nolock)", primaryKeyField:"ProductID");
If you don't want to have established dirty models and clean models, for the what I hope to be rare times you are doing this, you can just go straight to SQL:
var dirtyresult = tbl.Fetch("set transaction isolation level read uncommitted; SELECT * FROM Categories c INNER JOIN things t on c.id = t.id ");
I use the above code for system objects and some other behind the scenes stuff where I know it won't matter, but you should always be very cautious and know what you are getting into when you drop to that isolation level.
See blog post How dirty are your reads?

Related

Run complex SQL Query with spring boot repositories

i have a little complex scenario using spring data and jpa currently.
My data structure is like:
And i like to create a filter: give me all events which belongs to a list of structures, within a given period and is assigned to a list of categories.
I was able to create a sample SQL statement:
select * from event e inner join period p on e.period_id=p.id
inner join category_item_ids ci on e.id=ci.item_ids
inner join category c on ci.category_id=c.id
inner join event_assignment_structure_ids es on es.event_assignment_id=e.id
where p.start between '2020-05-01 12:00:00.000000' and '2020-11-14 12:00:00.000000' and
c.id in (1) and
es.structure_ids in (1,2)
But my objects are currently not wired together by all the JPA annotations.
e.g. the "Same ID" is something i did by convention to make the parts a little more independend.
So using the JPA way is currently no option i guess due to the missing relations.
Introducing them will be also quite a lot of work.
So i was wondering if there is a possiblity to run the sql query directly (i could use native query, but thats also no option, cause the filter values are not always given, so i need 13 native queries)
I used enityManager and query Builder in the past, but thats also no option due to the missing jpa relations.
Any ideas are much welcome :-)
Regards
Oliver

How to map a collection efficiently in an ORM?

I have a simple model containing two entities:
Post entity - an aggregate root, which can have a list of Comment -> Comments
Comment entity
When adding a new comment, lets say it's this:
post.Comments.Add(newComment);
System will request a SELECT to load all existing comments before adding the new one. But if there are hundreds of comments so it's heavy right. Is it possible to avoid the load? Thanks for any suggestion.
This is a classic problem with ORMs. There is no silver bullet here.
If you load all the comments in advance you're causing a lot of bandwidth wasted since all the comments might not be needed.
On the other hand, if you don't load the comments in advance and someone does something like:
for(var i = 0; i < 100; i++){
doSomethingWith(post.Comments[i]);
}
You're causing a hundred fetches from the database - effectively causing the Select n+1 issue.
So, it's an opinionated design choice, some ORMs will only fetch the post, others will include special syntax like .Include which indicates you're also interested in the comments and some will ignore this completely and waste requests.
You're concerned about performance of inserting a new comment, so maybe below options can help:
Reuse the db connection from nhibernate, write your own insert command
With support of Dapper, write your insert query https://github.com/StackExchange/dapper-dot-net

Left Join 1 to 1/0 with llblgen?

With EF, if you navigate to a singular related entity within a select projection(such as from the many side of a many-to-one or 1-to-1/0) it would coalesce nulls and give you a left join: https://stackoverflow.com/a/2525950/84206
Since it occurs in a project and not in a join, EF makes a pretty reasonable assumption that a left join is desired.
However, I haven't found a way to accomplish this in LINQ with LLBLGen. The above technique produces an inner join with LLBGen. I can't use techniques that use DefaultIfEmpty because that's only available when navigating into a many relationship.
I am hoping to avoid using WithPath/Prefetch because I'd really like to do the projection in LINQ instead of grabbing a huge object graph into memory and do the projection in memory.
This is LLBLGen 3.5.
If the FK is nullable, the join will be a left join. If the FK isn't nullable, it will be an inner join. This is the only way it's determinable what you want as Linq lacks any other system to specify the join type in this. Your link must use a nullable (optional) FK side as well to get a left join.
If nothing helps, please use queryspec, the query api will allow you to specify the join type in any case.
ps: please next time post on our forums, we don't monitor SO every day, but we do monitor our forums.

When to use JOIN and when not to in LINQ to entities

I am new to Linq and I have seen that if there are multiple entities, some use the multiple FROM syntax like this:
from h in db.Hubs
from ch in h.CityHubs where ch.Cities.CityID == 1
select
and some use the explicity join syntax.
from h in db.Hubs
join ch in da.CityHubs on h.CityId equals ch.CityId
select
If I am using Linq to entities, which one should I use? If I were to use Linq to objects, which one should I use?
As a rule, in Entity Framework, if you have a proper model and properly set up navigation properties for foreign keys, you should almost never use join - instead you access your navigational property directly and EF will generate the necessary join in the SQL code for you. I recommend taking a look at #Craig Stuntz's blogpost regarding this issue.
Regarding Linq-to-objects, however, it depends on the particular query you are writing.

Entity Framework: Doing large queries

I'm probably addressing one of the bigger usability-issues in EF.
I need to perform a calculation on a very big part of a model. For example, say we need a Building, with all of its doors, the categories of those doors. But I'd also need the windows, furniture, roof etc.
And imagine that my logic also depends on more coupled tables behind those categories (subcategories etc.).
We need most of this model at a lot of points in the code, so I'd need to have the whole model filled and linked up by EF.
For doing this, we are simply querying the ObjectContext and using type-safe includes.
But this gets inpractical and error-prone.
Does anyone have suggestions for tackling this kind of problems?
Use projection to get only the values you need, especially if you don't intend to update everything. You probably don't need every property of a piece of furniture, etc. So instead of retrieving the entity itself, project what you want:
from b in Context.Buildings
where b.Id == 123
select new
{
Name = b.Name,
Rooms = from r in b.Rooms
select new
{
XDimension = r.XDimension,
// etc.
Now you no longer have to worry about whether something is loaded; the stuff you need is loaded, and the stuff you don't need is not. The generated SQL will be dramatically simpler, as well.

Resources