Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions python/querying/nested_index_example/README.md
Original file line number Diff line number Diff line change
@@ -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**
53 changes: 53 additions & 0 deletions python/querying/nested_index_example/nested_index_example.py
Original file line number Diff line number Diff line change
@@ -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)}")
1 change: 1 addition & 0 deletions python/querying/nested_index_example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GQLAlchemy==1.7.0