Large code in ThingsDB is running slow, how to increase performance?

Question:

For testing purposes, I use a script which is rather long. It basically creates a workspace for every test with as name the test together with mock data (this is the large part).

This is the idea of the query in python:

await client.query(f"""//ti
     // create workspace
     workspace = Workspace{{
         name: "{name}",
     }};
     .workspaces.add(workspace);

     // create some mock like users, rules and other stuff...
 """)

While my tests are working, the overhead for the script above is rather large. It is possible to improve the script so my tests run faster?

Asked By: Harry Doler

||

Answers:

I noticed you are using an f-string to format your query. While this might be fine from a security point (as the code is only used for testing), ThingsDB will not be able to cache the code as the "name" changes for each test.

Instead, you should make use of variable injection:

await client.query(f"""//ti
    // create workspace
    workspace = Workspace{{
        name:,
    }};
    .workspaces.add(workspace);

    // create some mock like users, rules and other stuff...
""", name=name)

This way your code doesn’t change and ThingsDB will cache your query which most likely gives you a great performance boost.

Answered By: joente
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.