OQGRAPH Examples
Contents
Creating a table with origid, destid only
CREATE TABLE oq_backing ( origid INT UNSIGNED NOT NULL, destid INT UNSIGNED NOT NULL, PRIMARY KEY (origid, destid), KEY (destid) );
Some data can be inserted into the backing table to test with later:
INSERT INTO oq_backing(origid, destid) VALUES (1,2), (2,3), (3,4), (4,5), (2,6), (5,6);
Now the read-only OQGRAPH table is created. The CREATE statement must match the format below - any difference will result in an error.
CREATE TABLE oq_graph ( latch VARCHAR(32) NULL, origid BIGINT UNSIGNED NULL, destid BIGINT UNSIGNED NULL, weight DOUBLE NULL, seq BIGINT UNSIGNED NULL, linkid BIGINT UNSIGNED NULL, KEY (latch, origid, destid) USING HASH, KEY (latch, destid, origid) USING HASH ) ENGINE=OQGRAPH data_table='oq_backing' origid='origid' destid='destid';
Creating a table with weight
For the examples on this page, we'll create a second OQGRAPH table and backing table, this time with weight
as well.
CREATE TABLE oq2_backing ( origid INT UNSIGNED NOT NULL, destid INT UNSIGNED NOT NULL, weight DOUBLE NOT NULL, PRIMARY KEY (origid, destid), KEY (destid) );
INSERT INTO oq2_backing(origid, destid, weight) VALUES (1,2,1), (2,3,1), (3,4,3), (4,5,1), (2,6,10), (5,6,2);
CREATE TABLE oq2_graph ( latch VARCHAR(32) NULL, origid BIGINT UNSIGNED NULL, destid BIGINT UNSIGNED NULL, weight DOUBLE NULL, seq BIGINT UNSIGNED NULL, linkid BIGINT UNSIGNED NULL, KEY (latch, origid, destid) USING HASH, KEY (latch, destid, origid) USING HASH ) ENGINE=OQGRAPH data_table='oq2_backing' origid='origid' destid='destid' weight='weight';
Shortest Path
A latch
value of 'breadth_first'
and an origid
and destid
is used for finding the shortest path between two nodes, for example:
SELECT * FROM oq_graph WHERE latch='breadth_first' AND origid=1 AND destid=6; +--------------+--------+--------+--------+------+--------+ | latch | origid | destid | weight | seq | linkid | +--------------+--------+--------+--------+------+--------+ | breadth_first| 1 | 6 | NULL | 0 | 1 | | breadth_first| 1 | 6 | 1 | 1 | 2 | | breadth_first| 1 | 6 | 1 | 2 | 6 | +--------------+--------+--------+--------+------+--------+
Note that nodes are uni-directional, so there is no path from node 6 to node 1:
SELECT * FROM oq_graph WHERE latch='breadth_first' AND origid=6 AND destid=1; Empty set (0.00 sec)
Using the GROUP_CONCAT function can produce more readable results, for example:
SELECT GROUP_CONCAT(linkid ORDER BY seq) AS path FROM oq_graph WHERE latch='breadth_first' AND origid=1 AND destid=6; +-------+ | path | +-------+ | 1,2,6 | +-------+
Using the table oq2_graph
, the shortest path is different:
SELECT GROUP_CONCAT(linkid ORDER BY seq) AS path FROM oq2_graph WHERE latch='breadth_first' AND origid=1 AND destid=6; +-------------+ | path | +-------------+ | 1,2,3,4,5,6 | +-------------+
The reason is the weight between nodes 2 and 6 is 10
in oq_graph2
, so the shortest path taking into account weight
is now across more nodes.
Possible destinations
SELECT GROUP_CONCAT(linkid) AS dests FROM oq_graph WHERE latch='breadth_first' AND origid=2; +-----------+ | dests | +-----------+ | 5,4,6,3,2 | +-----------+
Note that this returns all possible destinations along the path, not just immediate links.
Note: it is also possible to use a value for latch of '1' however the use of integer latch commands is deprecated and may be phased out in a future release.
The use of integer latches is controlled using the oqgraph_allow_create_integer_latch system variable.