Commit 06be2c64 authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru Committed by Sergei Petrunia

cleanup: Don't pass THD to get_merge_many_buff_cost_fast

We can pass the cost directly.

Reviewer: Monty
parent ca2851d1
...@@ -1594,9 +1594,10 @@ static bool check_if_pq_applicable(Sort_param *param, ...@@ -1594,9 +1594,10 @@ static bool check_if_pq_applicable(Sort_param *param,
scanning the table and comparing the rows with the where clause! scanning the table and comparing the rows with the where clause!
*/ */
const double sort_merge_cost= const double sort_merge_cost=
get_merge_many_buffs_cost_fast(table->in_use, num_rows, get_merge_many_buffs_cost_fast(num_rows,
num_available_keys, num_available_keys,
(uint)row_length); (uint)row_length,
ROWID_COMPARE_COST_THD(table->in_use));
/* /*
PQ has cost: PQ has cost:
(insert + qsort) * log(queue size) * ROWID_COMPARE_COST (insert + qsort) * log(queue size) * ROWID_COMPARE_COST
......
...@@ -27,12 +27,15 @@ PSI_memory_key key_memory_Filesort_buffer_sort_keys; ...@@ -27,12 +27,15 @@ PSI_memory_key key_memory_Filesort_buffer_sort_keys;
A local helper function. See comments for get_merge_buffers_cost(). A local helper function. See comments for get_merge_buffers_cost().
*/ */
static double get_merge_cost(double rowid_compare_cost, ha_rows num_elements, static
ha_rows num_buffers, uint elem_size) double get_merge_cost(ha_rows num_elements,
ha_rows num_buffers,
uint elem_size,
double key_compare_cost)
{ {
return (2.0 * ((double) num_elements * elem_size) / IO_SIZE return (2.0 * ((double) num_elements * elem_size) / IO_SIZE
+ (double) num_elements * log((double) num_buffers) * + (double) num_elements * log((double) num_buffers) *
rowid_compare_cost / M_LN2); key_compare_cost / M_LN2);
} }
/** /**
...@@ -43,19 +46,19 @@ static double get_merge_cost(double rowid_compare_cost, ha_rows num_elements, ...@@ -43,19 +46,19 @@ static double get_merge_cost(double rowid_compare_cost, ha_rows num_elements,
TODO: Use this function for Unique::get_use_cost(). TODO: Use this function for Unique::get_use_cost().
*/ */
double get_merge_many_buffs_cost_fast(THD *thd, double get_merge_many_buffs_cost_fast(ha_rows num_rows,
ha_rows num_rows,
ha_rows num_keys_per_buffer, ha_rows num_keys_per_buffer,
uint elem_size) uint elem_size,
double key_compare_cost)
{ {
ha_rows num_buffers= num_rows / num_keys_per_buffer; ha_rows num_buffers= num_rows / num_keys_per_buffer;
ha_rows last_n_elems= num_rows % num_keys_per_buffer; ha_rows last_n_elems= num_rows % num_keys_per_buffer;
double total_cost, rowid_compare_cost= ROWID_COMPARE_COST_THD(thd); double total_cost;
// Calculate CPU cost of sorting buffers. // Calculate CPU cost of sorting buffers.
total_cost= total_cost=
((num_buffers * num_keys_per_buffer * log(1.0 + num_keys_per_buffer) + ((num_buffers * num_keys_per_buffer * log(1.0 + num_keys_per_buffer) +
last_n_elems * log(1.0 + last_n_elems)) * rowid_compare_cost); last_n_elems * log(1.0 + last_n_elems)) * key_compare_cost);
// Simulate behavior of merge_many_buff(). // Simulate behavior of merge_many_buff().
while (num_buffers >= MERGEBUFF2) while (num_buffers >= MERGEBUFF2)
...@@ -69,16 +72,16 @@ double get_merge_many_buffs_cost_fast(THD *thd, ...@@ -69,16 +72,16 @@ double get_merge_many_buffs_cost_fast(THD *thd,
// Cost of merge sort 'num_merge_calls'. // Cost of merge sort 'num_merge_calls'.
total_cost+= total_cost+=
num_merge_calls * num_merge_calls *
get_merge_cost(rowid_compare_cost, num_keys_per_buffer * MERGEBUFF, get_merge_cost(num_keys_per_buffer * MERGEBUFF, MERGEBUFF, elem_size,
MERGEBUFF, elem_size); key_compare_cost);
// # of records in remaining buffers. // # of records in remaining buffers.
last_n_elems+= num_remaining_buffs * num_keys_per_buffer; last_n_elems+= num_remaining_buffs * num_keys_per_buffer;
// Cost of merge sort of remaining buffers. // Cost of merge sort of remaining buffers.
total_cost+= total_cost+=
get_merge_cost(rowid_compare_cost, last_n_elems, 1 + num_remaining_buffs, get_merge_cost(last_n_elems, 1 + num_remaining_buffs, elem_size,
elem_size); key_compare_cost);
num_buffers= num_merge_calls; num_buffers= num_merge_calls;
num_keys_per_buffer*= MERGEBUFF; num_keys_per_buffer*= MERGEBUFF;
...@@ -86,8 +89,8 @@ double get_merge_many_buffs_cost_fast(THD *thd, ...@@ -86,8 +89,8 @@ double get_merge_many_buffs_cost_fast(THD *thd,
// Simulate final merge_buff call. // Simulate final merge_buff call.
last_n_elems+= num_keys_per_buffer * num_buffers; last_n_elems+= num_keys_per_buffer * num_buffers;
total_cost+= get_merge_cost(rowid_compare_cost, last_n_elems, 1 + num_buffers, total_cost+= get_merge_cost(last_n_elems, 1 + num_buffers, elem_size,
elem_size); key_compare_cost);
return total_cost; return total_cost;
} }
......
...@@ -20,12 +20,14 @@ ...@@ -20,12 +20,14 @@
#include "sql_array.h" #include "sql_array.h"
class Sort_param; class Sort_param;
/*
/**
Calculate cost of merge sort Calculate cost of merge sort
@param num_rows Total number of rows. @param num_rows Total number of rows.
@param num_keys_per_buffer Number of keys per buffer. @param num_keys_per_buffer Number of keys per buffer.
@param elem_size Size of each element. @param elem_size Size of each element.
@param key_compare_cost Cost to compare two keys during QSort & merge
Calculates cost of merge sort by simulating call to merge_many_buff(). Calculates cost of merge sort by simulating call to merge_many_buff().
...@@ -38,10 +40,10 @@ class Sort_param; ...@@ -38,10 +40,10 @@ class Sort_param;
See also comments get_merge_many_buffs_cost(). See also comments get_merge_many_buffs_cost().
*/ */
double get_merge_many_buffs_cost_fast(ha_rows num_rows,
double get_merge_many_buffs_cost_fast(THD *thd, ha_rows num_rows,
ha_rows num_keys_per_buffer, ha_rows num_keys_per_buffer,
uint elem_size); uint elem_size,
double key_compare_cost);
/** /**
......
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