Introduction
The Streamlined API abstracts away the complexity of the Foundation API, making it quicker to write scripts to interact with Granta MI without having to know the full details of the underlying API. However, all abstractions require a trade-off, and in this case the simpler interface results reduced control over the individual requests that are sent to the Granta MI server. In some circumstances, this can cause slower performance than using the lower level API. To address this, use bulk operation methods to optimize performance with the Streamlined API. These methods all improve performance through one or both of the following techniques:- Reducing the amount of data requested from the server
- Batching requests, such that the same data is requested in fewer requests
Object caches
Accessing a database and table
The first steps in any MI Scripting Toolkit script are to connect to Granta MI and fetch a database and table. The three objects, and the fastest ways of acquiring them, are as follows:Session:SessionBuilderis the fastest way to create a session.Database:Session.get_db()is the fastest way to fetch a database.Table:Database.get_table()is the fastest way to fetch a table.
Session.get_db() and Database.get_table() fetch all databases and all tables (for the
selected database) loaded on the server, cache the results, and then return the requested database or table. Because the
results are cached, those calls to Granta MI are not repeated when the objects are accessed again. As a result, it
is not necessary to pass references to database and tables between functions, as long as the session itself is shared.
This results in cleaner code which is still performant.
The code below shows which calls result in a request being sent to Granta MI:
Filtering records based on the record tree
There are many search methods in the Streamlined API that return records that match a user-defined attribute-based criterion or text-based criterion. However, there are some record properties that are not searchable, for example queries based on details of the tree structure of a table. In this case, useTable.all_records() to fetch all
records in the table with the children of each record pre-cached. Then apply your own search filters to the record list
using standard Python operations:
Record properties:
These properties are pre-populated on all Record objects returned by the Table.all_records() method.
Other properties will require additional calls to Granta MI unless the appropriate ‘bulk fetch’ methods are used before
the properties are accessed. See Table bulk operations and Session bulk operations for more information about
these methods.
Creating records
To create records:- Use
Table.path_from()orTable.paths_from()to create the required tree structure if required. - Use
Table.create_record()to create record objects, and optionally add attribute values and links. - Import the records using
Session.update().
Step 1: Creating a tree structure
Although a tree structure can be created by adding each folder manually, this isn’t advisable because you have to ensure:- that parent folders exist before any child folders are created meaning tree structure levels must be created as separate requests.
- that folder names are unique before creation or the operation will fail.
Table.path_from(), overcomes these issues. All levels
of a hierarchy can be created in a single operation, and no exceptions or warnings are generated if a folder already
exists with the same name. The record color can also be specified, which is applied to all records or folders created by
the method. The method returns the last node specified in the path, allowing it to be provided as the parent
argument to the Table.create_record() method.
Firstly, an example of creating a new folder structure. Create three new folders with the names “our”, “new”, and
“path”:
Step 2: Creating and populating records
TheTable.create_record() method creates a new record object in Python only, and does not push that record to the
server. These records can therefore be modified, for example by adding attribute values:
Step 3: Importing records and data
TheSession.update() method is used to import changes to attribute values, and by default accepts multiple
records to update which are passed to Granta MI in an appropriately batched request:
Record objects reflect the latest state of the Granta MI server.
This behavior can be suppressed by calling Session.update() with refresh_attributes=False. Then either export
only the required attributes with Table.bulk_fetch() (see Bulk fetching attributes section for more
information):
Table.bulk_fetch() step entirely if the Record objects are no longer required:
Table bulk operations
The following bulk operations are implemented on theTable object and are used to group requests to to improve
script performance:
Table.bulk_fetch()Table.bulk_link_fetch()Table.bulk_fetch_associated_records()Table.bulk_fetch_data_revision_history()Table.bulk_fetch_all_record_versions()
bulk_fetch and bulk_link_fetch methods are described in more detail in the following subsections.
Bulk fetching attributes
AccessingRecord.attributes causes the Streamlined API to check to see if the attributes property has been
populated yet. If not, it automatically populates it by exporting all the attributes for that record. This is
inefficient if only specific attributes are needed.
Instead, the Table.bulk_fetch() method should be used to ensure only the required attributes are fetched. Also,
attributes can be requested for multiple records, which fetches the data in a smaller number of batched requests.
NOTE
TheTable.bulk_fetch() method does not return a value. Instead, the appropriate properties in the provided
records are updated in-place.
This example shows how to fetch a single attribute value for all records in a table. In this case, the table contains
1,000 records.
Record.attributes property directly. This example contains the
fewest lines of code, but is the least efficient approach because all attributes are fetched for each record separately.
Table.bulk_fetch() method to fetch all attributes for all records in a smaller
number of requests before accessing the Record.attributes dictionary. By default, Table.bulk_fetch()
groups records into batches of 100, and requests all attributes for those records. Performance is improved, but unused
attributes are still being fetched:
Bulk fetching record links
Record link groups are accessed similarly to attributes. AccessingRecord.links causes the Streamlined API
to check if the links property has been populated yet. If not, it automatically exports all record link groups for
the record. This is inefficient if only specific record link groups are needed.
Instead, the Table.bulk_link_fetch() method should be used to ensure only the required record link groups are
fetched. Also, record link groups can be requested for multiple records, which fetches the data in a smaller number of
batched requests.
NOTE
TheTable.bulk_link_fetch() method does not return a value. Instead, the appropriate properties in the
provided records are updated in-place.
This example shows how to fetch the links in a single record link group for all records in a table. In this case, the
table contains 1,000 records.
Record.links property directly. This example contains
the fewest lines of code, but is the least efficient approach because all record link groups are fetched for each record
separately:
Table.bulk_link_fetch() method to fetch all record links for all records in a
smaller number of requests before accessing the Record.links dictionary. By default,
Table.bulk_link_fetch() groups records into batches of 100, and requests all record link groups for those
records. Performance is improved, but unused record link groups are still being fetched:
Session bulk operations
The following bulk operations are implemented on theSession object and are used to group requests to to
improve script performance:
Fetching release states
AccessingRecord.release_state causes the Streamlined API to check if the release_state property has been
populated yet. If not, it automatically exports the release state and release history for the record. This is
inefficient if the property is accessed for multiple records in a short space of time, for example within a loop or
comprehension.
Instead, the Session.bulk_fetch_release_states() method should be used to request the revision information for
multiple records in a smaller number of batched requests. Additionally, since the bulk_fetch_release_states method
is implemented on the Session object, release states can be fetched for records in multiple tables and
databases simultaneously.
This method can also be parallelized and batched by using the appropriate arguments, see the API guide for more details.
This example shows how to fetch the release states for all records in a table. In this case, the table contains 1,000
records.
Record.release_state
property. By default, Session.bulk_fetch_release_states() groups records into batches of 100:
Deleting records
CallingRecord.delete_or_withdraw_record_on_server() causes the Streamlined API to immediately delete or
withdraw the record on the server. This is inefficient if multiple records are deleted or withdrawn in a short space of
time, for example within a loop or comprehension.
Instead, the Session.bulk_delete_or_withdraw_records() method should be used to delete or withdraw multiple
records in smaller number of batched requests. Additionally, since the bulk_delete_or_withdraw_records method
is implemented on the Session object, records in multiple tables and databases can be deleted or withdrawn
simultaneously.
This method can also be parallelized and batched by using the appropriate arguments, see the API guide for more details.
NOTE
If the record is version controlled, the record is deleted or withdrawn depending on its revision status. A record which is at least v1 released is be withdrawn. An unreleased record is deleted. This example shows how to delete all records in a table. In this case, the table contains 1,000 records.Batch sizes
As described in the table and session bulk operations section on this page, MI Scripting Toolkit performance can be improved significantly through the use of bulk operations. The performance increase is dependent on the chosen batch size and degree of parallelism for bulk operations, captured in thebatch_size and max_num_threads properties for bulk
methods respectively. There is an optimal value for both of these settings which depends on the operation type, the
schema structure 1, and the complexity of the operation.
If batch_size is too small, then the overhead of sending and receiving requests and responses becomes significant,
which reduces performance. However, if batch_size is too large, the request may time out. Equally, if
max_num_threads is too small, then the server is under-utilized. However, if max_num_threads is too large, the
server may become saturated, slowing down performance for all users.
To cope with large batch_size and max_num_threads values, the operation timeout can be increased through use of
the SessionConfiguration timeout parameter, but this can hamper development by increasing the time taken
to receive invalid or null responses also. Additionally, timeout errors have to be handled
carefully to avoid interacting with the database in an inconsistent state.
The default batch_size value for each bulk operation is chosen as a starting point, with parallelization disabled by
default. These settings will result in improved performance when compared to making each request individually, but there
is still often significant scope for further performance improvement. A common approach is to measure the time taken to
complete a request as a function of batch_size and max_num_threads to find an optimal value.
However, you should always leave some margin for error between the expected request duration and the allowed timeout to
account for variable server load and changes in both the schema structure and the amount of data stored in the database.
- [1] Certain Granta MI attribute types take longer to export than others. For example, tabular data can take longer to export than other data types due to the need to resolve associated records and linked attribute values. In general, testing should be performed to understand how your particular database schema impacts the performance of associated scripts and tools.