#At file:///export/home/didrik/repo/next-mr-opt-team-wl1393-merge/ based on revid:tor.didriksen@stripped
3259 Tor Didriksen 2010-12-10
WL#1393 Optimizing filesort with small limit
@ sql/filesort.cc
Adjust, and comment on, cost function.
modified:
mysql-test/r/order_by_sortkey.result
mysql-test/t/order_by_sortkey.test
sql/filesort.cc
=== modified file 'mysql-test/r/order_by_sortkey.result'
--- a/mysql-test/r/order_by_sortkey.result 2010-11-30 11:54:00 +0000
+++ b/mysql-test/r/order_by_sortkey.result 2010-12-10 15:06:03 +0000
@@ -40,8 +40,6 @@ INSERT INTO tmp SELECT f1,f2 FROM t1;
INSERT INTO t1(f1,f2) SELECT * FROM tmp;
INSERT INTO tmp SELECT f1,f2 FROM t1;
INSERT INTO t1(f1,f2) SELECT * FROM tmp;
-INSERT INTO tmp SELECT f1,f2 FROM t1;
-INSERT INTO t1(f1,f2) SELECT * FROM tmp;
set sort_buffer_size= 32768;
FLUSH STATUS;
SHOW SESSION STATUS LIKE 'Sort%';
=== modified file 'mysql-test/t/order_by_sortkey.test'
--- a/mysql-test/t/order_by_sortkey.test 2010-11-30 11:54:00 +0000
+++ b/mysql-test/t/order_by_sortkey.test 2010-12-10 15:06:03 +0000
@@ -50,8 +50,6 @@ INSERT INTO tmp SELECT f1,f2 FROM t1;
INSERT INTO t1(f1,f2) SELECT * FROM tmp;
INSERT INTO tmp SELECT f1,f2 FROM t1;
INSERT INTO t1(f1,f2) SELECT * FROM tmp;
-INSERT INTO tmp SELECT f1,f2 FROM t1;
-INSERT INTO t1(f1,f2) SELECT * FROM tmp;
# Test when only sortkeys fits to memory
set sort_buffer_size= 32768;
=== modified file 'sql/filesort.cc'
--- a/sql/filesort.cc 2010-12-09 11:54:39 +0000
+++ b/sql/filesort.cc 2010-12-10 15:06:03 +0000
@@ -218,7 +218,8 @@ ha_rows filesort(THD *thd, TABLE *table,
!(param.tmp_buffer= (char*) my_malloc(param.sort_length,MYF(MY_WME))))
goto err;
- if (check_if_pq_applicable(¶m, &table_sort,
+ if (param.max_rows != HA_POS_ERROR &&
+ check_if_pq_applicable(¶m, &table_sort,
table, num_rows, memory_available))
{
DBUG_PRINT("info", ("filesort PQ is applicable"));
@@ -1174,8 +1175,7 @@ bool check_if_pq_applicable(Sort_param *
{
make_char_array(filesort_info,
param->max_keys_per_buffer, param->rec_length);
- if (filesort_info->sort_keys)
- DBUG_RETURN(true);
+ DBUG_RETURN(filesort_info->sort_keys != NULL);
}
else
{
@@ -1189,8 +1189,7 @@ bool check_if_pq_applicable(Sort_param *
{
make_char_array(filesort_info,
param->max_keys_per_buffer, param->rec_length);
- if (filesort_info->sort_keys)
- DBUG_RETURN(true);
+ DBUG_RETURN(filesort_info->sort_keys != NULL);
}
// Try to strip off addon fields.
@@ -1211,11 +1210,17 @@ bool check_if_pq_applicable(Sort_param *
PQ has cost:
(insert + qsort) * log(queue size) / TIME_FOR_COMPARE_ROWID +
cost of file lookup afterwards.
+ The lookup cost is a bit pessimistic: we take scan_time and assume
+ that on average we find the row after scanning half of the file.
+ A better estimate would be lookup cost, but note that we are doing
+ random lookups here, rather than sequential scan.
*/
- const double pq_cost=
+ const double pq_cpu_cost=
(PQ_slowness * num_rows + param->max_keys_per_buffer) *
- log((double) param->max_keys_per_buffer) / TIME_FOR_COMPARE_ROWID +
- param->max_rows * table->file->scan_time();
+ log((double) param->max_keys_per_buffer) / TIME_FOR_COMPARE_ROWID;
+ const double pq_io_cost=
+ param->max_rows * table->file->scan_time() / 2.0;
+ const double pq_cost= pq_cpu_cost + pq_io_cost;
if (sort_merge_cost < pq_cost)
DBUG_RETURN(false);
Attachment: [text/bzr-bundle] bzr/tor.didriksen@oracle.com-20101210150603-cxd7s460s5kbf0ar.bundle