0

For reference: This is how we do it in SQL Server:

create index indexname on tablename (col1, col2)

I checked the documentation https://docs.griddb.net/architecture/database-function/#transaction-isolation-level and didn't find anything specific to my requirement.

1 Answers1

1

How to create composite indexes in GridDB?

GridDB is a distributed key-value store that supports high-performance queries on multi-dimensional data. One of the features of GridDB is the ability to create composite indexes, which are indexes that span multiple columns or attributes of a container.

Why use composite indexes?

Composite indexes can improve the performance of queries that involve multiple conditions on different columns or attributes. For example, if you have a container that stores information about products, such as name, category, price, and rating, and you want to query for products that belong to a certain category and have a high rating, you can use a composite index on the category and rating columns to speed up the query.

Composite indexes can also reduce the storage space and memory usage of the index, since they store only the combinations of values that exist in the container, rather than the values of each column separately.

How to create composite indexes?

To create a composite index in GridDB, you need to use the create_index method of the Container class. The create_index method takes two arguments: a list of column names or numbers that specify the columns or attributes to be indexed, and an optional index type that specifies the type of the index. The index type can be one of the following:

  • griddb.IndexType.DEFAULT: The default index type, which is suitable for most cases. It creates a B-tree index that supports range queries and sorting.
  • griddb.IndexType.HASH: A hash index that supports equality queries and fast lookups, but not range queries or sorting.
  • griddb.IndexType.TREE: A tree index that supports range queries and sorting, but not equality queries or fast lookups. It is similar to the default index type, but it uses a different data structure that may have different performance characteristics.

For example, to create a composite index on the category and rating columns of a product container, you can use the following code:

import griddb_python as griddb

# Connect to the GridDB cluster
factory = griddb.StoreFactory.get_instance()
store = factory.get_store(
    host="127.0.0.1",
    port=10001,
    cluster_name="defaultCluster",
    username="admin",
    password="admin"
)

# Get the product container
product_container = store.get_container("product")

# Create a composite index on the category and rating columns
product_container.create_index(["category", "rating"])

How to query using composite indexes?

To query using composite indexes, you need to use the query method of the Container class. The query method takes a string argument that specifies the query condition in the TQL (TimeSeries Query Language) syntax. TQL is a SQL-like language that supports various operators and functions for querying data in GridDB.

For example, to query for products that belong to the category "electronics" and have a rating of 4 or higher, you can use the following code:

import griddb_python as griddb

# Connect to the GridDB cluster
factory = griddb.StoreFactory.get_instance()
store = factory.get_store(
    host="127.0.0.1",
    port=10001,
    cluster_name="defaultCluster",
    username="admin",
    password="admin"
)

# Get the product container
product_container = store.get_container("product")

# Query using the composite index on the category and rating columns
query = product_container.query("select * where category = 'electronics' and rating >= 4")
result_set = query.fetch()

# Print the query results
for row in result_set:
    print(row)

Summary

  • Composite indexes are indexes that span multiple columns or attributes of a container in GridDB.
  • Composite indexes can improve the performance of queries that involve multiple conditions on different columns or attributes, and reduce the storage space and memory usage of the index.
  • To create a composite index, use the create_index method of the Container class, and specify a list of column names or numbers, and an optional index type.
  • To query using a composite index, use the query method of the Container class, and specify the query condition in the TQL syntax.
Ahmed Mohamed
  • 866
  • 1
  • 4