Basic MATCH queries

You will start with a very simple but common query—finding all users followed by a given user. In the relational approach, the following code returns all users followed by the user with the username @MilosSQL:

SELECT t2.UserName
FROM dbo.TwitterUser t1
INNER JOIN dbo.UserFollows uf ON t1.UserId = uf.UserId
INNER JOIN dbo.TwitterUser t2 ON t2.UserId = uf.FollowingUserId
WHERE t1.UserName = '@MilosSQL';

The resulting list contains four usernames:

UserName
----------------------
@DejanSarka
@sql_williamd
@tomaz_tsql
@WienerSportklub

The same result can be achieved by using the new MATCH clause, as in the following code:

SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t1.UserName = '@MilosSQL';

You can see that the dbo.TwitterUser node table is referenced twice, and the edge table once. One instance of the node table represents followers, the other users who follow @MilosSQL. The MATCH filter contains the following statement:

MATCH (t1-(Follows)->t2)

It specifies that an element of the set t1 is in relation (follows) an element in the set t2. The MATCH clause is combined with another non-graph predicate by using the AND operator. And what about the execution plans for both queries? As you can see in the following figure, they are identical:

Execution plans for relational and graph queries

So, there is no difference from a performance point of view, but a query that uses the MATCH clause is more intuitive than one that uses JOINs.

To get the list of users who follow the user @MilosSQL, you can use one of the following two logically equivalent statements:

SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1<-(Follows)-t2) AND t1.UserName = '@MilosSQL';

SELECT t1.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) AND t2.UserName = '@MilosSQL';

In both cases, the result will look like the one shown in the following list:

UserName
------------------
@DejanSarka
@tomaz_tsql
@WienerSportklub

In the first case, you have simply changed the arrow direction and left all the other sections untouched. The second query references the other instance of the node table in the WHERE clause. In both cases, the given user is on the right side of the relationship (the one who is followed), and both of them are intuitive and simpler than the relational counterpart.

To get all followers of MilosSQL's, you can use the following query:

SELECT DISTINCT t3.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows f1, dbo.TwitterUser t3, dbo.Follows f2
WHERE MATCH (t1-(f1)->t2-(f2)->t3) AND t1.UserName = '@MilosSQL';

Since you have specified two levels in graph traversing, you would need three instances of the node and two instances of the edge table. The MATCH statement is a bit more complex, but still single lined and intuitive. Here is the result of the previous query:

UserName
------------------
@DejanSarka
@MilosSQL
@nkolimpija
@sql_williamd
@tomaz_tsql

The same query with relational tables looks more complex and error-prone, as shown in the following code:

SELECT DISTINCT u3.UserName
FROM dbo.TwitterUser u1
INNER JOIN dbo.UserFollows f ON u1.UserId = f.UserId
INNER JOIN dbo.TwitterUser u2 ON f.FollowingUserId = u2.UserId
INNER JOIN dbo.UserFollows f2 ON u2.UserId = f2.UserId
INNER JOIN dbo.TwitterUser u3 ON f2.FollowingUserId = u3.UserId
WHERE u1.UserName = '@MilosSQL';

As in the previous example, the execution plans are still the same; the benefit of graph queries are mainly readability and their intuitive nature.

As mentioned earlier, the MATCH clause cannot be combined with other expressions using OR and NOT in the WHERE clause. Therefore, the following code won't work:

SELECT t2.UserName
FROM dbo.TwitterUser t1, dbo.TwitterUser t2, dbo.Follows
WHERE MATCH (t1-(Follows)->t2) OR t1.UserName = '@MilosSQL';

Instead of the list of followers, you will get an error message:

Msg 13905, Level 16, State 1, Line 3
A MATCH clause may not be directly combined with other expressions using OR or NOT.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.239.226