What are the keys and indexes
FindEntity and FindEntities methods can be used to search any row(s) with any condition(s). But they are slow, because they iterate the rows one by one. To address this problem keys and indexes were introduced.
- A key consists of 1-n fields and can be used to look up row(s) quickly. It also can be used in rows validation
- An index has a reference to a single field and can be used for effective range scan
Different row(s) retrieval methods efficiency comparison
Method | Underlying structure | Time complexity | Comments |
---|---|---|---|
meta.GetEntity(int index)
Retrieve a single row using provided physical row's index
|
list | O(1) | fastest method |
meta.GetEntity(BGId Id)
Retrieve a single row using provided row's ID
|
hashtable | O(1) | |
GetEntity(string name)
Retrieve a single row using provided row's name
|
hashtable | O(1) | |
key.GetEntityByKey(params object[] keys) key.GetEntitiesByKey(params object[] keys) Retrieve a first row/all rows with provided keys values
|
hashtable | O(1) | |
index.FindEntitiesByIndex(indexOperator)
Retrieve all rows with index field value within provided range
|
b-tree | O(log n) | if index field data is not skewed |
meta.FindEntity(predicate) meta.FindEntities(predicate) Retrieve a first row/all rows using provided filter (any conditions supported)
|
N/A | O(n), full-scan |
Keys
Following fields can be added to a key: bool, int, long, decimal, bool?, int?, long?, string, text, enum, relationSingle.
You can access the matching row(s) directly without searching, using BGKey class. Available options:
- Use generated methods (recommended)
- Use basic API. To access the key, use
BGMetaEntity.GetKey({keyName})
method. To get a row(s), useBGKey.GetEntityByKey
orBGKey.GetEntitiesByKey
methods - If you use Visual Scripting assets, you can use generated unit/node if it's supported
Keys make row(s) access faster, but uses extra memory and may slow down write (update) operations, so do not use keys for data, which is updated frequently.
Indexes
Indexes enables row(s) range scan fast operations, but uses extra memory and may slow down write (update) operations. Currently only single field index is supported. Available options to use an index:
- Use generated methods (recommended)
- Use basic API. To access the index use
BGMetaEntity.GetIndex({indexName})
method. To query rows useindex.FindEntitiesByIndex
method
Indexes also provide methods for finding max/min values. Obtain a reference to an index (via generated property _{IndexName}) and call GetMax/GetMin methods