For the complete documentation index, see llms.txt. This page is also available as Markdown.
Cortex XDR

XQL Query best practices

Write effective and performant XQL queries in Cortex XDR.

Learn how to write Cortex Query Language (XQL) queries that return accurate results quickly and consume fewer compute units in Cortex XDR.

XQL queries run on a columnar, distributed query engine. A query's speed and cost depend mainly on how much data it reads, how much data moves between stages, how much computation each stage performs, and how much data it returns. Queries that do less work run faster, cost less, and are less likely to hit query limits or timeouts.

This guide describes how query performance works in Cortex XDR, and provides best practices, examples, and monitoring techniques that you can apply to investigation queries, widgets, correlation rules, BIOCs, scheduled queries, and XQL public API calls.


Why query performance matters

Query optimization affects both cost and speed.

  • Cost: Queries that run through the XQL public API (including queries that run from integrations and playbooks) and queries against cold storage consume compute units (CU). As a general guide, querying or thawing approximately 35 GB of data consumes 1 CU. Queries that you run against hot storage from the Cortex XDR console don't consume CU, but optimizing them is still good practice.

  • Speed: Query duration is critical for widgets, dashboards, BIOCs, and correlation rules, which run repeatedly and often concurrently.

  • Capacity: Every running query counts toward the concurrent query limit of the user who ran it. Long-running queries keep slots occupied and can block your next query.

Tip

Make optimization part of your query-writing process rather than a separate step. A few habits, such as setting a small timeframe and adding an early fields stage, deliver most of the benefit.

What affects query performance

The following factors determine how long a query runs and how many resources it consumes:

Factor
Description
What you control

Input data

The number of bytes the query reads from storage.

Timeframe, selected fields, filters on partitioned and clustered fields.

Data movement

The data transferred between stages, for example during join, comp, dedup, and sort.

Pre-aggregation, join order, avoiding unnecessary dedup.

Computation

The work required to evaluate functions and expressions.

Function placement, regular expressions, repeated transformations.

Output

The amount of data the query writes and returns.

limit, fields, comp.

Capacity and concurrency

The available resources and the number of queries running at the same time.

Widget refresh cadence, scheduled query timing, cancelling unused queries.

Query patterns

Whether the query follows XQL best practices.

For example, using window functions instead of self-joins.

For example, a join stage that produces many more rows than it receives indicates an opportunity to filter earlier in the query.

XQL queries begin with a data source (a dataset, preset, or Cortex Data Model) followed by zero or more stages, each delimited by a pipe character (|). Each stage operates on the output of the previous stage, so placing data-reducing stages first reduces the work of every stage that follows.

Use the following order as a general guideline:

Order
Stage
Purpose

1

config

Set query behavior, such as timeframe. config must be the first stage.

2

dataset / preset / datamodel

Define the data source.

3

filter

Ask first. Filter on raw field values that don't require functions to evaluate.

4

fields

Reduce fields. Keep only the fields that subsequent stages or the results need.

5

alter

Structure data. Extract, transform, and create fields.

6

comp / windowcomp

Compute. Calculate aggregates or window statistics.

7

filter

Ask again. Filter on values created by alter or comp.

8

sort / limit

Order and bound the results.

9

fields / view

Present results. Set the final field names and order, or display results graphically.

Example

Reduce the data that a query reads

The amount of data a query reads is the most significant factor in its cost and duration. Use the following practices to minimize it.

Use the smallest practical timeframe

Data is partitioned by time. A smaller timeframe means fewer partitions are read.

  • Set the timeframe explicitly with config timeframe, or select Relative time and choose a short period, such as Last 30 Minutes, where possible.

  • Avoid running exploratory queries over a default 24-hour or longer window when a shorter window answers the question.

  • Expand the timeframe only after you validate the query logic on a small window.

Add a fields stage directly after the data source

This is the single most effective optimization. Storage is columnar, so fields that a query doesn't reference aren't read. The xdr_data dataset contains more than 1,000 fields and, in large environments, can reach petabyte scale.

  • Add a fields stage immediately after the data source or the first filter, and include only the fields that subsequent stages use.

  • Avoid fields *, which reads every column in the dataset.

  • During exploration, use fields - to exclude large fields you know you don't need.

Example

The following query returns Windows logon events (event ID 4624) and reads only five fields:

Exclude known fields during exploration:

Note

If you omit the fields stage, dataset queries return all system fields, and Cortex Data Model queries return the xdm_core fields.

Filter on partitioned and clustered fields

