Message from discussion
How to query nodes with multiple relationships?
Received: by 10.204.10.88 with SMTP id o24mr552797bko.0.1344618794800;
Fri, 10 Aug 2012 10:13:14 -0700 (PDT)
X-BeenThere: neo4j@googlegroups.com
Received: by 10.204.4.74 with SMTP id 10ls3515738bkq.0.gmail; Fri, 10 Aug 2012
10:13:12 -0700 (PDT)
Received: by 10.205.130.14 with SMTP id hk14mr550374bkc.5.1344618792561;
Fri, 10 Aug 2012 10:13:12 -0700 (PDT)
Received: by 10.205.130.14 with SMTP id hk14mr550373bkc.5.1344618792538;
Fri, 10 Aug 2012 10:13:12 -0700 (PDT)
Return-Path: <michael.hun...@neopersistence.com>
Received: from mail-bk0-f50.google.com (mail-bk0-f50.google.com [209.85.214.50])
by gmr-mx.google.com with ESMTPS id q3si1177314bkv.1.2012.08.10.10.13.12
(version=TLSv1/SSLv3 cipher=OTHER);
Fri, 10 Aug 2012 10:13:12 -0700 (PDT)
Received-SPF: pass (google.com: domain of michael.hun...@neopersistence.com designates 209.85.214.50 as permitted sender) client-ip=209.85.214.50;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of michael.hun...@neopersistence.com designates 209.85.214.50 as permitted sender) smtp.mail=michael.hun...@neopersistence.com
Received: by bkwj5 with SMTP id j5so756138bkw.9
for <neo4j@googlegroups.com>; Fri, 10 Aug 2012 10:13:12 -0700 (PDT)
d=google.com; s=20120113;
h=from:mime-version:content-type:subject:date:in-reply-to:to
:references:message-id:x-mailer:x-gm-message-state;
bh=ueVz7I5nGGEQCvGbdm/gmZGBJmsqW2tz6YKN8lJV8xc=;
b=RUcEohhw5wjqc0oLafTNkMLGVuCDaxkWCxg9tKy1JEre86SKehebLylB2W+0zoeRTv
MI99sLK8Fe9VsAPEDMD5ThV/7SoXCIM3vTTW02CjW4gkFN0qBM+VgwYW42b1PGEjM21+
ZKJLJ8aVwwVrAS0ojapK54ghGAEmuScvh90PPQUnyycnBy3ow55n1H5gubS4XZgWx8Ee
ISugi0uifFleLSpzJMgy4KYSe7ni267JAykNoNw4LXEo/wVaiXXfYdR41NnI3L29eS4u
N3mazGCt6CQ+hVDDUm2WaPIIv6xpPqu8HoAthcA8+9t/28PZiXP+b7em5675HiQtYg+M
5eiw==
Received: by 10.204.149.86 with SMTP id s22mr1525770bkv.1.1344618792194;
Fri, 10 Aug 2012 10:13:12 -0700 (PDT)
Return-Path: <michael.hun...@neopersistence.com>
Received: from [192.168.111.100] (146-52-36-225-dynip.superkabel.de. [146.52.36.225])
by mx.google.com with ESMTPS id 25sm2358444bkx.9.2012.08.10.10.13.10
(version=TLSv1/SSLv3 cipher=OTHER);
Fri, 10 Aug 2012 10:13:11 -0700 (PDT)
From: Michael Hunger <michael.hun...@neotechnology.com>
Mime-Version: 1.0 (Apple Message framework v1084)
Content-Type: multipart/alternative; boundary=Apple-Mail-90--908114378
Subject: Re: [Neo4j] How to query nodes with multiple relationships?
Date: Fri, 10 Aug 2012 19:13:09 +0200
In-Reply-To: <7f2fa1cf-d75d-4e5e-8e8d-532a2c3f90e1@googlegroups.com>
To: neo4j@googlegroups.com
References: <69c0483d-c115-4a20-b986-fb3523f3c2ff@googlegroups.com> <78ED9D6D-0A4D-4BD4-B317-AD71CF130...@neotechnology.com> <7f2fa1cf-d75d-4e5e-8e8d-532a2c3f90e1@googlegroups.com>
Message-Id: <6F0DE8CE-D53B-4AA8-BC49-4CF22FC5F...@neotechnology.com>
X-Mailer: Apple Mail (2.1084)
X-Gm-Message-State: ALoCoQmGChCePMkOAChxq6XkehON06Qu5DVtc4t3NDW1OznbLOiIItIkCXoaK66otlsqy416/DcP
--Apple-Mail-90--908114378
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
you can write the first one like this:
> start a=3Dnode(1405) match a-[:A*1..3]->m
> where a-[:B*1..3]->m
> return distinct m.name
Please remember that distinct is an expensive operation (in term of =
non-lazyness and memory consumption) as it has to fetch all data and =
merge the results in memory.
You may at least try to run it without distinct to see the difference.
Michael
Am 10.08.2012 um 18:59 schrieb fli.rpx:
> Hi Michael,
>=20
> Thanks for your reply.
>=20
> There are differences between the two queries. The first query, "start =
a=3Dnode(1405) match a-[:A*1..3]->m, a-[:B*1..3]->m return distinct =
m.name", has AND relationship. Its result set will be the intersection =
of two traversals.
>=20
> The second query, "start a=3Dnode(1405) match a-[:A|B*1..3] return =
distinct m; ", is OR relationship.=20
>=20
> These two queries have performance difference. The first one will take =
two traversals then compare. The second one only has one traversal.
>=20
> I am wondering if I can write the first query in a way of the second =
query. Something like this?:
>=20
> start a=3Dnode(1405) match a-[:A&B*1..3] return distinct m;
>=20
>=20
> Thanks,
>=20
> Fudong=20
>=20
>=20
>=20
>=20
> On Thursday, August 9, 2012 2:16:52 PM UTC-7, Michael Hunger wrote:
> I don't really understand your question?=20
>=20
> Does the second query not work for you?=20
>=20
> Michael=20
>=20
> Am 09.08.2012 um 21:48 schrieb fli.rpx:=20
>=20
> > I am trying to write a query to return nodes with two relationships.=20=
> >=20
> > One way to do it: start a=3Dnode(1405) match a-[:A*1..3]->m, =
a-[:B*1..3]->m return distinct m.name;=20
> >=20
> > This will work, but takes much a lot more time than a query like =
this: start a=3Dnode(1405) match a-[:A|B*1..3] return distinct m;=20
> >=20
> > I think maybe neo4j does two traversals in the first query.=20
> yes it does and their results span up a cross product which is =
filtered down by distinct again.=20
> >=20
> > Is it possible to do a query to get nodes with both relationships =
like the second query?=20
>=20
--Apple-Mail-90--908114378
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
charset=us-ascii
<html><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; ">you can write the first one =
like this:<br><br><blockquote type=3D"cite">start a=3Dnode(1405) match =
a-[:A*1..3]->m<br></blockquote><blockquote type=3D"cite"><font =
class=3D"Apple-style-span" =
color=3D"#000000">where </font>a-[:B*1..3]->m</blockquote><blockqu=
ote type=3D"cite">return distinct <a =
href=3D"http://m.name">m.name</a><br></blockquote><div><br></div>Please =
remember that distinct is an expensive operation (in term of =
non-lazyness and memory consumption) as it has to fetch all data and =
merge the results in memory.<div><br></div><div>You may at least try to =
run it without distinct to see the =
difference.</div><div><br></div><div>Michael<br><div><br>Am 10.08.2012 =
um 18:59 schrieb fli.rpx:<br><br><blockquote type=3D"cite">Hi =
Michael,<br><br>Thanks for your reply.<br><br>There are differences =
between the two queries. The first query, "start a=3Dnode(1405) match =
a-[:A*1..3]->m, a-[:B*1..3]->m return distinct <a =
href=3D"http://m.name">m.name</a>", has AND relationship. Its result set =
will be the intersection of two traversals.<br><br>The second query, =
"start a=3Dnode(1405) match a-[:A|B*1..3] return distinct m; ", is =
OR relationship. <br><br>These two queries have performance =
difference. The first one will take two traversals then compare. The =
second one only has one traversal.<br><br>I am wondering if I can write =
the first query in a way of the second query. Something like =
this?:<br><br>start a=3Dnode(1405) match a-[:A&B*1..3] return =
distinct m;<br><br><br>Thanks,<br><br>Fudong <br><br><br><br><br>On =
Thursday, August 9, 2012 2:16:52 PM UTC-7, Michael Hunger wrote:<br>I =
don't really understand your question? <br><br>Does the second =
query not work for you? <br><br>Michael <br><br>Am 09.08.2012 =
um 21:48 schrieb fli.rpx: <br><br>> I am trying to write a query =
to return nodes with two relationships. <br>> <br>> One =
way to do it: start a=3Dnode(1405) match a-[:A*1..3]->m, =
a-[:B*1..3]->m return =
distinct m.name; <br>> <br>> This will work, but =
takes much a lot more time than a query like this: start a=3Dnode(1405) =
match a-[:A|B*1..3] return distinct m; <br>> <br>> I =
think maybe neo4j does two traversals in the first query. <br>yes =
it does and their results span up a cross product which is filtered down =
by distinct again. <br>> <br>> Is it possible to do a =
query to get nodes with both relationships like the second =
query? <br><br></blockquote><br></div></div></body></html>=
--Apple-Mail-90--908114378--