| Bug #88181 | Another example of optimizer choosing ref over range when range is faster | ||
|---|---|---|---|
| Submitted: | 23 Oct 2017 5:30 | Modified: | 23 Oct 2017 7:09 |
| Reporter: | Jaime Sicam | Email Updates: | |
| Status: | Verified | Impact on me: | |
| Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
| Version: | 5.7.20 | OS: | Any |
| Assigned to: | CPU Architecture: | Any | |
[23 Oct 2017 5:30]
Jaime Sicam
[23 Oct 2017 7:09]
MySQL Verification Team
Hello Jaime Sicam, Thank you for the report and test case. Thanks, Umesh
[23 Oct 2017 9:21]
Øystein Grøvlen
Posted by developer:
It seems an 0.2*#rows is added to the cost of range access. This makes is more expensive that ref.
This is the trace from the range optimizer:
"range_scan_alternatives": [
{
"index": "PRIMARY",
"ranges": [
"10 < a"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": false,
"rows": 1196250,
"cost": 241341,
"chosen": true
},
{
"index": "i1",
"ranges": [
"7 <= b <= 7 AND 10 < a"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 390,
"cost": 80.524,
"chosen": true
},
{
"index": "i2",
"ranges": [
"7 <= b <= 7"
],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": true,
"rows": 400,
"cost": 82.177,
"chosen": false,
"cause": "cost"
}
],
We see that i1 has the lowest cost (80.524). However, when selecting access method, we see that the range cost is higher:
"best_access_path": {
"considered_access_paths": [
{
"access_type": "ref",
"index": "i1",
"rows": 2.39e6,
"cost": 487810,
"chosen": true
},
{
"access_type": "ref",
"index": "i2",
"rows": 400,
"cost": 82.167,
"chosen": true
},
{
"rows_to_scan": 390,
"access_type": "range",
"range_details": {
"used_index": "i1"
},
"resulting_rows": 390,
"cost": 158.52,
"chosen": false
}
]
Hence, range access is selected.
[23 Oct 2017 9:41]
Øystein Grøvlen
Posted by developer: Sorry, for typo at end of last comment. I meant to say: "Hence, ref access is selected"
[11 Jun 2018 19:54]
Vinicius Malvestio Grippa
The problem can be verified on previous versions as well.
master [localhost] {msandbox} ((none)) > explain format=json SELECT a.profile_id,a.ad_id_in_target,a.affcode FROM test.test_table a USE KEY(profile_id_date) WHERE profile_id = 53 AND DATE >= '2018-02-28' AND DATE < NOW();
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
"query_block": {
"select_id": 1,
"table": {
"table_name": "a",
"access_type": "ref",
"possible_keys": [
"profile_id_date"
],
"key": "profile_id_date",
"used_key_parts": [
"profile_id"
],
"key_length": "4",
"ref": [
"const"
],
"rows": 19818,
"filtered": 100,
"index_condition": "((`test`.`a`.`date` >= '2018-02-28') and (`test`.`a`.`date` < now()))"
}
}
} |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
master [localhost] {msandbox} ((none)) > explain format=json SELECT a.profile_id,a.ad_id_in_target,a.affcode FROM test.test_table a WHERE profile_id = 53 AND DATE >= '2018-02-28' AND DATE < NOW();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
"query_block": {
"select_id": 1,
"table": {
"table_name": "a",
"access_type": "ref",
"possible_keys": [
"affcode_per_day",
"profile_ad_id_date",
"profile_affcode",
"profile_id_date"
],
"key": "affcode_per_day",
"used_key_parts": [
"profile_id"
],
"key_length": "4",
"ref": [
"const"
],
"rows": 23410,
"filtered": 100,
"using_index": true,
"attached_condition": "((`test`.`a`.`date` >= '2018-02-28') and (`test`.`a`.`date` < <cache>(now())))"
}
}
} |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
[11 Jun 2018 19:55]
Vinicius Malvestio Grippa
master [localhost] {msandbox} ((none)) > select @@version;
+------------+
| @@version |
+------------+
| 5.6.36-log |
+------------+
1 row in set (0.00 sec)
