Skip to content

Commit 4aa34e1

Browse files
authored
Merge pull request #69 from stackql/feature/ja-updates
Added performance enhancement strategy
2 parents a58efbe + e592268 commit 4aa34e1

File tree

16 files changed

+378
-127
lines changed

16 files changed

+378
-127
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.8.8 (2025-10-15)
4+
5+
- Added performance enhancement query strategy
6+
37
## 1.8.7 (2025-10-14)
48

59
- Added tab completion

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,25 @@ Deployment orchestration using `stackql-deploy` includes:
130130
- **_deployment_** scripts, which are StackQL queries to create or update resoruces (or delete in the case of de-provisioning)
131131
- **_post-deployment_** tests, which are StackQL queries to confirm that resources were deployed and have the desired state
132132
133-
This process is described here:
133+
**Performance Optimization**: `stackql-deploy` uses an intelligent query optimization strategy which is described here:
134134
135135
```mermaid
136136
graph TB
137137
A[Start] --> B{foreach\nresource}
138-
B --> C[exists\ncheck]
139-
C --> D{resource\nexists?}
140-
D -- Yes --> E[run update\nor createorupdate query]
141-
D -- No --> F[run create\nor createorupdate query]
142-
E --> G[run statecheck check]
143-
F --> G
144-
G --> H{End}
138+
B --> C{exports query\navailable?}
139+
C -- Yes --> D[try exports first\n🔄 optimal path]
140+
C -- No --> E[exists\ncheck]
141+
D --> F{exports\nsuccess?}
142+
F -- Yes --> G[✅ validated with\n1 query only]
143+
F -- No --> E
144+
E --> H{resource\nexists?}
145+
H -- Yes --> I[run update\nor createorupdate query]
146+
H -- No --> J[run create\nor createorupdate query]
147+
I --> K[run statecheck check]
148+
J --> K
149+
G --> L[reuse exports result]
150+
K --> M{End}
151+
L --> M
145152
```
146153
147154
### `INSERT`, `UPDATE`, `DELETE` queries
@@ -187,8 +194,34 @@ WHERE subscriptionId = '{{ subscription_id }}'
187194
AND resourceGroupName = '{{ resource_group_name }}'
188195
AND location = '{{ location }}'
189196
AND JSON_EXTRACT(properties, '$.provisioningState') = 'Succeeded'
197+
198+
/*+ exports */
199+
SELECT resourceGroupName, location, JSON_EXTRACT(properties, '$.provisioningState') as state
200+
FROM azure.resources.resource_groups
201+
WHERE subscriptionId = '{{ subscription_id }}'
202+
AND resourceGroupName = '{{ resource_group_name }}'
190203
```
191204
205+
### Query Optimization
206+
207+
`stackql-deploy` implements intelligent query optimization that significantly improves performance:
208+
209+
**Traditional Flow (3 queries):**
210+
1. `exists` - check if resource exists
211+
2. `statecheck` - validate resource configuration
212+
3. `exports` - extract variables for dependent resources
213+
214+
**Optimized Flow (1 query in happy path):**
215+
1. **Try `exports` first** - if this succeeds, it validates existence, state, and extracts variables in one operation
216+
2. **Fallback to traditional flow** only if exports fails
217+
218+
**Performance Benefits:**
219+
- Up to **66% reduction** in API calls for existing, correctly configured resources
220+
- **2-3x faster** deployments in typical scenarios
221+
- Maintains full validation integrity and backward compatibility
222+
223+
**Best Practice:** Design your `exports` queries to include the validation logic from `statecheck` queries to maximize the benefits of this optimization.
224+
192225
## Usage
193226
194227
<!-- > see [__readthedocs__]() for more detailed documentation -->

examples/databricks/serverless/resources/aws/s3/s3_bucket.iql

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ WHERE
3636
region = '{{ region }}'
3737
AND data__Identifier = '{{ bucket_name }}'
3838

39-
/*+ statecheck, retries=3, retry_delay=5 */
40-
SELECT COUNT(*) as count FROM (
39+
/*+ exports, retries=3, retry_delay=5 */
40+
SELECT
41+
arn,
42+
bucket_name
43+
FROM (
4144
SELECT
45+
arn,
46+
bucket_name,
4247
JSON_EQUAL(ownership_controls, '{{ ownership_controls }}') as test_ownership_controls,
4348
JSON_EQUAL(bucket_encryption, '{{ bucket_encryption }}') as test_encryption,
4449
JSON_EQUAL(public_access_block_configuration, '{{ public_access_block_configuration }}') as test_public_access_block_configuration,
@@ -51,11 +56,3 @@ WHERE test_ownership_controls = 1
5156
AND test_encryption = 1
5257
AND test_public_access_block_configuration = 1
5358
AND test_versioning_configuration = 1
54-
55-
/*+ exports, retries=3, retry_delay=5 */
56-
SELECT
57-
arn,
58-
bucket_name
59-
FROM aws.s3.buckets
60-
WHERE region = '{{ region }}'
61-
AND data__Identifier = '{{ bucket_name }}'

examples/databricks/serverless/resources/databricks_account/credentials.iql

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,15 @@ SELECT
1515
'{{ credentials_name }}',
1616
'{{ aws_credentials }}'
1717

18-
/*+ statecheck, retries=3, retry_delay=5 */
19-
SELECT COUNT(*) as count FROM
20-
(
21-
SELECT
22-
credentials_id
23-
FROM databricks_account.provisioning.credentials
24-
WHERE account_id = '{{ databricks_account_id }}'
25-
AND credentials_name = '{{ credentials_name }}'
26-
AND JSON_EXTRACT(aws_credentials, '$.sts_role.role_arn') = '{{ aws_iam_cross_account_role_arn }}'
27-
) t
28-
29-
/*+ exports */
18+
/*+ exports, retries=3, retry_delay=5 */
3019
SELECT
3120
'{{ credentials_name }}' as databricks_credentials_name,
3221
credentials_id as databricks_credentials_id,
3322
JSON_EXTRACT(aws_credentials, '$.sts_role.external_id') as databricks_role_external_id
3423
FROM databricks_account.provisioning.credentials
3524
WHERE account_id = '{{ databricks_account_id }}'
3625
AND credentials_name = '{{ credentials_name }}'
26+
AND JSON_EXTRACT(aws_credentials, '$.sts_role.role_arn') = '{{ aws_iam_cross_account_role_arn }}'
3727

3828
/*+ delete */
3929
DELETE FROM databricks_account.provisioning.credentials

examples/databricks/serverless/resources/databricks_account/network.iql

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@ SELECT
1919
'{{ subnet_ids }}',
2020
'{{ security_group_ids }}'
2121

22-
/*+ statecheck, retries=3, retry_delay=5 */
23-
SELECT COUNT(*) as count FROM
24-
(
22+
/*+ exports, retries=3, retry_delay=5 */
23+
SELECT
24+
network_id as databricks_network_id
25+
FROM (
2526
SELECT
27+
network_id,
2628
JSON_EQUAL(subnet_ids, '{{ subnet_ids }}') as subnet_test,
2729
JSON_EQUAL(security_group_ids, '{{ security_group_ids }}') as sg_test
2830
FROM databricks_account.provisioning.networks
2931
WHERE account_id = '{{ databricks_account_id }}'
3032
AND network_name = '{{ databricks_network_name }}'
3133
AND vpc_id = '{{ vpc_id }}'
32-
AND subnet_test = 1
33-
AND sg_test = 1
3434
)t
35-
36-
/*+ exports */
37-
SELECT
38-
network_id as databricks_network_id
39-
FROM databricks_account.provisioning.networks
40-
WHERE account_id = '{{ databricks_account_id }}' AND
41-
network_name = '{{ databricks_network_name }}'
35+
WHERE subnet_test = 1
36+
AND sg_test = 1
4237

4338
/*+ delete */
4439
DELETE FROM databricks_account.provisioning.networks

examples/databricks/serverless/resources/databricks_account/storage_configuration.iql

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@ SELECT
1515
'{{ storage_configuration_name }}',
1616
'{{ root_bucket_info }}'
1717

18-
/*+ statecheck, retries=3, retry_delay=5 */
19-
SELECT COUNT(*) as count
20-
FROM databricks_account.provisioning.storage
21-
WHERE account_id = '{{ databricks_account_id }}'
22-
AND storage_configuration_name = '{{ storage_configuration_name }}'
23-
AND JSON_EXTRACT(root_bucket_info, '$.bucket_name') = '{{ aws_s3_workspace_bucket_name }}'
24-
25-
/*+ exports */
18+
/*+ exports, retries=3, retry_delay=5 */
2619
SELECT
2720
storage_configuration_id as databricks_storage_configuration_id
2821
FROM databricks_account.provisioning.storage
2922
WHERE account_id = '{{ databricks_account_id }}'
3023
AND storage_configuration_name = '{{ storage_configuration_name }}'
24+
AND JSON_EXTRACT(root_bucket_info, '$.bucket_name') = '{{ aws_s3_workspace_bucket_name }}'
3125

3226
/*+ delete */
3327
DELETE FROM databricks_account.provisioning.storage

examples/databricks/serverless/resources/databricks_account/workspace.iql

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,18 @@ SELECT
2121
'{{ storage_configuration_id }}',
2222
'{{ pricing_tier }}'
2323

24-
/*+ statecheck, retries=3, retry_delay=5 */
25-
SELECT COUNT(*) as count
26-
FROM databricks_account.provisioning.workspaces
27-
WHERE account_id = '{{ databricks_account_id }}'
28-
AND workspace_name = '{{ workspace_name }}'
29-
AND aws_region = '{{ aws_region }}'
30-
AND credentials_id = '{{ credentials_id }}'
31-
AND storage_configuration_id = '{{ storage_configuration_id }}'
32-
AND pricing_tier = '{{ pricing_tier }}'
33-
34-
/*+ exports */
24+
/*+ exports, retries=3, retry_delay=5 */
3525
SELECT
3626
'{{ workspace_name }}' AS databricks_workspace_name,
3727
workspace_id AS databricks_workspace_id,
3828
deployment_name AS databricks_deployment_name
3929
FROM databricks_account.provisioning.workspaces
4030
WHERE account_id = '{{ databricks_account_id }}'
4131
AND workspace_name = '{{ workspace_name }}'
32+
AND aws_region = '{{ aws_region }}'
33+
AND credentials_id = '{{ credentials_id }}'
34+
AND storage_configuration_id = '{{ storage_configuration_id }}'
35+
AND pricing_tier = '{{ pricing_tier }}'
4236

4337
/*+ delete */
4438
DELETE FROM databricks_account.provisioning.workspaces

examples/databricks/serverless/resources/databricks_account/workspace_group.iql

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ SELECT
1313
'{{ databricks_account_id }}',
1414
'{{ display_name }}'
1515

16-
/*+ statecheck, retries=3, retry_delay=5 */
17-
SELECT COUNT(*) as count
18-
FROM databricks_account.iam.groups
19-
WHERE account_id = '{{ databricks_account_id }}'
20-
AND displayName = '{{ display_name }}'
21-
22-
/*+ exports */
16+
/*+ exports, retries=3, retry_delay=5 */
2317
SELECT id AS databricks_group_id,
2418
displayName AS databricks_group_name
2519
FROM databricks_account.iam.groups

examples/databricks/serverless/resources/databricks_workspace/external_location.iql

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ SELECT
2424
{{ skip_validation }}
2525
;
2626

27-
/*+ statecheck, retries=3, retry_delay=5 */
28-
SELECT COUNT(*) as count
27+
/*+ exports, retries=3, retry_delay=5 */
28+
SELECT name as external_location_name
2929
FROM databricks_workspace.unitycatalog.external_locations
3030
WHERE name = '{{ name | replace('-', '_') }}' AND
3131
deployment_name = '{{ databricks_deployment_name }}'
3232
AND url = '{{ url }}' AND
3333
credential_name = '{{ credential_name | replace('-', '_') }}' AND
3434
read_only = {{ read_only }} AND
3535
comment = '{{ comment }}';
36-
37-
/*+ exports */
38-
SELECT name as external_location_name
39-
FROM databricks_workspace.unitycatalog.external_locations
40-
WHERE name = '{{ name | replace('-', '_') }}' AND
41-
deployment_name = '{{ databricks_deployment_name }}'
4236

4337
/*+ delete */
4438
DELETE FROM databricks_workspace.unitycatalog.external_locations

examples/databricks/serverless/resources/databricks_workspace/storage_credential.iql

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,14 @@ SELECT
2222
'{{ skip_validation }}'
2323
;
2424

25-
/*+ statecheck, retries=3, retry_delay=5 */
26-
SELECT COUNT(*) as count
27-
FROM databricks_workspace.unitycatalog.storage_credentials
28-
WHERE name = '{{ name | replace('-', '_') | upper }}' AND
29-
deployment_name = '{{ databricks_deployment_name }}' AND
30-
JSON_EXTRACT(aws_iam_role, '$.role_arn') = '{{ metastore_access_role_arn }}';
31-
32-
/*+ exports */
25+
/*+ exports, retries=3, retry_delay=5 */
3326
SELECT
3427
name as storage_credential_name,
3528
JSON_EXTRACT(aws_iam_role, '$.external_id') as storage_credential_external_id
3629
FROM databricks_workspace.unitycatalog.storage_credentials
3730
WHERE name = '{{ name | replace('-', '_') | upper }}' AND
38-
deployment_name = '{{ databricks_deployment_name }}';
31+
deployment_name = '{{ databricks_deployment_name }}' AND
32+
JSON_EXTRACT(aws_iam_role, '$.role_arn') = '{{ metastore_access_role_arn }}';
3933

4034
/*+ delete */
4135
DELETE FROM databricks_workspace.unitycatalog.storage_credentials

0 commit comments

Comments
 (0)