MySQL storing undirected graph edges efficiently -


I want to store underside graph edges (for example, for friends) node one , one can use:

Create two rows per line, query on one column per node:

  + ---------------------- + + | ID | From_node | To_node | + ---------------------- + + | 1 | One | B | 2 | B One | + -------------------------- + SELECT * FROM `x` from which to_node = a   

Create one line per line, use or :

  + --------------------- --- - + | ID | Node_A | Nod_b | + ---------------------- + + | 1 | One | B + -------------------------- + SELECT * to `y`, where node_a = one OR node_b = a   

Which makes for a more efficient lookup?

  • Index with 2n rows, from_node and to_node , n with rows, index on node_a, > and node_b
  • / Code>, OR

    If you customize everything If you do, X will be faster, assuming you read data from the disk and are inquiring about a single person's friends. This is because you can arrange your data on the disk so that it can be ordered to match an index that you are asking. Therefore, for a person, you only have to search for a disc. Y is required on two indices questions, so even for a person, (and usually dominates ordinary questions at the time of disk access) for friends There are several attempts to recover.

    See Wikipedia (and)

    If you are fortunate to know that the data will always remain in memory then both of them will be "fast enough" (and even if the data is on disk, they Can be fast - I'm not saying that X is the best design, only that it can be made the most efficient).

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -