geoDB indexes¶
You may create indexes on properties of your collections in geoDB. These indexes potentially speed up queries drastically, but adding too many indexes on a collection might also hamper its performance. Please use with care, and use only if you know what you are doing. This notebook will demonstrate how to work with indexes.
Note that this notebook does not show any performance tests; this is due to the fact that in order for indexes to work efficiently, the collection sizes must be significantly higher than of our demonstration collections.
from xcube_geodb.core.geodb import GeoDBClient
### uncomment if not in managed environment
#client_id=YourID
#client_secret=YourSecret
#geodb = GeoDBClient(client_id=client_id, client_secret=client_secret, auth_mode="client-credentials")
Login in managed environment¶
The environment is prepared with your user credentials, so you simply can start the client.
geodb = GeoDBClient()
Show current user, and create a collection, as shown in the notebook Manage collections¶
print(geodb.whoami)
collection_name = "land_use"
collections = {
collection_name: {"crs": 3794, "properties": {"RABA_PID": "float",
"RABA_ID": "float",
"D_OD": "date"
}
}
}
geodb.create_collections(collections, clear=True)
import geopandas
gdf = geopandas.read_file('data/sample/land_use.shp')
m = geodb.insert_into_collection(collection_name, gdf)
str(m)
Create an index on the geometry of the new collection¶
m = geodb.create_index(collection_name, 'geometry')
str(m)
Show the indexes on the collection.¶
Note that there are two indexes: geoDB automatically creates an index on the collection's id.
print(geodb.show_indexes(collection_name))
Running a sample query which does a geometric lookup¶
For collections of the size of the demonstration collection, the query will not use the index, but if you run a similar query on a larger collection, you will experience huge performance gains.
geodb.get_collection_by_bbox(collection_name, bbox=(453952, 91124, 456136, 93054), bbox_crs=3794)
Remove the index again.¶
This is useful e.g. if you have created too many indexes, and thus ingestion speed is reduced.
geodb.remove_index(collection_name, 'geometry')
Finally, clean up¶
geodb.drop_collection(collection_name)