I am trying to select a edge "Contained" that points to a "Rgb" vertex, in OrientDB 2.1, with PhpOrient. The edge haves a property "probability", and the Rgb haves a property "hex".
So:
Artist -> created -> Artwork -> contained -> Rgb.
This query finds the edge "Contained" with the property "probability".
SELECT *, EXPAND (OUT("Created").OUTE("Contained")) FROM Artist WHERE path="Achebach, Andreas"
This query finds the vertex Rgb with the property "hex".
SELECT *, EXPAND (OUT("Created").OUT("Contained")) FROM Artist WHERE path="Achebach, Andreas"
But I want to have both properties. How can I combine the queries to have one query, that can find both properties?
In v 2.1 you can do it with a subquery:
SELECT probability, inV().hex FROM (
SELECT expand(out("Created").outE("Contained"))
FROM Artist WHERE path="Achebach, Andreas"
)
In OrientDB v 2.2 you can also do it with pattern matching
MATCH
{class:Artist, as:a, where:(path="Achebach, Andreas")}
.out("created"){as:aw}
.outE("contained"){as:cont}
.inV(){as:rgb}
RETURN cont.probability, rgb.hex
Related
Let's imagine we have a table containing columns for 'color' and for 'size'. I have a list with color-size combinations (e.g. [(red, small), (blue, medium)]), of which the length is unknown. The table should be filtered based on this list, so that the result contains only the rows where the combinations apply.
A query based on the example would look like this:
SELECT * FROM items WHERE ((color = 'red' AND size = 'small') OR (color = 'blue' and size = 'medium'));
Parameterizing this query wouldn't work of course, since the amount of combinations varies.
Is there a way to achieve this using the parameterized queries like the ones that are use in node-postgres? The only solution I can think of is using string interpolation, which doesn't appear to be a safe.
It looks like good scenario for IN operator
select * from items where
(color, size) in (('red','small'), ('blue','medium'))
and it can be parametrized using arrays
select * from items where
(color, size) in (
select unnest (array['red','blue']), unnest(array['small','medium']))
First array is for colors, second for sizes. Unnest in one select create pairs.Arrays should have the same number of elements.
And arrays can be passed as parameters to query.
Let's say I have the following list of vertices (connected by edges) in the orient database:
[t=1] --> [t=2] --> [t=3] --> [t=4] --> [t=5] --> [t=6] --> [t=7]
Each vertex has a timestamp t. I now want to receive the last vertex before a given date. Example: give me the last vertex before t=5, which is t=4.
Currently I'am using the following query to do this:
SELECT FROM ANYVERTEX WHERE t < 5 ORDER BY t DESC LIMIT 1
This is working fine when having up to let's say 1000 elements but the performance of that query drops with the number of elements inserted in the list. I already tried using an index, which improved the overall performance, but the problem, that the performance drops with the amount of elements still persists.
When building queries, always try to use the information you have about the relationship in your query to improve performance. In this case you don't need the sort (which is an expensive operation) because you know that the vertex you need has an incoming edge to the vertex, you can simply use that information in your query.
For example, let's say I have the following setup:
CREATE CLASS T EXTENDS V
CREATE VERTEX T SET t = 1
CREATE VERTEX T SET t = 2
CREATE VERTEX T SET t = 3
CREATE VERTEX T SET t = 4
CREATE VERTEX T SET t = 5
CREATE CLASS link EXTENDS E
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 1) TO (SELECT * FROM T WHERE t = 2)
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 2) TO (SELECT * FROM T WHERE t = 3)
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 3) TO (SELECT * FROM T WHERE t = 4)
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 4) TO (SELECT * FROM T WHERE t = 5)
Then I can select the vertex before any T as such:
SELECT expand(in('link')) FROM T WHERE t = 2
This query does the following:
Select the vertex from T where t=2
From that vertex, traverse the incoming edge(s) of type link
expand() the vertex from which that edge comes from to get all of its information
The result is exactly what you want:
This should give better performance (especially if you add an index on the attribute t of the vertices) because you are using all the information you know in advance about the relationship = the node you need has an edge to the node you select.
Hope that helps you out.
I want to get just radius value from ST_MinimumBoungingRadius.
Something like this (from postgresql documentation) works just fine:
SELECT radius
FROM
ST_MinimumBoundingRadius('MULTIPOINT(1 2,3 8,5 6)')
So I don't understand, why doesn't work similar query on existing table:
SELECT radius
FROM
(SELECT
ST_MinimumBoundingRadius(ST_Collect(geom)) minrad
FROM a) b
Result of this query is ERROR: column "radius" does not exist
Is there any way to extract just radius value?
The main difference is that in the first case you are calling the function in the FROM clause while in the second it is in the select clause. In the first case, the result is made of two column while in the later it is a string aggregation of all columns.
You can fix it by using the function in the FROM clause again, using either a double-parenthesis or a lateral join:
SELECT radius
FROM ST_MinimumBoundingRadius((SELECT ST_Collect(geom)
FROM a)) minrad;
or
SELECT radius
FROM (SELECT ST_Collect(geom) geom FROM a) tbla,
LATERAL ST_MinimumBoundingRadius(tbla.geom) minrad;
I have a query in Orientdb like this:
select out('E_MyEdge').#rid, creationUser from V_MyVertex where out('E_MyEdge').#rid <> creationUser
V_MyVertex is a vertex, E_MyEdge is an edge. creationUser is a property of type "LINK" from V_MyVertex, which is an "rid" (something like "#12:4"). The problem is both columns from result are kind of "rid", but the query returns also the rows which have the same value (for example #12:117 | #12:117), and I don't know how to exclude them from the result.
You could use
select out('E_MyEdge').#rid, creationUser from V_MyVertex where out('E_MyEdge').#rid NOT IN creationUser
Hope it helps
Like lightweight edges, it seems like when a new vertex subtype is added to the schema then it is "invisible" until it has a concrete instance, ie:
create class Invisible extends V
create property Invisible.name string
select distinct(#class) from V
will return 0 results until I
create vertex Invisible set name='Not invisible anymore'
Is there a way around this?
select name from ( select expand(classes) from metadata:schema ) where superClass='V'
Thanks, Lvca