Best practices
Performance optimization
Start with the elements that will give the less results
Whenever you are creating prepared queries or writing queries in the Admin UI, you should remember that the less "starting points" or "roots" you find, the more efficient your query will be.
You should remember that CogTL reads CoQL queries from left to right, therefore, the left part of your query should give as few results as possible, even if it may be a unnatural way of writing the query.
For example, if you are looking for all Citizens
that live in a particular country, a natural way of writing the query could be:
.type="Citizen" -(lives in)-> Spain
The above query is correct, but will iterate on every citizen of the knowledge graph, and navigate from this citizen to a country, and evaluate whether this country has "Spain" in one of its attribute. That means that CogTL will load from the database a huge amount of entities and relations that won't be part of the final search results.
Therefore, the most efficient way of writing such a query would be:
.type="Country" name="Spain" <-(lives in)-
We have several optimizations here:
- First, we will search very precisely for the Country named Spain. This will be extremely efficient.
- Then from this entity only, we will iterate on all incoming relations of type
lives in
, and keep the entity that is on the other side of this relation, without performing any supplementary test.
Note that we then assume here that only entities of type Citizen
live in a country. If our model also contained objects of type Animal
for example, we may have to precise the entity type that we wish to keep in the query (.type="Citizen"
).
Tag and re-use
When you want to implement multiple assertions that share a specific condition, you should implement this specific condition as a separate assertion, and use its result in the others (typically through the usage of a tag).
For example: let's say that you have two rules:
- A person that belongs to the HR team should not have access to the production systems.
- A person that doesn't belong to the HR team should not have access to the payroll.
In that case, the best practice is to:
- Create an assertion
A person belongs to the HR
, that will find the right users (e.g. through a navigation in the org chart), and as a result will put a tag#HR
on the concerned people. - Create an assertion
A person that belongs to the HR should not have access to the production systems
, that uses this tag#HR
in the Subject condition as a starting point, and then looks for the other conditions. - Create an assertion
A person that doesn't belongs to the HR should not have access to the payroll
, that uses this tag#HR
in the Subject condition as a starting point also but with the checkbox "NOT set", and then looks for the other conditions.
Even if it requires to create a supplementary assertion, you will gain in modularity and global performance, as CogTL can only recompute the minimum whenever the underlying knowledge changes.