In addition to time partitioning, the xdr_data dataset is clustered on event_type and agent_id. Filters on these fields allow the query engine to skip large blocks of data.

  • Always filter xdr_data on event_type when you know the event category.

  • When you investigate specific endpoints, filter on agent_id.

Choose the right data source

Prefer xdr_data with event_type filters over presets

Presets are predefined collections of xdr_data fields for specific activities, such as network_story and authentication_story. Presets are convenient, but they apply additional transformations, including deduplication, which increases processing time.

Approach
Compute unit cost
Speed

Unoptimized query on xdr_data

Highest

Slow

Query on a preset

Typically about 50% lower than unoptimized xdr_data

Usually no speed improvement

Optimized query on xdr_data (early fields and event_type filter)

Lowest

Fastest

For speed-sensitive queries, such as widgets, query xdr_data directly with an event_type filter and an early fields stage. Optimizing a query on xdr_data is almost always less expensive than joining presets.

Example: Replace a preset with an optimized xdr_data query

Instead of preset = authentication_story, use:

Use the same pattern for network_story by filtering on dfe_labels = "network". For the xdr_ presets, filter on the matching enumeration value, for example event_type = ENUM.EVENT_LOG. The enumeration values align with the xdr_ preset names.

Start from a dataset, not the search stage

The search stage performs free-text searches across one or more datasets. It's intended for manual investigation and isn't supported in correlation rules or widgets. When you know which dataset and fields contain the data, start from that dataset and use filter.

Query the Cortex Data Model by dataset

When you use datamodel, specify the datasets mapped to the Cortex Data Model that you want to query. Cortex Data Model queries search only the specified mapped datasets.

Optimize filters

Filter on inexpensive data types

Comparisons on BOOL, INT, FLOAT, and DATE values are faster than comparisons on STRING or BYTE values. When a numeric or Boolean field represents the same condition as a string field, filter on the numeric or Boolean field.

Filter before you transform

Filter on raw field values first, then apply functions to the reduced data set. A filter that wraps a field in a function must evaluate that function for every row.

Match whole values instead of substrings

Exact matches (=, in) are less expensive than substring matches (contains) and regular expressions (~=). Use contains and regular expressions only when you need partial matching.

Use in and not in for lists of values

Use in and not in instead of chaining multiple or or and conditions. Wildcards are supported in in lists for string values.

For IP ranges, use incidr, which accepts multiple CIDR ranges evaluated with a logical OR, instead of multiple separate conditions.

Exclude null and empty values explicitly

Empty strings are represented as "", and null fields are represented as null. Excluding them reduces the rows processed by subsequent stages.

  • Exclude null and empty values: <field> != null and <field> != ""

  • Include only null or empty values: <field> = null or <field> = ""

Set case sensitivity deliberately

Use config case_sensitive to control whether string comparisons are case sensitive. Note that JSON field names are always case sensitive.

Optimize aggregations, joins, and deduplication

Pre-aggregate before joining

A join combines the results of two queries and moves data between processing nodes. Reduce both sides of the join before combining them.

  • Filter and aggregate each side with comp before the join.

  • Place the larger data set first (on the left side of the join).

  • Remove the join key and intermediate fields after the join.

  • Prefer built-in XQL functions over a join when a function can achieve the same result.

Example

Use window functions instead of self-joins

Joining a dataset to itself to compare each row with a group statistic duplicates data movement. Use windowcomp to calculate statistics over a window of rows without a join.

The following example calculates each user's share of the total CPU time consumed by investigation queries, and a running (Pareto) total, without joining the dataset to itself:

Avoid cross-joins

A join without a selective join condition multiplies rows. If a cross-join is unavoidable, pre-aggregate both sides first.

Replace dedup with comp count_distinct where possible

The dedup stage removes duplicate occurrences of field values. It's resource intensive because it can require an internal self-join on large data sets. There are very few scenarios in which you should use dedup early in a query.

When your goal is to count unique values, use comp count_distinct instead.

Less efficient

More efficient

Use approximate aggregates for large data sets

When an estimate is sufficient, use approximate aggregate functions, such as approx_count, approx_quantiles, and approx_top, which use fewer resources than exact calculations on very large data sets.

Use functions efficiently

Place expensive functions last

Regular expressions, JSON parsing, and complex math are computationally expensive. Apply them after filter, fields, and comp have reduced the data.

Avoid repeating the same transformation

Calculate a value once in an alter stage and reference the new field in subsequent stages, instead of repeating the same function call in multiple places.

Use array functions instead of repeated conditions

Use arraycreate, arrayfilter, and array_length to work with lists of values instead of repeating or and and conditions. This makes queries shorter, easier to maintain, and more efficient.

The following example returns events that contain at least one tag:

