# Isolate interactive and batch workloads (/docs/guides/operate/isolate-workloads)



Isolate interactive and batch workloads [#isolate-interactive-and-batch-workloads]

Use a workload tree when interactive queries and batch processing should share a
server without sharing the same scheduling policy.

Before you begin [#before-you-begin]

* Set `RAWTREE_API_KEY` to an administrator API key.
* Use `analytics`. If it does not exist, create it with the [`POST /v1/databases` endpoint](/docs/reference/api/databases#post-v1databases).
* Pass `?database=analytics` on each query. If you omit it, the Query API uses the API key's stored default database.
* Apply workload statements to the `default` cluster.

1\. Create the CPU resource [#1-create-the-cpu-resource]

Create a CPU resource.

```sql test
CREATE RESOURCE cpu ON CLUSTER 'default' (MASTER THREAD, WORKER THREAD)
```

2\. Create the root workload [#2-create-the-root-workload]

Create the root workload.

```sql test
CREATE WORKLOAD all ON CLUSTER 'default' SETTINGS max_concurrent_threads = 4
```

3\. Add a default child workload [#3-add-a-default-child-workload]

Create the default child workload.

```sql test
CREATE WORKLOAD default ON CLUSTER 'default' IN all
```

4\. Add an interactive workload [#4-add-an-interactive-workload]

Give interactive queries more weight and two threads.

```sql test
CREATE WORKLOAD interactive ON CLUSTER 'default' IN all SETTINGS weight = 3, priority = 1, max_concurrent_threads = 2 FOR cpu
```

5\. Add a batch workload [#5-add-a-batch-workload]

Give batch queries less weight and one thread.

```sql test
CREATE WORKLOAD batch ON CLUSTER 'default' IN all SETTINGS weight = 1, priority = 5, max_concurrent_threads = 1 FOR cpu
```

6\. Select a workload [#6-select-a-workload]

Select the interactive workload for one query.

```sql test
SELECT count() FROM system.one SETTINGS workload = 'interactive'
```

7\. Inspect workload definitions [#7-inspect-workload-definitions]

List the workload definitions.

```sql test
SELECT name FROM system.workloads
```

8\. Inspect scheduler resources [#8-inspect-scheduler-resources]

List the scheduler resources.

```sql test
SELECT name FROM system.resources
```

9\. Inspect scheduler nodes [#9-inspect-scheduler-nodes]

Inspect the request counts for the `interactive` workload.

```sql test
SELECT resource, path, dequeued_requests, inflight_requests
FROM system.scheduler
WHERE path LIKE '%interactive%'
```

The `dequeued_requests` count increases for the `interactive` path after a query uses that workload. `inflight_requests` shows requests that run now. `is_active` is `1` only when requests wait in the queue.

Check the result [#check-the-result]

Run a query with
[`SETTINGS workload = 'interactive'`](/docs/reference/sql/settings/session#workload).
After it finishes, inspect `dequeued_requests` in `system.scheduler`. While it runs,
inspect `inflight_requests`.

Cleanup [#cleanup]

Drop `interactive` before you drop its parent workload.

```sql
DROP WORKLOAD interactive ON CLUSTER 'default'
```

Drop `batch` before you drop its parent workload.

```sql
DROP WORKLOAD batch ON CLUSTER 'default'
```

Drop `default` before you drop the root workload.

```sql
DROP WORKLOAD default ON CLUSTER 'default'
```

Drop the root workload.

```sql
DROP WORKLOAD all ON CLUSTER 'default'
```

Drop the CPU resource after you drop its workloads.

```sql
DROP RESOURCE cpu ON CLUSTER 'default'
```

Next steps [#next-steps]

* [Workload scheduling concepts](/docs/concepts/workload-scheduling)
* [Lock a role to a workload](/docs/guides/operate/settings-profiles)
* [Workload statements](/docs/reference/sql/statements/workload)
