diff --git a/python/querying/nested_index_example/README.md b/python/querying/nested_index_example/README.md new file mode 100644 index 0000000..c886234 --- /dev/null +++ b/python/querying/nested_index_example/README.md @@ -0,0 +1,55 @@ +# Nested Index Query Performance Example with Memgraph + +This example demonstrates how to use GQLAlchemy to showcase the performance impact of nested indexes in Memgraph. + +## ๐Ÿง  What This Example Does + +The script performs the following actions: + +1. **Connects to a running Memgraph instance** using `gqlalchemy`. +2. **Creates 2000 `Person` nodes** with a nested `address` property. +3. **Executes a query** to find people by a nested property (country) and times the query execution. +4. **Creates a nested index** on `:Person(address.country)`. +5. **Executes the same query again** and times the execution to show the performance improvement. + +## ๐Ÿš€ How to Run Memgraph with Docker + +To run Memgraph Community using Docker: + +```bash +docker run -it --rm -p 7687:7687 memgraph/memgraph:3.3 +``` + +## ๐Ÿ›  Requirements + +Install dependencies with: + +```bash +pip install -r requirements.txt +``` + +Your `requirements.txt` should include: + +``` +GQLAlchemy==1.7.0 +``` + +## ๐Ÿงช How to Run the Script + +Once Memgraph is running: + +```bash +python3 nested_index_example.py +``` + +## ๐Ÿ”– Version Compatibility + +This example was built and tested with: + +- **Memgraph v3.3** + +If you run into any issues or have questions, feel free to reach out on the [Memgraph Discord server](https://discord.gg/memgraph). We're happy to help! + +## ๐Ÿข Enterprise or Community? + +This example works with **Memgraph Community Edition** \ No newline at end of file diff --git a/python/querying/nested_index_example/nested_index_example.py b/python/querying/nested_index_example/nested_index_example.py new file mode 100644 index 0000000..5cbda03 --- /dev/null +++ b/python/querying/nested_index_example/nested_index_example.py @@ -0,0 +1,53 @@ +from gqlalchemy import Memgraph +import random +import time + +# Establish a connection to Memgraph +memgraph = Memgraph(host='127.0.0.1', port=7687) + +# Clean up any existing data +memgraph.execute("MATCH (n) DETACH DELETE n;") +memgraph.execute("DROP INDEX ON :Person(address.country);") + +# Sample data for addresses +countries = ["USA", "Canada", "UK", "Germany", "France", "Italy", "Spain", "Australia", "Japan", "Brazil"] +cities = ["New York", "Toronto", "London", "Berlin", "Paris", "Rome", "Madrid", "Sydney", "Tokyo", "Rio de Janeiro"] +streets = ["Main St", "Oak Ave", "Pine Rd", "Maple Dr", "Elm St", "Cedar Ave", "Birch Rd", "Spruce Dr", "Willow St", "Ash Ave"] + +# Create 2000 Person nodes with nested address +for i in range(2000): + name = f"Person_{i}" + address = { + 'street': f"{random.randint(1,999)} {random.choice(streets)}", + 'city': random.choice(cities), + 'country': random.choice(countries) + } + query = f""" + CREATE (:Person {{name: '{name}', address: {{street: '{address['street']}', city: '{address['city']}', country: '{address['country']}'}}}}) + """ + memgraph.execute(query) + +# Query to time (e.g., find all people in the UK) +country_to_query = "UK" +query = f""" +MATCH (p:Person) +WHERE p.address.country = '{country_to_query}' +RETURN p +""" + +start = time.time() +results = list(memgraph.execute_and_fetch(query)) +time_no_index = time.time() - start +print(f"Query time without index: {time_no_index:.4f} seconds. Results: {len(results)}") + +# Create nested index +memgraph.execute("CREATE INDEX ON :Person(address.country);") + +# Wait a moment for index to be created +import time as t +t.sleep(2) + +start = time.time() +results = list(memgraph.execute_and_fetch(query)) +time_with_index = time.time() - start +print(f"Query time with index: {time_with_index:.4f} seconds. Results: {len(results)}") \ No newline at end of file diff --git a/python/querying/nested_index_example/requirements.txt b/python/querying/nested_index_example/requirements.txt new file mode 100644 index 0000000..a4d7e05 --- /dev/null +++ b/python/querying/nested_index_example/requirements.txt @@ -0,0 +1 @@ +GQLAlchemy==1.7.0 \ No newline at end of file