0

I've used indexes in mySQL databases and I know how indexes work. I used indexes to get better performance from the database. I know that there could be downsides if too much data is indexed. What is the situation with Memgrah since it is a graph database? Can the data be indexed and which data should be indexed?

KWriter
  • 1,024
  • 4
  • 22

1 Answers1

1

Memgraph will not automatically index labeled data. If you want to optimize queries that fetch nodes by a label, you need to create indexes.

When you are running queries, you want to get results as soon as possible. In the worst-case scenario, when you execute a query, all nodes need to be checked to see if there is a match.

By enabling indexes, this process can be much faster. When a query is executed, the engine first checks if there is an index. An index stores additional information on certain types of data so that retrieving indexed data becomes more efficient. Indexes basically store data in a different kind of way, i.e., they partition it with a key. For example, if you set an index on a label, the query MATCH (:Label) won't have to explicitly check every node. You just need to check the nodes that were placed on a "shelf". Each "shelf" has nodes with a specific label. The data is not copied or duplicated to the "shelf". You actually create a memory map to those nodes and there is no need to look anywhere else for them.

There are some downsides to indexing, so it is important to carefully choose the right data for creating an index. The downsides of indexing are:

  • requiring extra storage (memory) for each index and
  • slowing down write operations to the database.

Indexing all of the content will not improve the database speed. The structures in the index are dynamically updated on modifications or insertions of new nodes. Once a new node is created, it needs to be assigned to an index group. Such an indexed node will be retrieved much faster from the database.

Also, indexing certain data types will not bring any significant performance gain, e.g., for boolean in the best case scenario, the time will be cut in half.

Indexing can be applied to data with a specific label or a combination of label and property. They are not automatically created, and the user needs to create them explicitly. Creation is done using a special CREATE INDEX ON :Label(property) language construct.

When you create an index, it is added to the registry of indexes.

Memgraph supports two types of indexes:

  • label index - create it with Cypher query, e.g. CREATE INDEX ON :Person;
  • label-property index - create it with Cypher query, e.g. CREATE INDEX ON :Person(age);
KWriter
  • 1,024
  • 4
  • 22