SPARQL: Finding the relation that connects two entities from a list of possible relations - sparql

I have two sets of entities, entities1 and entities2 and I want to identify all relations that link the two together, from a specified set of relations.
This can be done by declaring the relation as a variable:
?entities1 ?relations ?entities2
However, this iterates over all possible relations, therefore making the query incredibly slow and inefficient.
This could also be done by declaring the relations needed using a OR operator:
?entities1 (relation1 | relation2 | ... | relation_n) ?entities2
This finds all of the cases where entities 1 and 2 are linked by one of our relations but the relation is not returned, therefore it's not known which relation was correct.
I need a method of finding the relations between each entity from a specified set of relations that can then be returned as a table with the headers (entity1, entity2, relation).
Any ideas?

#UninformedUser kindly pointed out that a set of relations can be declared by using VALUES meaning that the desired effect can be achieved as follows:
VALUES ?relation {:relation1 :relation2 ... :relation_n} ?entities1 ?relation ?entities2 .
Thanks for your help!

Related

How to get few last rows from related table?

Need help with a orm request.
There are 2 tables. Author and Book (names for example, so you don't need to look at the logic), linked through FK.
class Book(models.Models):
title = models.Charfield(...)
class Author(models.Model):
book = models.ForeignKey(Book)
Need to group the authors by the book and go through them in a loop. The question is how to select only the last 50 authors of each book.
I can write this:
for book in Book.all()
for author in book.author_set.all()[:50]:
....
But this is not an optimal solution.
Bad answer, I didn't realize that slicing implies a new query. My bad, don't look further down.
To optimize your query, you need to use the prefetch_related() method:
prefetch_related()
Returns a QuerySet
that will automatically retrieve, in a single batch, related objects
for each of the specified lookups.
This has a similar purpose to select_related, in that both are
designed to stop the deluge of database queries that is caused by
accessing related objects, but the strategy is quite different.
select_related works by creating an SQL join and including the fields
of the related object in the SELECT statement. For this reason,
select_related gets the related objects in the same database query.
However, to avoid the much larger result set that would result from
joining across a ‘many’ relationship, select_related is limited to
single-valued relationships - foreign key and one-to-one.
prefetch_related, on the other hand, does a separate lookup for each
relationship, and does the ‘joining’ in Python. This allows it to
prefetch many-to-many and many-to-one objects, which cannot be done
using select_related, in addition to the foreign key and one-to-one
relationships that are supported by select_related.
for book in Book.all().prefetch_related()
for author in book.author_set.all()[:50]
You also need to order your book.author_set queryset to make sure you get the latest entries.

Horizontal/Vertical partitioning and relations

I've got a question regarding horizontal/vertical partitioning in a relational database.
I'm gonna demonstrate with an example:
Let's say we've got a disjoint inheritance for Person. A person can either be registered, or unregistered, but not both.
A person also has a many-to-many relation with the table House, a house can be owned by 1..* persons, and a person can own 1..* houses.
If I then were to horizontal partition the Person table, which means we have a two identical tables, but one containing the registered persons, and one containing the unregistered. How would it work with the many-to-many relation?
I've thought of also partition the relationship, but if there's an n amount of relations between person and other tables, a horizontal partition would cause the tables to grow by n * 2. Is this really the way to go?
I hope I made myself clear and thanks in advance.
There's no "right way" to do this. There are different approaches with different trade offs. However, I would start with something like this:
+------+ +-----+
|Person<----------+House|
+^----^+ +-----+
| |
| |
+---------------++ ++-----------------+
|RegisteredPerson| |UnRegisteredPerson|
+----------------+ +------------------+
Person would have an autogenerated PersonID. The child tables RegisteredPerson and UnregisteredPerson would have the same Primary Key (making the relationship 1 to 0 or 1).
It is slightly tricky with this approach to enforce a Person having exactly one RegisteredPerson or UnRegistered person. The most straightforward way is to only allow write access to the tables through stored procedures that maintain the correct invariants. There are also schemes using triggers and tagging the Person record with the subtype.
A note on terminology: in the context of databases, "horizontal" and "vertical" partitioning usually refer to storage mechanisms that are entirely unrelated to your question. This is a question about inheritance.
Laurence's answer is correct, as far as it goes. You need an entity called Person for the relationship to House as you have described the problem. Two ways of enforcing the "only-one" registration is to store the ids in the Person table. One way is:
create table Person (
. . .
RegisteredId int references RegisteredPersons(RegisteredPersonId),
UnregisteredId int references UnregisteredPersons(UnregisteredPersonId),
check (RegisteredId is null or (UnregisteredId is null)
)
(Note this allows a person to be neither registered nor unregistered, although the check can be fixed for that.)
An alternative is:
create table Person (
. . .
RegType varchar(255),
RegId int
check RegType in ('Registered', 'Unregistered')
)
Then, depending on the database, you can define a computed column or view that defines the foreign key reference. Something like this:
RegisteredId as (case when RegType = 'Registered' then RegId end),
UnregisteredId as (case when RegType = 'Unregistered' then RegId end),
Advantages and disadvantages. The first approach enforces the foreign key relationships. However, it eats up an integer of storage for each type. For two types, this isn't a big deal.
The second approach requires functionality that is not standard across databases. For instance, in SQL Server and Oracle, you can enforce the foreign key relationship -- but one does it with computed columns and one does it with filtered indexes.

How do you map a HasOne relationship with nHibernate Fluent mapping and avoid N+1?

I have 2 tables ATable and AATable where both have a shared Primary Key - ATable.aKey and AATable.aKey to represent a one-to-one relationship. For my Fluent mapping I have a HasOne Relationship defined within my Fluent ATableMapping, all of which works fine. However I have noticed that querying for ATable generates a 2nd query (N+1) for the child Table AATable. My understanding is that Hasone eager loads by default, and I had assumed this would be part of the query for ATable, but I may well have this wrong?
I have researched various solutions including using .Not.LazyLoad().Fetch.Join(), PropertyRef, ForeignKey but I cannot seem to resolve the n+1 so that either it is Eager loaded with 1 query, or Lazy loaded and I can fetch the child with my queries.
Has anyone had any issues with this or have an example they know to work with no n+1? Grateful for any advice.
You have two options:
Not.LazyLoad() which disables possibility to provide lazy loaded related entity and it would enforce NHB to provide corresponding subselect within original query
Use component mapping so both entities point to the same table. This is better approach as once you decided to fetch both entities together, generated queries hit only one table - not two like within first option. This is definitely better for performance.

How to design SQL tables to allow for multiple parent table options?

Looking into creating a series of tables in a reporting hierarchy and am more or less drawing a blank.
While tables in this structure can only have one parent, how do I structure the fields so that they point to the right parent table? As you'll see in my example below, a row's parent table can differ.
ARRANGEMENT
/ \
MATTERS ISSUES
| |
PHASES MATTERS
/ \ |
ISSUES TASKS PHASES
/ \ | / \
TITLES TASKS ISSUES TASKS TITLE
| | |
TITLES TITLES TITLE
Essentially, is it best to have each "branch" have a unique table (even though Tasks in branch 1 has the same data structure as branch 2 or 3), or is it best to have the records identify which table is their parent?
Arrangement(ID)
Matters(ParentTable, ParentID, ID)
Phases(ParentTable, ParentID, ID)
Issues(ParentTable,ParentID, ID)
Titles(ParentTable,ParentID, ID)
Tasks(ParentTable,ParentID, ID)
The above doesn't seem right to me at all. Help?
I have two opinions. You must be clear on the meaning of these "polymorphic" entities. Are they semantically different and separate? Even if they look the same, you may not want to put them into the same table if they serve different purposes.
If a Matter is truly, semantically fungible between Arrangements and Issues, then I'd suggest using a form of "Mapping" tables:
Arrangement(ID)
Matters(ID)
Issues(ID)
ArragementMatters (FK_ArrangementID, FK_MatterID)
IssueMatters (FK_IssueID, FK_MatterID)
You can continue this pattern throughout the "polymorphic" tables.
You can add a unique constraint on FK_MatterID columns if required.
It's easy to write queries:
Select * from Arrangement a
inner join ArrangementMatters am on am.FK_arrangementID = a.ID
inner join Matters m on m.ID = am.FK_matterID
to get all your Matters associated with Arrangements.
On the other hand, if a Matter under and Arrangement is NOT semantically fungible and only has the same schema, then I'd suggest creating completely separate tables:
Arrangement(ID)
ArrangementMatters(ID, FK_ArrangementID)
Issues(ID)
IssueMatters(ID, FK_IssueID)
This conveys the distinction to the world. It gives you a lot of benefits if you can separate your concerns. (i.e. Maybe you have lots of heavy usage of IssueMatters vs. ArrangementMatters - you can optimize the indexes and table layout independently.)
A query is also simpler:
Select * from Arrangement a
inner join Matters m on m.FK_arrangementID = a.ID
You can't really do that - at least not in any RDBMS I know of, if you use referential integrity (foreign key relationships), since those always have to reference one and exactly one parent table - you cannot have a FK relationship that reference parent table A in one case and parent table B in another.
In your concrete case, where a "Title" could be child of both "Phases" or "Tasks", one way to solve this would be to have a "dummy" Task for those "Title" entries that should be direct children of the "Phases" table.
Anything else will be a hack and a nightmare to maintain in the long run.
Marc
I have seen polymorphic associations of various sorts (not just parent relationships) handled that way in relational databases. I'd say go ahead and use the `ParentTable, ParentID' approach.
The downsides are that you won't be able to enforce referential integrity at the database level (ie, use foreign keys), and it's going to be a little more work to fetch association unless you're using a framework that will do the legwork for you (eg, Rails). If you really need polymorphic associations, though, I don't know any good way around those complications.
You could have 1 table with ID & ParentID.
The top level row (Arrangement) will have NULL ParentID.

Loading relations in linq2entities automatically

When i have a relation between two entities in my model:
[GroupMember] (*) ----- (1) [User]
and tries to select items from this relation with LINQ:
From entity in _user.GroupMember select entity
I always get an empty result unless I load the relation first with following statement:
_user.GroupMember.Load()
Is there a way to avoid loading the relations like this?
If you have cascading relations, you can handle them with .Include("GroupMember.AnotherTable.YetAnotherTable") which is a little nicer than having to do chained Include calls.
I just realized that when i load the User from the database, I can use Include to load GroupMember with the User like this:
Users=from entity in db.User.Include("GroupMember") select entity
But if I have several relations and maybe wants to access relations on the relations, this gets very ugly.
So I am still looking for a better/nicer solution to my issue.