I am designing a graph, and see examples where several vertices will have a similar label, such as 'user', etc. When knowing its unique value, one can assign it to the vertex' id, and look it up as:
g.V('person').has('id','unique-value'). ...
Or assign that unique value as a label, and reference it that way.
g.V('unique-value'). ...
Is there a particular reason not use unique values (an id, essentially) as a label, such as performance? What is the best strategy for this?
Your question and your Gremlin examples don't quite align. I think that you mean to compare:
g.V().hasLabel('person').has(T.id,'unique-value')
and
g.V('unique-value')
Note my corrections in that first Gremlin statement. V() does not take a vertex label as an argument - it can only take a vertex id or a Vertex object. Also, the actual vertex identifier must be referenced by T.id and not 'id', the latter being a reference to a user-defined property named "id". T.id is what you get returned from g.V().id(). We often refer to T.id as just id and I will do so going forward.
With that being straightened out, there is no need to do hasLabel('person') if you have the id handy, so the two examples above return the same value and I would think that most graph databases would likely optimize away the label filter and just use the id for their lookup so I wouldn't imagine that you'd see a difference in performance, but for readability purposes I'd stick to just using V('unique-value').
Your question specifically asked about using a unique label as a way to identify a vertex, so I will also address that. A label is not meant for unique identification of a graph element. It is meant to categorize groups of elements. Aside from that convention, I think there are a number of technical reasons not to do that. Some graphs have limits on the number of labels you can have so that could be a problem depending on your graph provider. At the very least, you reduce the portability of your code by doing that. I think it would impact performance as label lookups are not going to be as fast as id lookups (especially as you scale the graph up in size).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With