Commit 50e9f7ae authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru Committed by Sergei Petrunia

Rewrite cost computation for filesort operations

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
parent 06be2c64
......@@ -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
......@@ -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
......
......@@ -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;
......@@ -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;
......
......@@ -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
#
......
This diff is collapsed.
This diff is collapsed.
......@@ -16,9 +16,13 @@
#ifndef FILESORT_UTILS_INCLUDED
#define FILESORT_UTILS_INCLUDED
#include "my_global.h"
#include "my_base.h"
#include "sql_array.h"
#include <utility>
class TABLE;
class Sort_param;
/**
......@@ -42,17 +46,81 @@ class Sort_param;
*/
double get_merge_many_buffs_cost_fast(ha_rows num_rows,
ha_rows num_keys_per_buffer,
uint elem_size,
double key_compare_cost);
size_t elem_size,
double compare_cost,
bool with_addon_fields);
/**
These are the current sorting algorithms we compute cost for:
PQ_SORT_ALL_FIELDS Sort via priority queue, with addon fields.
PQ_SORT_ORDER_BY_FIELDS Sort via priority queue, without addon fields.
MERGE_SORT_ALL_FIELDS Sort via merge sort, with addon fields.
MERGE_SORT_ORDER_BY_FIELDS Sort via merge sort, without addon fields.
Note:
There is the possibility to do merge-sorting with dynamic length fields.
This is more expensive than if there are only fixed length fields,
however we do not (yet) account for that extra cost. We can extend the
cost computation in the future to cover that case as well.
Effectively there are 4 possible combinations for merge sort:
With/without addon fields
With/without dynamic length fields.
*/
enum sort_type
{
PQ_SORT_ALL_FIELDS= 0,
PQ_SORT_ORDER_BY_FIELDS,
MERGE_SORT_ALL_FIELDS,
MERGE_SORT_ORDER_BY_FIELDS,
FINAL_SORT_TYPE,
NO_SORT_POSSIBLE_OUT_OF_MEM,
};
struct Sort_costs
{
Sort_costs() :
fastest_sort(NO_SORT_POSSIBLE_OUT_OF_MEM), lowest_cost(DBL_MAX) {}
void compute_sort_costs(Sort_param *param, ha_rows num_rows,
size_t memory_available,
bool with_addon_fields);
/* Cache value for fastest_sort. */
enum sort_type fastest_sort;
/* Cache value for lowest cost. */
double lowest_cost;
private:
/*
Array to hold all computed costs.
TODO(cvicentiu) This array is only useful for debugging. If it's not
used in debugging code, it can be removed to reduce memory usage.
*/
double costs[FINAL_SORT_TYPE];
void compute_pq_sort_costs(Sort_param *param, ha_rows num_rows,
size_t memory_available,
bool with_addon_fields);
void compute_merge_sort_costs(Sort_param *param, ha_rows num_rows,
size_t memory_available,
bool with_addon_fields);
void compute_fastest_sort();
};
/**
A wrapper class around the buffer used by filesort().
The sort buffer is a contiguous chunk of memory,
containing both records to be sorted, and pointers to said records:
<start of buffer | still unused | end of buffer>
|rec 0|record 1 |rec 2| ............ |ptr to rec2|ptr to rec1|ptr to rec0|
<start of buffer | still unused | end of buffer>
| rec0 | rec1 | rec2 | ............ |ptr to rec2|ptr to rec1|ptr to rec0|
Records will be inserted "left-to-right". Records are not necessarily
fixed-size, they can be packed and stored without any "gaps".
......@@ -284,5 +352,4 @@ class Filesort_buffer
int compare_packed_sort_keys(void *sort_keys, unsigned char **a,
unsigned char **b);
qsort2_cmp get_packed_keys_compare_ptr();
#endif // FILESORT_UTILS_INCLUDED
......@@ -54,6 +54,20 @@ static inline double cache_hit_ratio(uint ratio)
/* Cost of finding a key from a row_ID (not used for clustered keys) */
#define ROW_LOOKUP_COST ((double) 1.0)
/*
These constants impact the cost of QSORT and priority queue sorting,
scaling the "n * log(n)" operations cost proportionally.
These factors are < 1.0 to scale down the sorting cost to be comparable
to 'read a row' = 1.0, (or 0.55 with default caching).
A factor of 0.1 makes the cost of get_pq_sort_cost(10, 10, false) =0.52
(Reading 10 rows into a priority queue of 10 elements).
One consenquence if this factor is too high is that priority_queue will
not use addon fields (to solve the sort without having to do an extra
re-read of rows) even if the number of LIMIT is low.
*/
#define QSORT_SORT_SLOWNESS_CORRECTION_FACTOR (0.1)
#define PQ_SORT_SLOWNESS_CORRECTION_FACTOR (0.1)
/*
Cost of finding and copying keys from the storage engine index cache to
an internal cache as part of an index scan.
......
......@@ -541,11 +541,22 @@ to be fixed later
class Sort_param {
public:
uint rec_length; // Length of sorted records.
uint sort_length; // Length of sorted columns.
// Length of sorted records. ALWAYS equal to sort_length + addon_length
uint rec_length;
/*
Length of what we need to sort: Sorted columns + ref_length if not
addon fields are used
*/
uint sort_length;
/* Length of the reference to the row (rowid or primary key etc */
uint ref_length; // Length of record ref.
/* Length of all addon fields. 0 if no addon fields */
uint addon_length; // Length of addon_fields
uint res_length; // Length of records in final sorted file/buffer.
/*
The length of the 'result' we are going to return to the caller for
each sort element. Also the length of data in final sorted file/buffer.
*/
uint res_length;
uint max_keys_per_buffer; // Max keys / buffer.
uint min_dupl_count;
ha_rows limit_rows; // Select limit, or HA_POS_ERROR if unlimited.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment