Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating Index on nodes property #920

Closed
Munmud opened this issue May 14, 2023 · 10 comments
Closed

Creating Index on nodes property #920

Munmud opened this issue May 14, 2023 · 10 comments
Labels
question Further information is requested

Comments

@Munmud
Copy link
Contributor

Munmud commented May 14, 2023

Is there any way to create index on property of nodes/edges ? If so how to add an index. For example i have created nodes as below

SELECT * 
FROM cypher('Students', $$
    CREATE (:Person {roll : 25, age : 20})
$$) as (v agtype);

Now I want to create index on property roll how to do that ?
How to use the created index for faster query ?

@Munmud Munmud added the question Further information is requested label May 14, 2023
@talhahahae
Copy link

I think by using create index clause on the property 'roll' of node 'person' you can achieve your target.

CREATE INDEX property_index ON "Person" (roll);

Above statement will create index on roll property of person node.

@hammadsaleemm
Copy link

hammadsaleemm commented May 14, 2023

To create the index you can use this commad as mentioned by Talha CREATE INDEX ON :Person(roll), and to speed it up , I think you can use cypher queries like

MATCH (p:Person)
WHERE p.roll = 25
RETURN p

With the index in place, this query should run faster than a similar query without the index.

@waleedahmed0001
Copy link
Contributor

To create an index on the "roll" property of the "Person" node, you can use the following Cypher query:

CREATE INDEX ON :Person(roll)

If you want to retrieve data based on this index, you can use this command
MATCH (p:Person) USING INDEX p:Person(roll) WHERE p.roll = 10 RETURN p

This query uses the index on the "roll" property of the "Person" node to quickly find all nodes where the "roll" property is equal to 10.

@humzakt
Copy link

humzakt commented May 14, 2023

Kindly have a look at this issue as it has alot of examples on creating uniques indexes: #45

@Hamza-Mushtaque
Copy link

Yes, It is possible to create an index on properties of nodes/edges in Apache AGE.

Try using the following Cypher query:

CREATE INDEX ON :Person(roll);

This will create an index on the "roll" property of the "Person" nodes.

And for using the created index for faster query USING INDEX clause in your Cypher query can be utilized.

For example, to find all the "Person" nodes with "roll" property value of 25 using the index, try using following query.

MATCH (p:Person)
WHERE p.roll = 25
USING INDEX p:Person(roll)
RETURN p;

In this query, the USING INDEX p:Person(roll) clause tells Apache AGE to use the index on the "roll" property of the "Person" nodes for faster lookup.

I hope this helps you.

@kenwoon
Copy link
Contributor

kenwoon commented May 15, 2023

I've tried the suggestions but none of them worked. From the following input:

SELECT *
FROM cypher('Demo', $$
    CREATE INDEX ON :Person(roll)
$$) as (v agtype);

I get an error that says ERROR: syntax error at or near "ON".

@WendelLana
Copy link
Contributor

You can use the following command to create GIN indices:

CREATE INDEX index_name
ON graph_name."Label" USING gin (properties);

This GIN indices normally improves WHERE clause performance. Here is an example:

CREATE INDEX load_city_gin_idx
ON cypher_index."City" USING gin (properties);

SELECT * FROM cypher('cypher_index', $$
    EXPLAIN MATCH (c:City {city_id: 1})
    RETURN c
$$) as (n agtype);
                                     QUERY PLAN
------------------------------------------------------------------------------------
 Bitmap Heap Scan on "City" c  (cost=12.00..16.02 rows=1 width=32)
   Recheck Cond: (properties @> agtype_build_map('city_id'::text, '1'::agtype))
   ->  Bitmap Index Scan on load_city_gin_idx  (cost=0.00..12.00 rows=1 width=0)
         Index Cond: (properties @> agtype_build_map('city_id'::text, '1'::agtype))
(4 rows)

@Munmud
Copy link
Contributor Author

Munmud commented May 16, 2023

You can use the following command to create GIN indices:

CREATE INDEX index_name
ON graph_name."Label" USING gin (properties);

This GIN indices normally improves WHERE clause performance. Here is an example:

CREATE INDEX load_city_gin_idx
ON cypher_index."City" USING gin (properties);

SELECT * FROM cypher('cypher_index', $$
    EXPLAIN MATCH (c:City {city_id: 1})
    RETURN c
$$) as (n agtype);
                                     QUERY PLAN
------------------------------------------------------------------------------------
 Bitmap Heap Scan on "City" c  (cost=12.00..16.02 rows=1 width=32)
   Recheck Cond: (properties @> agtype_build_map('city_id'::text, '1'::agtype))
   ->  Bitmap Index Scan on load_city_gin_idx  (cost=0.00..12.00 rows=1 width=0)
         Index Cond: (properties @> agtype_build_map('city_id'::text, '1'::agtype))
(4 rows)

@WendelLana After that how you will delete the index ? DROP INDEX index_name; not working here

@WendelLana
Copy link
Contributor

WendelLana commented May 16, 2023

@Munmud You have to provide the graph name, so use this command:
DROP INDEX graph_name.gin_index_name;

@Munmud
Copy link
Contributor Author

Munmud commented May 16, 2023

Thank you @WendelLana

@Munmud Munmud closed this as completed May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

8 participants