Use array_any and array_all to test whether any or all elements of an array meet a condition, for example array_any(<array>, "@element" = true).

Extract JSON efficiently

  • JSON field names are case sensitive. If a field name is "TIMESTAMP" and your query uses "timestamp", no results are returned.

  • Filter on the raw field before you parse it, so that JSON functions run only on matching rows.

  • Use the syntactic sugar format (->) for concise access to nested fields. The $ prefix isn't required in this format.

  • If a field name in the JSON path contains a dot (.) or colon (:), use the "['<json_field>']" format, for example json_extract(field_json_1, "$['a.b'].inn").

Less efficient

More efficient

Use regextract and regexcapture for the right context

regextract extracts values that match a regular expression in queries. regexcapture is specific to Parsing Rules and supports multiple named groups.

Manage result size and query complexity

Add a limit stage

Add a limit stage to restrict the number of returned results, especially during development and after a sort stage. A smaller limit reduces the data the query writes and returns.

The default result limit depends on how the query runs:

Query type
Default maximum results

Basic dataset and Cortex Data Model queries with no stages except fields, and no limit stage

1,000

Widgets, correlation rules, XQL public API, saved queries, and scheduled queries

Up to 1,000,000

Legacy templates

10,000

Split queries over very long timeframes

For very long time windows, for example longer than 30 days, splitting a single query into multiple queries over smaller time segments and combining the results can significantly outperform one long-running query. Use the target stage to append each segment's results to a working dataset, and then query that dataset.

Split complex queries

Break down overly complex queries into smaller queries. Use the target stage to save intermediate results to a dataset or lookup, and query the saved results in subsequent steps.

Note

Saving intermediate results with target improves readability and can avoid recalculating the same results multiple times, but it doesn't guarantee a performance improvement on its own.

Materialize large results

If a query's results are very large (more than approximately 10 GB compressed), write them to a dataset with the target stage to prevent errors and to make subsequent access faster.

Optimize queries for widgets and dashboards

Widgets run their queries every time a dashboard loads or refreshes, and each widget query counts toward the concurrent query limit of the user who views the dashboard. Fast widget queries make dashboards load faster and reduce the chance of blocked queries.

  • Aggregate in the query. Use comp to return a small, aggregated result set and display it with the view stage, instead of returning raw rows to the chart.

  • Bound every widget query. Add a limit stage, or select Apply default limit in the Chart Editor.

  • Use a short query window. When you change the widget's time period, the config timeframe updates automatically, although it isn't visible in the query unless you add it manually.

  • Query xdr_data directly. For speed, use an early fields stage and an event_type filter rather than a preset.

  • Group small values. Select Group additional values as Others to limit the number of displayed values.

  • Don't use the search stage. It isn't supported in widgets.

Note

XQL queries generated in the Widget Library don't appear in the Query Center. Their results are used only to build the widget.

Example: Aggregated column chart

Example: Time series chart

To display results over time, group events into time buckets with the bin stage, and then display them with view:

Write readable, maintainable queries

Readable queries are easier to review, optimize, and reuse.

  • Write each stage on a separate line. This makes it easier to identify and modify individual stages.

  • Add comments. Use // for single-line comments and /* */ for multi-line comments. Comment the purpose of the query and the reason for non-obvious stages.

  • Keep previous versions. When you optimize a query, keep the original in a comment block below the optimized version.

  • Rename fields for clarity. Use as in the fields and comp stages.

  • Save reusable queries. Save queries to the Query Library, and use the call stage to reuse saved queries in other queries.

Build queries systematically

When you build a reusable query, building block, or detection, use the following approach:

  1. Define the goal. State the specific analytical or detection objective.

  2. Identify the data source. Choose the most relevant dataset or Cortex Data Model dataset. Avoid presets when their overhead isn't necessary.

  3. Filter first. Apply the most restrictive filters and set the smallest relevant timeframe with config timeframe.

  4. Select fields early. Immediately after the initial filters, keep only the fields that later stages and the results need.

  5. Transform. Use functions in alter stages to prepare the data. Prefer native functions over join.

  6. Aggregate. Use comp or windowcomp for statistics and grouping.

  7. Order and limit. Use sort when order matters, and limit to control the result size.

  8. Refine and test. Review the compute units and duration in the Query Center, and refine filters, fields, and functions.

  9. Document. Add comments and save the query to the Query Library.

Monitor query performance and cost

Use the Query Center

The Query Center shows the Simulated Compute Units and Duration (SEC) for each query. High compute unit usage or long run times indicate that a query should be simplified and optimized. You can also view your In Progress queries and cancel queries that you no longer need.

Query the Query Center history with XQL

