1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00
Files
mariadb/mysql-test/r
Sergey Petrunya 244d4b532a 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'.
2014-04-12 01:01:32 +04:00
..
2013-10-29 15:08:44 +01:00
2014-02-01 00:54:03 +01:00
2012-10-01 13:15:29 +02:00
2012-08-27 18:13:17 +02:00
2014-03-16 21:03:01 +01:00
2014-02-25 16:04:35 +01:00
2012-08-24 13:57:39 +02:00
2013-11-04 08:43:56 +01:00
2013-12-17 17:28:48 +04:00
2014-03-26 22:25:38 +01:00
2014-03-26 22:25:38 +01:00
2013-12-17 17:28:48 +04:00
2013-12-17 17:28:48 +04:00
2013-12-17 17:28:48 +04:00
2013-12-17 17:28:48 +04:00
2013-12-17 17:28:48 +04:00
2014-03-26 22:25:38 +01:00
2013-12-17 17:28:48 +04:00
2013-12-17 17:28:48 +04:00
2013-12-20 12:42:33 +04:00
2014-03-26 22:25:38 +01:00
2013-12-17 17:28:48 +04:00
2014-03-26 22:25:38 +01:00
2014-02-26 15:28:07 +01:00
2014-02-26 15:28:07 +01:00
2014-02-26 15:28:07 +01:00
2014-02-26 15:28:07 +01:00
2014-03-26 22:25:38 +01:00
2014-01-26 16:41:15 +02:00
2014-02-25 16:04:35 +01:00
2013-05-21 09:42:10 +02:00
2014-02-26 15:28:07 +01:00
2012-08-31 14:15:52 +02:00
2012-09-27 20:09:46 +02:00
2013-10-29 15:08:44 +01:00
2013-01-25 11:24:42 +01:00
2014-03-16 19:21:37 +01:00
2012-09-09 01:22:06 +03:00
2013-07-21 16:39:19 +02:00
2014-03-25 11:09:12 +01:00
2013-03-27 10:03:28 +01:00
2012-05-21 20:54:41 +02:00
2014-03-26 22:25:38 +01:00
2013-07-12 16:24:20 +02:00
2013-09-21 10:14:42 +02:00
2013-08-20 14:48:29 +03:00
2014-03-16 21:03:01 +01:00
2012-06-20 15:01:28 +04:00
2013-09-21 10:14:42 +02:00
2013-11-21 14:25:28 +01:00
2014-02-22 22:51:20 +01:00
2013-07-21 16:39:19 +02:00
2014-02-26 15:28:07 +01:00
2013-10-29 15:08:44 +01:00
2013-11-04 08:43:56 +01:00
2014-02-03 15:22:39 +01:00
2013-11-11 22:21:39 -08:00
2014-02-26 15:28:07 +01:00
2014-03-05 23:20:10 +01:00
2014-03-05 23:20:10 +01:00
2014-02-03 15:22:39 +01:00
2013-11-23 00:50:54 +01:00
2013-05-04 13:05:24 +04:00
2013-09-21 10:14:42 +02:00
2014-03-16 21:03:01 +01:00
2014-03-16 21:03:01 +01:00
2014-02-27 13:54:05 -08:00
2013-04-15 15:09:22 +02:00
2013-01-29 15:10:47 +01:00
2013-08-20 14:48:29 +03:00
2013-07-21 19:24:20 +02:00
2013-07-08 16:49:42 +04:00
2013-12-16 13:02:21 +01:00
2014-01-31 12:06:28 +02:00
2014-02-01 00:54:03 +01:00
2013-07-21 16:39:19 +02:00
2014-02-03 15:22:39 +01:00
2013-11-04 21:47:54 +01:00
2014-03-16 21:03:01 +01:00
2013-09-25 21:07:06 +03:00
2013-11-08 20:59:08 +04:00
2012-06-20 13:41:31 +04:00
2013-10-13 13:43:29 -07:00
2014-03-07 21:05:28 +01:00
2014-02-01 00:54:03 +01:00
2013-07-17 21:24:29 +02:00
2013-07-21 16:39:19 +02:00
2012-07-13 22:17:32 +03:00
2013-07-17 21:24:29 +02:00
2014-03-26 22:25:38 +01:00
2013-07-08 16:49:42 +04:00
2014-03-26 22:25:38 +01:00
2014-01-22 15:29:36 +01:00
2013-11-19 13:16:25 +01:00
2014-02-01 00:54:03 +01:00
2013-07-21 16:39:19 +02:00
2013-03-29 17:53:21 +02:00
2013-07-03 10:22:19 +03:00
2014-02-26 15:28:07 +01:00
2013-10-17 19:01:57 +03:00
2013-07-21 16:39:19 +02:00
2014-02-03 15:22:39 +01:00
2013-07-05 10:40:45 +04:00
2013-02-20 18:01:36 -08:00
2014-02-26 15:28:07 +01:00
2014-03-29 11:32:49 +01:00
2012-10-19 20:38:59 +02:00
2014-02-03 15:22:39 +01:00
2013-07-12 10:17:52 +02:00
2013-07-05 17:21:14 +03:00
2013-07-05 17:21:14 +03:00
2014-02-03 15:22:39 +01:00
2013-07-05 17:21:14 +03:00
2013-07-05 17:21:14 +03:00
2012-09-27 20:09:46 +02:00
2013-11-04 08:43:56 +01:00
2014-02-26 15:28:07 +01:00
2013-06-06 21:32:29 +02:00
2012-08-23 00:32:25 +03:00
2013-07-17 21:24:29 +02:00
2013-07-21 19:24:20 +02:00
2014-02-26 15:28:07 +01:00
2014-03-26 22:25:38 +01:00
2013-09-21 10:14:42 +02:00
2013-11-13 13:38:37 +01:00
2014-02-22 22:51:20 +01:00
2013-02-28 22:47:29 +01:00
2014-03-26 22:25:38 +01:00
2014-02-10 17:00:51 -08:00
2014-02-10 17:00:51 -08:00
2014-02-10 17:00:51 -08:00
2014-02-25 16:04:35 +01:00
2014-03-26 22:25:38 +01:00
2014-02-25 16:04:35 +01:00
2013-12-15 15:57:26 +01:00
2012-10-19 20:38:59 +02:00
2014-02-26 15:28:07 +01:00
2013-03-10 12:46:56 +01:00
2013-01-23 15:18:05 -08:00
2014-02-14 14:09:29 +01:00
2013-12-13 13:00:38 +01:00
2013-11-04 21:47:54 +01:00
2013-06-06 21:32:29 +02:00
2014-03-16 13:59:44 +01:00
2013-05-08 13:36:17 +04:00
2014-03-26 22:25:38 +01:00
2012-04-10 08:28:13 +02:00
2014-03-23 15:15:07 +04:00