Estimating the cost of YQL queries to YDB
To run YQL queries, the system uses server-side resources. To view the information about consumed resources, see the relevant query statistics.
To evaluate the YDB API request cost, you need to calculate the CPU and I/O cost, out of which the largest value is the final one.
-
CPU cost:
The CPU time spent to compile the request is added to the CPU time spent at each step of request execution. This value is then divided by the CPU time increment of 1.5 ms, rounded down, and converted to RUs based on the pricing plan from the table below.
Cached requests are not recompiled. For caching, use
bind variablesin your requests and enable caching of the request execution plan. -
I/O cost:
The following values are calculated:
- Number of disk reads: The number of read records and data blocks is compared, and the largest of them is taken. The number of blocks is calculated by dividing the amount of bytes read by the block size of 4 KB and rounding up the value.
- Number of disk writes: The number of written records and data blocks is compared, and the largest of them is taken. The number of blocks is calculated by dividing the amount of bytes written by the block size of 1 KB and rounding up the value.
This includes I/O operations in tables and indexes. Record deletes are charged based on their number.
The number of reads and writes is converted to RUs based on the pricing plan from the table below with the resulting costs summed up.
Warning
Some record conversions involve data reads or deletes before updating, and this may affect the request cost.
Unit costs in RUs:
| Value to estimate | Cost |
|---|---|
| One CPU time increment | 1 RU |
| One read | 1 RU |
| One write | 2 RUs |
The following operations are not charged:
- Creating, updating, and deleting table schemas.
- Getting a description and list of tables.
- Creating and deleting directories.
Example of request cost calculation
Request statistics:
query_phases {
...
table_access {
...
reads {
rows: 2
bytes: 16
}
...
}
cpu_time_us: 475
...
}
query_phases {
...
table_access {
...
updates {
rows: 2
bytes: 2456
}
...
}
cpu_time_us: 514
...
}
compilation {
...
cpu_time_us: 4062
}
process_cpu_time_us: 870
Where:
query_phases[].cpu_time_us: CPU time spent on executing the request, in microseconds.compilation.cpu_time_us: CPU time spent on compiling the request, in microseconds.process_cpu_time_us: CPU time spent on managing interaction, in microseconds.query_phases[].reads.rows: Number of read data records.query_phases[].reads.bytes: Number of read data bytes.query_phases[].updates.rows: Number of written data records.query_phases[].updates.bytes: Number of written data bytes.
To estimate the CPU cost, the request's total CPU time is divided by 1.5 ms, ( 475 + 514 + 4062 + 870 ) / 1500 = 3.95. The result is rounded down and converted to RUs:
3 × 1 RU = 3 RUs
To estimate the I/O cost:
-
The number of disk reads is calculated in records and blocks, and the largest of them is taken:
- Two rows read, the number of operations is 2.
- 16 bytes read,
16 / ( 4 × 1024 ) = 0.004, the result is rounded up, the number of operations is 1.
The number of read operations is 2.
-
The number of disk writes in calculated in records and blocks, and the largest of them is taken:
- Two records written, the number of operations is 2.
- 2,456 bytes written,
2456 / 1024 = 2.398, the result is rounded up, the number of operations is 3.
The number of write operations is 3.
I/O cost:
2 × 1 RU + 3 × 2 RUs = 8 RUs
The request cost is 8 RUs.