summaryrefslogtreecommitdiff
path: root/mysql-test/r/subselect_innodb.result
diff options
context:
space:
mode:
authorSergey Petrunya <psergey@askmonty.org>2014-04-12 01:01:32 +0400
committerSergey Petrunya <psergey@askmonty.org>2014-04-12 01:01:32 +0400
commit244d4b532a1f0dc103192ec6f6990056124ee93d (patch)
tree947d776cb7b67b07f4db976a37c2b6e2a0d2e9d5 /mysql-test/r/subselect_innodb.result
parent2bbca99018422b80306443a93f524a2d58373ecd (diff)
downloadmariadb-git-244d4b532a1f0dc103192ec6f6990056124ee93d.tar.gz
MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686)
Add a testcase and backport this fix: Bug#14338686: MYSQL IS GENERATING DIFFERENT AND SLOWER (IN NEWER VERSIONS) EXECUTION PLAN PROBLEM: While checking for an index to sort for the order by clause in this query "SELECT datestamp FROM contractStatusHistory WHERE contract_id = contracts.id ORDER BY datestamp asc limit 1;" we do not calculate the number of rows to be examined correctly. As a result we choose index 'idx_contractStatusHistory_datestamp' defined on the 'datestamp' field, rather than choosing index 'contract_id'. And hence the lower performance. ANALYSIS: While checking if an index is present to give the records in sorted order(datestamp), we consider the selectivity of the 'ref_key'(contract_id here) using 'table->quick_condition_rows'. 'ref_key' here can be an index from 'REF_ACCESS' or from 'RANGE'. As this is a 'REF_ACCESS', 'table->quick_condition_rows' is not set to the actual value which is 2. Instead is set to the number of tuples present in the table indicating that every row that is selected would be satisfying the condition present in the query. Hence, the selectivity becomes 1 even when we choose the index on the order by column instead of the join_condition. But, in reality as only 2 rows satisy the condition, we need to examine half of the entire data set to get one tuple when we choose index on the order by column. Had we chosen the 'REF_ACCESS' we would have examined only 2 tuples. Hence the delay in executing the query specified. FIX: While calculating the selectivity of the ref_key: For REF_ACCESS consider quick_rows[ref_key] if range optimizer has an estimate for this key. Else consider 'rec_per_key' statistic. For RANGE ACCESS consider 'table->quick_condition_rows'.
Diffstat (limited to 'mysql-test/r/subselect_innodb.result')
-rw-r--r--mysql-test/r/subselect_innodb.result24
1 files changed, 23 insertions, 1 deletions
diff --git a/mysql-test/r/subselect_innodb.result b/mysql-test/r/subselect_innodb.result
index 159b1d4be81..07d00e96549 100644
--- a/mysql-test/r/subselect_innodb.result
+++ b/mysql-test/r/subselect_innodb.result
@@ -526,4 +526,26 @@ t1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 10
2 DEPENDENT SUBQUERY t2 ref key1 key1 5 test.t1.a 1 Using where
-drop table t1, t2;
+#
+# MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686)
+#
+alter table t2 add key2 int;
+update t2 set key2=key1;
+alter table t2 add key(key2);
+analyze table t2;
+Table Op Msg_type Msg_text
+test.t2 analyze status OK
+flush tables;
+# Table tsubq must use 'ref' + Using filesort (not 'index' w/o filesort)
+explain select
+(SELECT
+concat(id, '-', key1, '-', col1)
+FROM t2
+WHERE t2.key1 = t1.a
+ORDER BY t2.key2 ASC LIMIT 1)
+from
+t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL #
+2 DEPENDENT SUBQUERY t2 ref key1 key1 5 test.t1.a # Using where; Using filesort
+drop table t1,t2;