Skip to content

Commit

Permalink
Rewrite cost computation for filesort operations
Browse files Browse the repository at this point in the history
This is a rework of how filesort calculates costs to allow functions
like test_if_skip_sort_order() to calculate the cost of filesort to
decide between filesort and using a key to resolve ORDER BY.

Changes:
- Split cost calculation of qsort + optional merge sort and priority queue
  to dedicated functions.
- Fixed some wrong calculations of cost in old code (use of log() instead
  of log2()).
- Added costs realted to fetching the rows if addon fields are not used.
- Updated get_merge_cost() to take into account that we are going to
  read data from temporary files in big chuncks (DISK_CHUNCK_SIZE (64K) and
  not in IO_SIZE (4K).
- More code documentation including various variables in Sort_param.

One effect of the cost update is that the cost of priority queue
with addon field has decreased slightly and is used in more cases.
When the rowid is large (like with InnoDB where rowid is the priority key),
using addon fields is in many cases preferable.

Reviewer: Monty
  • Loading branch information
cvicentiu authored and spetrunia committed Feb 2, 2023
1 parent 06be2c6 commit 50e9f7a
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 233 deletions.
49 changes: 46 additions & 3 deletions mysql-test/main/fetch_first.result
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,57 @@ id first_name last_name score
7 Bob Trasc 9
2 John Doe 6
6 John Elton 8.1
analyze FORMAT=JSON
select * from t1
order by first_name, last_name
order by first_name, last_name, score
offset 2 rows
fetch first 3 rows only;
ANALYZE
{
"query_optimization": {
"r_total_time_ms": "REPLACED"
},
"query_block": {
"select_id": 1,
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"nested_loop": [
{
"read_sorted_file": {
"r_rows": 5,
"filesort": {
"sort_key": "t1.first_name, t1.last_name, t1.score",
"r_loops": 1,
"r_total_time_ms": "REPLACED",
"r_limit": 5,
"r_used_priority_queue": true,
"r_output_rows": 6,
"r_sort_mode": "sort_key,addon_fields",
"table": {
"table_name": "t1",
"access_type": "ALL",
"r_loops": 1,
"rows": 8,
"r_rows": 8,
"r_table_time_ms": "REPLACED",
"r_other_time_ms": "REPLACED",
"filtered": 100,
"r_filtered": 100
}
}
}
}
]
}
}
select * from t1
order by first_name, last_name, score
offset 2 rows
fetch first 3 rows only;
id first_name last_name score
2 John Doe 6
6 John Elton 8.1
5 John Smith 7
4 John Smith 6
select * from t1
order by first_name, last_name
offset 2 rows
Expand All @@ -454,7 +497,7 @@ id first_name last_name score
4 John Smith 6
5 John Smith 7
select * from t1
order by first_name, last_name
order by first_name, last_name, score
offset 3 rows
fetch first 3 rows only;
id first_name last_name score
Expand Down
11 changes: 9 additions & 2 deletions mysql-test/main/fetch_first.test
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,15 @@ order by first_name, last_name
offset 1 rows
fetch first 3 rows with ties;

--source include/analyze-format.inc
analyze FORMAT=JSON
select * from t1
order by first_name, last_name
order by first_name, last_name, score
offset 2 rows
fetch first 3 rows only;

select * from t1
order by first_name, last_name, score
offset 2 rows
fetch first 3 rows only;

Expand All @@ -360,7 +367,7 @@ offset 2 rows
fetch first 3 rows with ties;

select * from t1
order by first_name, last_name
order by first_name, last_name, score
offset 3 rows
fetch first 3 rows only;

Expand Down
20 changes: 10 additions & 10 deletions mysql-test/main/order_by.result
Original file line number Diff line number Diff line change
Expand Up @@ -3666,16 +3666,16 @@ t2.key1 = t1.a and t2.key1 IS NOT NULL
ORDER BY
t2.key2 ASC
LIMIT 1)
900-0-123456
901-1-123456
902-2-123456
903-3-123456
904-4-123456
905-5-123456
906-6-123456
907-7-123456
908-8-123456
909-9-123456
100-0-123456
101-1-123456
102-2-123456
103-3-123456
104-4-123456
105-5-123456
106-6-123456
107-7-123456
108-8-123456
109-9-123456
drop table t1,t2;
# End of 10.3 tests
#
Expand Down
Loading

0 comments on commit 50e9f7a

Please sign in to comment.