The xql_query_center_history dataset contains the execution history of queries, including compute units, duration, query text, source, and the user who ran the query.

Example: Find your most expensive queries

Example: Calculate thresholds for high compute unit usage and duration

The following query calculates the 99th percentile of compute units and duration in your environment. Use the results as thresholds to identify queries to optimize, and adjust them for your environment.

Example: Identify queries with optimization opportunities

You can split the query text into stages to flag common issues. The following example identifies queries that use dedup in the first third of the query, where it's rarely necessary, and queries that select fields early:

Track playbook and script performance

Use the following datasets to analyze playbook and script performance with XQL:

Dataset
Contents

playbook_tasks

Data about failed tasks and tasks that were retried.

playbook_runs

Data about playbook runs and statuses.

scripts_and_commands_metrics

Data about scripts and commands used in playbook tasks.

You can also use the Troubleshooting Playbooks dashboard to view playbook and task errors and average run times, and pivot to XQL for detailed analysis.

Example: Find playbooks with highly variable run times

Query limits, timeouts, and compute units

Concurrent queries per user

Administrators can set a maximum number of concurrent queries per user under Settings → Configurations → General → Query Management. When a user approaches the limit, a system message warns about the high query load. When a user exceeds the limit, new queries are blocked until the number of active queries drops below the limit. Other users can continue to run queries.

The following queries count toward the concurrent query limit:

  • XQL investigation queries, including cold and hot storage queries, Cortex Data Model and XDR templates, free text search, and queries from the Query Library.

  • Scheduled queries and scheduled reports. These run on behalf of the user who created them.

  • XQL widget queries in dashboards and reports.

  • XQL public API queries.

  • BIOC and correlation rule test queries.

  • XQL queries that run from playbook tasks.

Queries run by correlation rules aren't restricted by the query limit, and very short queries don't count toward concurrent queries.

Tip

If you're blocked, open the Query Center, review your In Progress queries, and cancel the queries you no longer need. Stagger scheduled queries and avoid opening many heavy dashboards at the same time.

Query timeout

All queries are subject to a default timeout of 60 minutes, which takes priority over the administrator-defined value. To allow a specific query to run longer, add the config max_runtime_minutes stage to the query. The value can't exceed the timeout defined by the administrator. The timeout is approximate.

If a query times out, reduce the timeframe, add an early fields stage, filter on event_type, or split the query into smaller time segments.

Compute units

Cortex XSIAM provides a free daily quota of compute units according to your license size. Compute units are consumed by XQL public API queries and cold storage queries. Queries that run without sufficient quota fail.

You can view consumption and set a daily limit under Settings → Configurations → Data Management → Compute Unit Consumption. The daily usage is calculated at 00:00 UTC. You can raise the daily limit temporarily during an incident investigation.

Example: Optimize a query step by step

The following example applies the practices in this guide to a query that finds PowerShell executions on specific endpoints.

Step 1: Original query

The query reads all fields, scans the default timeframe, and uses a substring match on an unclustered field.

Step 2: Set a timeframe and filter on a clustered field

Filtering on event_type and setting a short timeframe reduces the data scanned.

Step 3: Narrow to specific endpoints

Filtering on agent_id, which is also a clustered field, with an in list further reduces the data scanned.

Step 4: Select fields, aggregate, and limit

An early fields stage reduces the columns read, comp returns a small result set, and limit bounds the output.

Compare the Simulated Compute Units and Duration (SEC) of each version in the Query Center to measure the improvement.

Common XQL syntax considerations

XQL isn't SQL, Splunk SPL, or Microsoft KQL, although it shares some concepts. Use only supported XQL stages and functions. If you're familiar with other query languages, note the following:

Task
How to do it in XQL

Arithmetic

Use math functions instead of operators. For example, 1 + 3 * 3 is written as add(1, multiply(3, 3)).

Conditional aggregation (for example, count if)

Use alter with if() to set a conditional value, and then aggregate the new field with comp.

Fill null values

Use coalesce() or if() in an alter stage, or the replacenull stage.

Nested JSON fields

Use JSON functions or the -> syntactic sugar in an alter stage.

Time series charts

Use the bin stage to group events into time buckets, and the view stage to display the chart.

Expand arrays into rows

Use the arrayexpand stage.

Percentiles

Use approx_quantiles in a comp stage.

Reusable query logic

Save queries to the Query Library and reuse them with the call stage.

Example: Conditional aggregation

The following example counts completed and waiting playbook runs in a single comp stage by aggregating if() expressions:

Optimization checklist

Before you save or schedule a query, verify the following:

Last updated

Was this helpful?