This topic describes how to use aggregation instructions and provides examples.
stats
Use the stats instruction for the statistical analysis of logs. It is similar to aggregate functions in SQL, such as COUNT, SUM, and AVG. It performs statistical, grouping, and aggregate operations on specific fields in log data.
This instruction is used only for query analysis in Log Service. It does not apply to scenarios such as data transformation, Structured Process Language (SPL) rule consumption, write processors, or Logtail configurations.
By default, the stats instruction returns the first 100 aggregation results. To return more results, use the limit instruction.
Syntax
stats <output>=<aggOperator> by <group>,[<group>...]Parameters
Parameter | Type | Required | Description |
output | String | Yes | Specifies an alias for the statistical result field. |
aggOperator | SQLExp | Yes | The following aggregate functions are supported:
|
group | String | No | Specifies the dimension for aggregation. This is similar to the GROUP BY field in SQL. |
Examples
Example 1: Calculate the
pvof access logs byip.SPL statement
* | stats pv=count(*) by ipInput data
ip: 192.168.1.1 latencyMs: 10 ip: 192.168.1.1 latencyMs: 20 ip: 192.168.1.2 latencyMs: 10Output data
ip: 192.168.1.2 pv: 1 ip: 192.168.1.1 pv: 2
Example 2: Calculate the
min/maxlatency for allipaddresses in theaccesslog.SPL statement
* | extend latencyMs=cast(latencyMs as bigint) | stats minLatencyMs=min(latencyMs), maxLatencyMs=max(latencyMs) by ipInput data
ip: 192.168.1.1 latencyMs: 10 ip: 192.168.1.1 latencyMs: 20 ip: 192.168.1.2 latencyMs: 10Output data
ip: 192.168.1.2 minLatencyMs: 10 maxLatencyMs: 20 ip: 192.168.1.1 minLatencyMs: 10 maxLatencyMs: 10
Example 3: Calculate the total
pvin an access log.SPL statement
* | stats pv=count(*)Input data
ip: 192.168.1.1 latencyMs: 10 ip: 192.168.1.1 latencyMs: 20 ip: 192.168.1.2 latencyMs: 10Output data
pv: 3
sort
The sort instruction sorts query results. You can sort field values or statistical results in ascending (asc) or descending (desc) order. This is an important tool for log analysis that helps you quickly locate key data and generate ordered reports.
This instruction is used only for query analysis in Log Service. It does not apply to scenarios such as data transformation, SPL rule consumption, write processors, or Logtail configurations.
Syntax
sort <field> [asc/desc] ,(<field> [asc/desc])Parameters
Parameter | Type | Required | Description |
field | String | Yes | Specifies the field to sort by. The following field types are supported:
|
asc/desc | String | No |
|
Example
Sort accesslog by latencyMs.
SPL statement
* | extend latencyMs=cast(latencyMs as bigint) | sort latencyMs descInput data
ip: 192.168.1.1 latencyMs: 10 ip: 192.168.1.1 latencyMs: 20 ip: 192.168.1.2 latencyMs: 15Output data
ip: 192.168.1.1 latencyMs: 20 ip: 192.168.1.2 latencyMs: 15 ip: 192.168.1.1 latencyMs: 10
limit
The limit instruction limits the number of log rows returned in the query results. It is a core instruction for controlling data volume. Using limit helps prevent performance issues or resource waste caused by excessively large query results. It is suitable for various scenarios, such as log analysis and real-time monitoring.
This instruction is used only for query analysis in Log Service. It does not apply to scenarios such as data transformation, SPL rule consumption, write processors, or Logtail configurations.
If you do not use the sort instruction to specify a collation, the output of the limit instruction is in a random order. This is because the natural order of logs is not guaranteed during storage.
Syntax
limit (<offset>,) <size>Parameters
Parameter | Type | Required | Description |
offset | Integer | No | Skips the first |
size | Integer | Yes | The row limit. |
Example
Sort an access log by the latencyMs field and then retrieve the first row.
SPL statement
* | extend latencyMs=cast(latencyMs as bigint) | sort latencyMs | limit 1Input data
ip: 192.168.1.1 latencyMs: 10 ip: 192.168.1.1 latencyMs: 20 ip: 192.168.1.2 latencyMs: 15Output data
ip: 192.168.1.1 latencyMs: 20