diff --git a/mysql-test/main/analyze_format_json.result b/mysql-test/main/analyze_format_json.result
index b98500d4c34ca346abc03dfd5bfe11c124012b82..c306c2fc1b3fb26d73570e23aa81b902d5e97906 100644
--- a/mysql-test/main/analyze_format_json.result
+++ b/mysql-test/main/analyze_format_json.result
@@ -143,7 +143,7 @@ ANALYZE
         "attached_condition": "tbl2.b < 60"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "1Kb",
       "join_type": "BNL",
       "r_filtered": 100
     }
@@ -181,7 +181,7 @@ ANALYZE
         "attached_condition": "tbl2.b < 60"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "1Kb",
       "join_type": "BNL",
       "attached_condition": "tbl1.c > tbl2.c",
       "r_filtered": 15.833
@@ -625,7 +625,7 @@ ANALYZE
         "attached_condition": "<in_optimizer>(t2.b,t2.b in (subquery#2))"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "65",
       "join_type": "BNL",
       "attached_condition": "<in_optimizer>(t2.b,t2.b in (subquery#2))",
       "r_filtered": null
@@ -711,7 +711,7 @@ ANALYZE
                 "attached_condition": "t3.f3 in (1,2)"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "1",
               "join_type": "BNL",
               "r_filtered": null
             },
@@ -742,7 +742,7 @@ ANALYZE
                         "r_filtered": null
                       },
                       "buffer_type": "flat",
-                      "buffer_size": "256Kb",
+                      "buffer_size": "65",
                       "join_type": "BNL",
                       "attached_condition": "t2.f2 = t3.f3",
                       "r_filtered": null
diff --git a/mysql-test/main/analyze_stmt_orderby.result b/mysql-test/main/analyze_stmt_orderby.result
index 3f6a4973d0a381e37c6e2528a4e490231e33c3d1..23f4c39dd5bd0a3acc8793e097ce3ffedb87d25e 100644
--- a/mysql-test/main/analyze_stmt_orderby.result
+++ b/mysql-test/main/analyze_stmt_orderby.result
@@ -426,7 +426,7 @@ ANALYZE
             "r_filtered": 100
           },
           "buffer_type": "flat",
-          "buffer_size": "256Kb",
+          "buffer_size": "65",
           "join_type": "BNL",
           "attached_condition": "t3.a = t0.a",
           "r_filtered": 10
@@ -497,7 +497,7 @@ ANALYZE
                 "r_filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "119",
               "join_type": "BNL",
               "attached_condition": "t5.a = t6.a",
               "r_filtered": 21.429
@@ -537,7 +537,7 @@ EXPLAIN
                 "filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "119",
               "join_type": "BNL",
               "attached_condition": "t5.a = t6.a"
             }
diff --git a/mysql-test/main/cte_recursive.result b/mysql-test/main/cte_recursive.result
index f2ae9929145f1edb5b09f3a4d3a1f43e3a0e17a6..0b22da8f72a0679cd1602e02529a5b0d85b3419a 100644
--- a/mysql-test/main/cte_recursive.result
+++ b/mysql-test/main/cte_recursive.result
@@ -1409,7 +1409,7 @@ EXPLAIN
                                     "filtered": 100
                                   },
                                   "buffer_type": "flat",
-                                  "buffer_size": "256Kb",
+                                  "buffer_size": "686",
                                   "join_type": "BNL",
                                   "attached_condition": "prev_gen.father = folks.`id` or prev_gen.mother = folks.`id`"
                                 }
@@ -1636,16 +1636,16 @@ i div 4 - (i % 4) = ps.i div 4 - (ps.i % 4)
 )
 SELECT regexp_replace(board,concat('(',REPEAT('.', 4),')'),'\\1\n') n_queens FROM solutions WHERE n_queens = 4;
 n_queens
--*--
----*
-*---
---*-
-
 --*-
 *---
 ---*
 -*--
 
+-*--
+---*
+*---
+--*-
+
 #
 # MDEV-10883: execution of prepared statement from SELECT
 #             with recursive CTE that renames columns
diff --git a/mysql-test/main/derived_cond_pushdown.result b/mysql-test/main/derived_cond_pushdown.result
index 0d555ded687ec9b45f486ff635af8d549e92becf..e7e2df4f582d7651856e11690d262488d0ef4ac5 100644
--- a/mysql-test/main/derived_cond_pushdown.result
+++ b/mysql-test/main/derived_cond_pushdown.result
@@ -136,7 +136,7 @@ EXPLAIN
         "attached_condition": "v1.max_c > 214"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "t2.a > v1.a",
       "materialized": {
@@ -291,7 +291,7 @@ EXPLAIN
         "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
       "materialized": {
@@ -359,7 +359,7 @@ EXPLAIN
         "attached_condition": "v1.max_c > 300 or v1.max_c < 135"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.b = t2.b and v1.max_c > 300 and v1.avg_c > t2.d or v1.a = t2.a and v1.max_c < 135 and v1.max_c < t2.c",
       "materialized": {
@@ -416,7 +416,7 @@ EXPLAIN
         "attached_condition": "v1.a > 6"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "t2.b > v1.b",
       "materialized": {
@@ -483,7 +483,7 @@ EXPLAIN
         "attached_condition": "v2.b > 25"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "t2.a < v2.a",
       "materialized": {
@@ -571,7 +571,7 @@ EXPLAIN
         "attached_condition": "v1.a > 7 or v1.a < 2"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a > 7 and t2.c < v1.max_c or v1.a < 2 and t2.b < v1.b",
       "materialized": {
@@ -652,7 +652,7 @@ EXPLAIN
         "attached_condition": "v2.a > 7 or v2.a > 5"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v2.a > 7 and t2.c < v2.max_c or v2.a > 5 and t2.b < v2.b",
       "materialized": {
@@ -719,7 +719,7 @@ EXPLAIN
         "attached_condition": "v1.a > 4 or v1.a < 2"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a > 4 and v1.b > t2.b and v1.max_c = t2.d or v1.a < 2 and v1.max_c < t2.c and v1.max_c = t2.d",
       "materialized": {
@@ -779,7 +779,7 @@ EXPLAIN
         "attached_condition": "v1.a < 2 and v1.max_c > 400"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "t2.b > v1.b",
       "materialized": {
@@ -991,7 +991,7 @@ EXPLAIN
         "attached_condition": "v1.a > 7 and v1.max_c > 300 or v1.a < 4 and v1.max_c < 500"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a > 7 and v1.max_c > 300 and t2.c < v1.max_c or v1.a < 4 and v1.max_c < 500 and t2.b < v1.b",
       "materialized": {
@@ -1080,7 +1080,7 @@ EXPLAIN
         "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7",
       "materialized": {
@@ -1158,7 +1158,7 @@ EXPLAIN
         "attached_condition": "v1.a < 2 and v1.max_c > 120 or v1.a > 7"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.b = t2.b and v1.a < 2 and v1.max_c > 120 or v1.a > 7",
       "materialized": {
@@ -1225,7 +1225,7 @@ EXPLAIN
         "attached_condition": "v1.a < 2 and v1.max_c < 200 or v1.a > 4"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a < 2 and v1.max_c < 200 and t2.c > v1.max_c and v1.max_c = t2.d or v1.max_c = t2.c and v1.a > 4 and t2.c < 500 and t2.b < v1.b",
       "materialized": {
@@ -1302,7 +1302,7 @@ EXPLAIN
         "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
       "materialized": {
@@ -1345,7 +1345,7 @@ EXPLAIN
         "attached_condition": "v1.max_c > 400 or v1.max_c < 135"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.max_c > 400 and t2.a > v1.a or v1.max_c < 135 and t2.a < v1.a",
       "materialized": {
@@ -1684,7 +1684,7 @@ EXPLAIN
         "attached_condition": "v1.a = 3 and v1.b = 3"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -1734,7 +1734,7 @@ EXPLAIN
         "attached_condition": "v1.a = 1 and v1.b = 21"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -1807,7 +1807,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "220",
       "join_type": "BNL",
       "attached_condition": "t.b = v.b or v.max_c > 20"
     }
@@ -1931,7 +1931,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a = t2.a and t2.a < 4 or v1.max_c = t2.c and t2.c > 150",
       "materialized": {
@@ -2061,7 +2061,7 @@ EXPLAIN
         "attached_condition": "v1.a = 8 and v1.max_c = 404"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -2359,7 +2359,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "162",
       "join_type": "BNL"
     }
   }
@@ -2471,7 +2471,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -2598,7 +2598,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a = t2.a or v1.b = t2.b and (v1.a = 1 or v1.a = 6)",
       "materialized": {
@@ -2693,7 +2693,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a = 1 or v1.b = 21 or t2.a = 2",
       "materialized": {
@@ -2761,7 +2761,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a < t2.a or t2.a < 11",
       "materialized": {
@@ -2943,7 +2943,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.b is not null",
       "materialized": {
@@ -3038,7 +3038,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a = t2.a or t2.c < 115",
       "materialized": {
@@ -3067,7 +3067,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "incremental",
-      "buffer_size": "256Kb",
+      "buffer_size": "4Kb",
       "join_type": "BNL",
       "attached_condition": "v1.a = t2.a and v2.a = t2.a or v2.b > 13 and t2.c < 115",
       "materialized": {
@@ -3138,7 +3138,7 @@ EXPLAIN
         "attached_condition": "v1.max_c < 300"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -3167,7 +3167,7 @@ EXPLAIN
         "attached_condition": "v2.b < 50 or v2.b = 19"
       },
       "buffer_type": "incremental",
-      "buffer_size": "256Kb",
+      "buffer_size": "4Kb",
       "join_type": "BNL",
       "attached_condition": "(v2.a = v1.a or v1.a = t2.a) and (v2.b < 50 or v2.b = 19)",
       "materialized": {
@@ -3317,7 +3317,7 @@ EXPLAIN
         "attached_condition": "v1.a = 1 and v1.b > 10"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.b is not null",
       "materialized": {
@@ -3435,7 +3435,7 @@ EXPLAIN
         "attached_condition": "t.a = 'b'"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "220",
       "join_type": "BNL"
     }
   }
@@ -3544,7 +3544,7 @@ EXPLAIN
         "attached_condition": "v2.max_c > 300"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "715",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -3574,7 +3574,7 @@ EXPLAIN
         "attached_condition": "v1.max_c < 500"
       },
       "buffer_type": "incremental",
-      "buffer_size": "256Kb",
+      "buffer_size": "9Kb",
       "join_type": "BNL",
       "attached_condition": "v1.a = v2.a or v1.a = t2.a",
       "materialized": {
@@ -3790,7 +3790,7 @@ EXPLAIN
         "attached_condition": "v1.avg_c < 400 or v1.a > 1"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "(v1.avg_c < 400 or v1.a > 1) and v1.a is not null and v1.b is not null",
       "materialized": {
@@ -3935,7 +3935,7 @@ EXPLAIN
         "attached_condition": "(v1.a = 1 or v1.max_c < 300) and v1.b > 25"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v1.a = 1 or v1.max_c < 300",
       "materialized": {
@@ -4128,7 +4128,7 @@ EXPLAIN
                 "attached_condition": "v1.max_c > 100 and v1.a > 7"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "materialized": {
                 "query_block": {
@@ -4229,7 +4229,7 @@ EXPLAIN
                 "attached_condition": "v1.b = 19 and v1.a < 5"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "materialized": {
                 "query_block": {
@@ -4271,7 +4271,7 @@ EXPLAIN
                 "attached_condition": "v1.max_c > 400 or v1.avg_c > 270"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "attached_condition": "(v1.max_c > 400 or v1.avg_c > 270) and v1.a < t2.a",
               "materialized": {
@@ -4383,7 +4383,7 @@ EXPLAIN
                 "attached_condition": "v1.a = 1 or v1.a = 6"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "attached_condition": "(v1.a = t2.a or v1.b = t2.b) and (v1.a = 1 or v1.a = 6)",
               "materialized": {
@@ -4426,7 +4426,7 @@ EXPLAIN
                 "attached_condition": "v1.a > 3 and v1.b > 27 or v1.max_c > 550"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "attached_condition": "v1.a > 3 and v1.b > 27 or v1.max_c > 550",
               "materialized": {
@@ -4540,7 +4540,7 @@ EXPLAIN
                 "attached_condition": "v1.a = 1 and (v1.max_c < 500 or v1.avg_c > 500)"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "attached_condition": "v1.max_c < 500 or v1.avg_c > 500",
               "materialized": {
@@ -4584,7 +4584,7 @@ EXPLAIN
                 "attached_condition": "v2.b > 10"
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "238",
               "join_type": "BNL",
               "attached_condition": "v2.a < t2.b or v2.max_c > 200",
               "materialized": {
@@ -4706,7 +4706,7 @@ EXPLAIN
         "attached_condition": "v_union.a < 3 and v_union.c > 100"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -4814,7 +4814,7 @@ EXPLAIN
         "attached_condition": "(v_union.a < 2 or v_union.c > 800) and v_union.b > 12"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "v_union.a < 2 or v_union.c > 800",
       "materialized": {
@@ -4911,7 +4911,7 @@ EXPLAIN
         "attached_condition": "v_union.a = 1 and v_union.c < 200"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -5078,7 +5078,7 @@ EXPLAIN
         "attached_condition": "v1.a = 1"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -5108,7 +5108,7 @@ EXPLAIN
         "attached_condition": "v_union.a = 1"
       },
       "buffer_type": "incremental",
-      "buffer_size": "256Kb",
+      "buffer_size": "4Kb",
       "join_type": "BNL",
       "attached_condition": "v_union.c > 800 or v1.max_c > 200",
       "materialized": {
@@ -5923,7 +5923,7 @@ EXPLAIN
         "attached_condition": "v1.a > 5 and v1.b > 12"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "333",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -6306,7 +6306,7 @@ EXPLAIN
         "attached_condition": "v4.b > 10 and v4.a > 1 or v4.b < 20"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "238",
       "join_type": "BNL",
       "attached_condition": "(v4.b > 10 and v4.a > 1 or v4.b < 20) and v4.a is not null",
       "materialized": {
@@ -6451,7 +6451,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "333",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -6971,7 +6971,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "715",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -7766,7 +7766,7 @@ EXPLAIN
         "attached_condition": "v2.pk > 2"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "119",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -8763,7 +8763,7 @@ EXPLAIN
         "attached_condition": "v2.i2 = 1"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "65",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -8879,7 +8879,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "163",
       "join_type": "BNL",
       "attached_condition": "t1.id2 = vc.id2"
     }
@@ -8952,7 +8952,7 @@ EXPLAIN
         "attached_condition": "v1.a = 1 and v1.b = 1 and v1.max_c > 30"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -9027,7 +9027,7 @@ EXPLAIN
         "attached_condition": "v1.a = 1 and v1.b = 1 and v1.d = 1 and v1.max_c > 30"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -9759,7 +9759,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "65",
       "join_type": "BNL",
       "attached_condition": "t1.a = dt1.a"
     }
@@ -9818,7 +9818,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "119",
       "join_type": "BNL",
       "attached_condition": "t1.a = dt.a"
     }
@@ -10314,7 +10314,7 @@ EXPLAIN
               "attached_condition": "t1.f is not null"
             },
             "buffer_type": "flat",
-            "buffer_size": "256Kb",
+            "buffer_size": "64",
             "join_type": "BNL"
           }
         }
@@ -10489,7 +10489,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "65",
       "join_type": "BNL",
       "attached_condition": "t1.f2 = t.f2"
     }
@@ -10648,7 +10648,7 @@ EXPLAIN
         "attached_condition": "v1.a = 8"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -10736,7 +10736,7 @@ EXPLAIN
         "attached_condition": "v1.a = 8"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -11226,7 +11226,7 @@ EXPLAIN
         "attached_condition": "v1.a > 3 or v1.a = 1 and v1.c < 110"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "attached_condition": "v1.c = t2.c and v1.a > 3 and t2.c > 110 or v1.a = 1 and v1.c < 110",
       "materialized": {
@@ -11567,7 +11567,7 @@ EXPLAIN
         "attached_condition": "v1.a = 6"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -11657,7 +11657,7 @@ EXPLAIN
         "attached_condition": "v1.a = 6"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "materialized": {
         "query_block": {
@@ -12151,7 +12151,7 @@ EXPLAIN
         "attached_condition": "v1.a > 1 or v1.a = 1 and v1.c > 500"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "attached_condition": "v1.c = t2.c and v1.a > 1 and t2.c < 500 or v1.a = 1 and v1.c > 500",
       "materialized": {
@@ -14011,7 +14011,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "attached_condition": "t2.b = v1.b"
     }
@@ -14107,7 +14107,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "173",
       "join_type": "BNL",
       "attached_condition": "t2.b = v1.b"
     }
@@ -16414,7 +16414,7 @@ EXPLAIN
                     "filtered": 100
                   },
                   "buffer_type": "flat",
-                  "buffer_size": "256Kb",
+                  "buffer_size": "65",
                   "join_type": "BNL",
                   "attached_condition": "t2.i = t1.i and t2.i = t1.i"
                 }
diff --git a/mysql-test/main/except.result b/mysql-test/main/except.result
index b9959a3f520b124a1cb8d73d34ab465c38639c2e..9c5a3eaa93ce709cad7ca0983fc98b6a4dca7476 100644
--- a/mysql-test/main/except.result
+++ b/mysql-test/main/except.result
@@ -255,7 +255,7 @@ EXPLAIN
                 "filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "119",
               "join_type": "BNL"
             }
           }
@@ -278,7 +278,7 @@ EXPLAIN
                 "filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "119",
               "join_type": "BNL"
             }
           }
@@ -324,7 +324,7 @@ ANALYZE
                 "r_filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "119",
               "join_type": "BNL",
               "r_filtered": 100
             }
@@ -358,7 +358,7 @@ ANALYZE
                 "r_filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "119",
               "join_type": "BNL",
               "r_filtered": 100
             }
@@ -420,7 +420,7 @@ ANALYZE
                       "r_filtered": 100
                     },
                     "buffer_type": "flat",
-                    "buffer_size": "256Kb",
+                    "buffer_size": "119",
                     "join_type": "BNL",
                     "r_filtered": 100
                   }
@@ -454,7 +454,7 @@ ANALYZE
                       "r_filtered": 100
                     },
                     "buffer_type": "flat",
-                    "buffer_size": "256Kb",
+                    "buffer_size": "119",
                     "join_type": "BNL",
                     "r_filtered": 100
                   }
diff --git a/mysql-test/main/explain_json.result b/mysql-test/main/explain_json.result
index 2f60a827cc06c417d905527db0c87d847f6b1d00..b918e7614af65ef1942bf13f54082a6eccd98d5a 100644
--- a/mysql-test/main/explain_json.result
+++ b/mysql-test/main/explain_json.result
@@ -373,7 +373,7 @@ EXPLAIN
         "attached_condition": "tbl2.b < 5"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "1Kb",
       "join_type": "BNL",
       "attached_condition": "tbl2.a = tbl1.a"
     }
@@ -640,7 +640,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "1Kb",
       "join_type": "BNL"
     }
   }
@@ -674,7 +674,7 @@ EXPLAIN
         "first_match": "t2"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "141",
       "join_type": "BNL",
       "attached_condition": "t1.b = t2.b and t1.a = t2.a"
     }
@@ -711,7 +711,7 @@ EXPLAIN
           "filtered": 100
         },
         "buffer_type": "flat",
-        "buffer_size": "256Kb",
+        "buffer_size": "206",
         "join_type": "BNL",
         "attached_condition": "t1.b = t2.b and t1.a = t2.a"
       }
@@ -825,7 +825,7 @@ EXPLAIN
               "filtered": 100
             },
             "buffer_type": "flat",
-            "buffer_size": "256Kb",
+            "buffer_size": "65",
             "join_type": "BNL",
             "attached_condition": "t2.b <> outer_t1.a"
           }
@@ -876,7 +876,7 @@ EXPLAIN
               "filtered": 100
             },
             "buffer_type": "flat",
-            "buffer_size": "256Kb",
+            "buffer_size": "1Kb",
             "join_type": "BNL",
             "attached_condition": "tbl2.b = tbl1.b"
           }
@@ -1492,7 +1492,7 @@ EXPLAIN
         "index_condition_bka": "t4.b + 1 <= t3.b + 1"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "400",
       "join_type": "BKA",
       "mrr_type": "Rowid-ordered scan"
     }
@@ -1535,7 +1535,7 @@ ANALYZE
         "index_condition_bka": "t4.b + 1 <= t3.b + 1"
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "400",
       "join_type": "BKA",
       "mrr_type": "Rowid-ordered scan",
       "r_filtered": 100
diff --git a/mysql-test/main/in_subq_cond_pushdown.result b/mysql-test/main/in_subq_cond_pushdown.result
index 2dd66547e790020a1442af5a172ea230a529c923..eef320d2d046dcad8df7bf3d5ba3c6d58f5c4f0d 100644
--- a/mysql-test/main/in_subq_cond_pushdown.result
+++ b/mysql-test/main/in_subq_cond_pushdown.result
@@ -605,7 +605,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "119",
       "join_type": "BNL",
       "attached_condition": "t1.c = t3.y and t1.a is not null"
     },
@@ -1505,7 +1505,7 @@ EXPLAIN
         "filtered": 100
       },
       "buffer_type": "flat",
-      "buffer_size": "256Kb",
+      "buffer_size": "119",
       "join_type": "BNL",
       "attached_condition": "t1.a = t3.x and t1.c is not null"
     },
diff --git a/mysql-test/main/intersect.result b/mysql-test/main/intersect.result
index 5c7e9dfc6f6dbf45a19bee5acad9934800ef3638..f2d36e1d0e91fa74e5c9773f0a14d45596da4047 100644
--- a/mysql-test/main/intersect.result
+++ b/mysql-test/main/intersect.result
@@ -316,7 +316,7 @@ EXPLAIN
                 "filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "65",
               "join_type": "BNL"
             }
           }
@@ -380,7 +380,7 @@ ANALYZE
                 "r_filtered": 100
               },
               "buffer_type": "flat",
-              "buffer_size": "256Kb",
+              "buffer_size": "65",
               "join_type": "BNL",
               "r_filtered": 100
             }
@@ -459,7 +459,7 @@ ANALYZE
                       "r_filtered": 100
                     },
                     "buffer_type": "flat",
-                    "buffer_size": "256Kb",
+                    "buffer_size": "65",
                     "join_type": "BNL",
                     "r_filtered": 100
                   }
diff --git a/mysql-test/main/myisam_mrr,64bit.rdiff b/mysql-test/main/myisam_mrr,64bit.rdiff
new file mode 100644
index 0000000000000000000000000000000000000000..82f6dfabb6577693443efee98879e962b9d1b669
--- /dev/null
+++ b/mysql-test/main/myisam_mrr,64bit.rdiff
@@ -0,0 +1,13 @@
+--- main/myisam_mrr.result	2019-02-04 13:47:00.000000000 +0530
++++ main/myisam_mrr,64bit.reject	2019-02-04 13:50:01.000000000 +0530
+@@ -617,8 +617,8 @@
+ show status like 'handler_mrr%';
+ Variable_name	Value
+ Handler_mrr_init	1
+-Handler_mrr_key_refills	0
+-Handler_mrr_rowid_refills	0
++Handler_mrr_key_refills	1
++Handler_mrr_rowid_refills	1
+ set join_buffer_size=10;
+ explain select sum(t1.b) from t0,t1 where t0.a=t1.a;
+ id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
diff --git a/mysql-test/main/myisam_mrr.test b/mysql-test/main/myisam_mrr.test
index b15fdf60ce7a8076d6517353d95833227b599466..60d96c602ef3d973c817e5b001872f9190c984df 100644
--- a/mysql-test/main/myisam_mrr.test
+++ b/mysql-test/main/myisam_mrr.test
@@ -12,6 +12,7 @@ set @mrr_buffer_size_save= @@mrr_buffer_size;
 set mrr_buffer_size=79;
 
 -- source include/mrr_tests.inc
+-- source include/word_size.inc
 
 set @@mrr_buffer_size= @mrr_buffer_size_save;
 
diff --git a/mysql-test/main/mysqld--help.result b/mysql-test/main/mysqld--help.result
index e611c9b2dbf519ab48a927192d64c64da9f80f6a..bff1696d3d984e35097585159e507feab8e334e5 100644
--- a/mysql-test/main/mysqld--help.result
+++ b/mysql-test/main/mysqld--help.result
@@ -15,6 +15,10 @@ The following specify which files/extra groups are read (specified before remain
  --alter-algorithm[=name] 
  Specify the alter table algorithm. One of: DEFAULT, COPY,
  INPLACE, NOCOPY, INSTANT
+ --analyze-sample-percentage=# 
+ Percentage of rows from the table ANALYZE TABLE will
+ sample to collect table statistics. Set to 0 to let
+ MariaDB decide what percentage of rows to sample.
  -a, --ansi          Use ANSI SQL syntax instead of MySQL syntax. This mode
  will also set transaction isolation level 'serializable'.
  --auto-increment-increment[=#] 
@@ -1386,6 +1390,7 @@ The following specify which files/extra groups are read (specified before remain
 Variables (--variable-name=value)
 allow-suspicious-udfs FALSE
 alter-algorithm DEFAULT
+analyze-sample-percentage 100
 auto-increment-increment 1
 auto-increment-offset 1
 autocommit TRUE
@@ -1437,7 +1442,7 @@ encrypt-binlog FALSE
 encrypt-tmp-disk-tables FALSE
 encrypt-tmp-files FALSE
 enforce-storage-engine (No default value)
-eq-range-index-dive-limit 10
+eq-range-index-dive-limit 200
 event-scheduler OFF
 expensive-subquery-limit 100
 expire-logs-days 0
@@ -1572,7 +1577,7 @@ old-style-user-limits FALSE
 optimizer-prune-level 1
 optimizer-search-depth 62
 optimizer-selectivity-sampling-limit 100
-optimizer-switch index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+optimizer-switch index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 optimizer-trace 
 optimizer-trace-max-mem-size 1048576
 optimizer-use-condition-selectivity 4
diff --git a/mysql-test/main/range.result b/mysql-test/main/range.result
index bcb43d1e9054bcce583d4f948f1ceee54493b32e..9a2d99e2f82547a2308adb2de95036694ad6b364 100644
--- a/mysql-test/main/range.result
+++ b/mysql-test/main/range.result
@@ -3100,6 +3100,26 @@ a	b
 set eq_range_index_dive_limit=default;
 drop table t1;
 #
+# MDEV-18551: New defaults for eq_range_index_dive_limit
+#
+create table ten(a int);
+insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int, key(a));
+insert into t1 select A.a + B.a*10 + C.a*100 from ten A, ten B,ten C;
+insert into t1 select 1 from ten A, ten B,ten C;
+create table t2(a int, key(a));
+insert into t2 select A.a + B.a*10 + C.a*100 from ten A, ten B,ten C where A.a + B.a*10 + C.a*100 < 199;
+# expected type=range, rows=1487 , reason=using index dives
+analyze SELECT * FROM t1 where a in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	r_rows	filtered	r_filtered	Extra
+1	SIMPLE	t1	range	a	a	5	NULL	1487	1199.00	100.00	100.00	Using where; Using index
+insert into t2 values (200),(201);
+# expected type=range, rows=201 , reason=using index statistics
+analyze SELECT * FROM t1 where a in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,200,201);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	r_rows	filtered	r_filtered	Extra
+1	SIMPLE	t1	range	a	a	5	NULL	201	1201.00	100.00	100.00	Using where; Using index
+drop table t1,ten,t2;
+#
 # End of 10.2 tests
 #
 set global innodb_stats_persistent= @innodb_stats_persistent_save;
diff --git a/mysql-test/main/range.test b/mysql-test/main/range.test
index 9edb3f39ae39351dbd846b06c2fcf72fe52c94cf..76d893f56b7e0f031ee157de3c6437a08ce4ef40 100644
--- a/mysql-test/main/range.test
+++ b/mysql-test/main/range.test
@@ -2074,6 +2074,29 @@ set eq_range_index_dive_limit=default;
 
 drop table t1;
 
+--echo #
+--echo # MDEV-18551: New defaults for eq_range_index_dive_limit
+--echo #
+
+create table ten(a int);
+insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int, key(a));
+insert into t1 select A.a + B.a*10 + C.a*100 from ten A, ten B,ten C;
+insert into t1 select 1 from ten A, ten B,ten C;
+
+create table t2(a int, key(a));
+insert into t2 select A.a + B.a*10 + C.a*100 from ten A, ten B,ten C where A.a + B.a*10 + C.a*100 < 199;
+
+let $a= `select group_concat(a) from t2`;
+
+--echo # expected type=range, rows=1487 , reason=using index dives
+eval analyze SELECT * FROM t1 where a in ($a);
+insert into t2 values (200),(201);
+let $a= `select group_concat(a) from t2`;
+--echo # expected type=range, rows=201 , reason=using index statistics
+eval analyze SELECT * FROM t1 where a in ($a);
+drop table t1,ten,t2;
+
 --echo #
 --echo # End of 10.2 tests
 --echo #
diff --git a/mysql-test/main/range_mrr_icp.result b/mysql-test/main/range_mrr_icp.result
index 5d55a8206736d4a7cfe884c341fb4e0fb44196d8..5cda4111b6d3a3614ec0ef551e4b7664b757c4f1 100644
--- a/mysql-test/main/range_mrr_icp.result
+++ b/mysql-test/main/range_mrr_icp.result
@@ -3097,6 +3097,26 @@ a	b
 set eq_range_index_dive_limit=default;
 drop table t1;
 #
+# MDEV-18551: New defaults for eq_range_index_dive_limit
+#
+create table ten(a int);
+insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int, key(a));
+insert into t1 select A.a + B.a*10 + C.a*100 from ten A, ten B,ten C;
+insert into t1 select 1 from ten A, ten B,ten C;
+create table t2(a int, key(a));
+insert into t2 select A.a + B.a*10 + C.a*100 from ten A, ten B,ten C where A.a + B.a*10 + C.a*100 < 199;
+# expected type=range, rows=1487 , reason=using index dives
+analyze SELECT * FROM t1 where a in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	r_rows	filtered	r_filtered	Extra
+1	SIMPLE	t1	range	a	a	5	NULL	1487	1199.00	100.00	100.00	Using where; Using index
+insert into t2 values (200),(201);
+# expected type=range, rows=201 , reason=using index statistics
+analyze SELECT * FROM t1 where a in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,200,201);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	r_rows	filtered	r_filtered	Extra
+1	SIMPLE	t1	range	a	a	5	NULL	201	1201.00	100.00	100.00	Using where; Using index
+drop table t1,ten,t2;
+#
 # End of 10.2 tests
 #
 set global innodb_stats_persistent= @innodb_stats_persistent_save;
diff --git a/mysql-test/main/range_vs_index_merge.result b/mysql-test/main/range_vs_index_merge.result
index 82d2358cebcc35edc43b8332e54f68c3f65a3a21..efbf41d6372d0b5176d3492da247a78b574345fe 100644
--- a/mysql-test/main/range_vs_index_merge.result
+++ b/mysql-test/main/range_vs_index_merge.result
@@ -1079,7 +1079,7 @@ EXPLAIN SELECT Name, Country, Population FROM City WHERE
 (Name='Samara' AND Country='RUS') OR  
 (Name='Seattle' AND Country='USA');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CityName	35	NULL	27	Using index condition; Using where
+1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CountryName	38	NULL	28	Using index condition; Using where
 SELECT Name, Country, Population FROM City WHERE
 (Name='Manila' AND Country='PHL') OR
 (Name='Addis Abeba' AND Country='ETH') OR          
@@ -1109,33 +1109,33 @@ SELECT Name, Country, Population FROM City WHERE
 (Name='Samara' AND Country='RUS') OR  
 (Name='Seattle' AND Country='USA');
 Name	Country	Population
-Addis Abeba	ETH	2495000
-Ankara	TUR	3038159
-Bangalore	IND	2660088
+Toronto	CAN	688275
+Vancouver	CAN	514008
 Basel	CHE	166700
-Caracas	VEN	1975294
-Dakar	SEN	785071
-Delhi	IND	7206704
+Peking	CHN	7472000
+Praha	CZE	1181126
 Dresden	DEU	476668
+Addis Abeba	ETH	2495000
+Paris	FRA	2125246
 Jakarta	IDN	9604900
+Bangalore	IND	2660088
+Delhi	IND	7206704
+Teheran	IRN	6758845
+Roma	ITA	2643581
+Venezia	ITA	277305
+Tokyo	JPN	7980230
+Seoul	KOR	9981619
 Kaunas	LTU	412639
+Rabat	MAR	623457
+Tijuana	MEX	1212232
 Lagos	NGA	1518000
-Lugansk	UKR	469000
 Manila	PHL	1581082
-Paris	FRA	2125246
-Peking	CHN	7472000
-Praha	CZE	1181126
-Rabat	MAR	623457
-Roma	ITA	2643581
 Samara	RUS	1156100
+Dakar	SEN	785071
+Ankara	TUR	3038159
+Lugansk	UKR	469000
 Seattle	USA	563374
-Seoul	KOR	9981619
-Teheran	IRN	6758845
-Tijuana	MEX	1212232
-Tokyo	JPN	7980230
-Toronto	CAN	688275
-Vancouver	CAN	514008
-Venezia	ITA	277305
+Caracas	VEN	1975294
 set optimizer_switch='index_merge=off';
 EXPLAIN SELECT Name, Country, Population FROM City WHERE
 (Name='Manila' AND Country='PHL') OR
@@ -1166,7 +1166,7 @@ EXPLAIN SELECT Name, Country, Population FROM City WHERE
 (Name='Samara' AND Country='RUS') OR  
 (Name='Seattle' AND Country='USA');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CityName	35	NULL	27	Using index condition; Using where
+1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CountryName	38	NULL	28	Using index condition; Using where
 SELECT Name, Country, Population FROM City WHERE
 (Name='Manila' AND Country='PHL') OR
 (Name='Addis Abeba' AND Country='ETH') OR          
@@ -1196,33 +1196,33 @@ SELECT Name, Country, Population FROM City WHERE
 (Name='Samara' AND Country='RUS') OR  
 (Name='Seattle' AND Country='USA');
 Name	Country	Population
-Addis Abeba	ETH	2495000
-Ankara	TUR	3038159
-Bangalore	IND	2660088
+Toronto	CAN	688275
+Vancouver	CAN	514008
 Basel	CHE	166700
-Caracas	VEN	1975294
-Dakar	SEN	785071
-Delhi	IND	7206704
+Peking	CHN	7472000
+Praha	CZE	1181126
 Dresden	DEU	476668
+Addis Abeba	ETH	2495000
+Paris	FRA	2125246
 Jakarta	IDN	9604900
+Bangalore	IND	2660088
+Delhi	IND	7206704
+Teheran	IRN	6758845
+Roma	ITA	2643581
+Venezia	ITA	277305
+Tokyo	JPN	7980230
+Seoul	KOR	9981619
 Kaunas	LTU	412639
+Rabat	MAR	623457
+Tijuana	MEX	1212232
 Lagos	NGA	1518000
-Lugansk	UKR	469000
 Manila	PHL	1581082
-Paris	FRA	2125246
-Peking	CHN	7472000
-Praha	CZE	1181126
-Rabat	MAR	623457
-Roma	ITA	2643581
 Samara	RUS	1156100
+Dakar	SEN	785071
+Ankara	TUR	3038159
+Lugansk	UKR	469000
 Seattle	USA	563374
-Seoul	KOR	9981619
-Teheran	IRN	6758845
-Tijuana	MEX	1212232
-Tokyo	JPN	7980230
-Toronto	CAN	688275
-Vancouver	CAN	514008
-Venezia	ITA	277305
+Caracas	VEN	1975294
 set optimizer_switch=@save_optimizer_switch;
 # 
 # Bug mdev-585: range vs index-merge with ORDER BY ... LIMIT n
@@ -1900,23 +1900,23 @@ Country='NOR' AND Name IN ('Oslo', 'Bergen') OR
 Country='ITA' AND Name IN ('Napoli', 'Venezia');
 ID	Name	Country	Population
 175	Antwerpen	BEL	446525
-2808	Bergen	NOR	230948
+176	Gent	BEL	224180
 3068	Berlin	DEU	3386667
 3087	Bonn	DEU	301048
-2918	Braga	PRT	90535
-176	Gent	BEL	224180
 3242	Lahti	FIN	96921
-3580	Moscow	RUS	8389200
+2974	Paris	FRA	2125246
 1466	Napoli	ITA	1002619
+1474	Venezia	ITA	277305
+2808	Bergen	NOR	230948
 2807	Oslo	NOR	508726
-2974	Paris	FRA	2125246
+2928	Warszawa	POL	1615369
+2931	Wroclaw	POL	636765
+2918	Braga	PRT	90535
 2915	Porto	PRT	273060
+3580	Moscow	RUS	8389200
 3581	St Petersburg	RUS	4694000
 3048	Stockholm	SWE	750348
 3051	Uppsala	SWE	189569
-1474	Venezia	ITA	277305
-2928	Warszawa	POL	1615369
-2931	Wroclaw	POL	636765
 explain select * from City
 where
 Country='FIN' AND Name IN ('Lahti','Imatra') OR
@@ -1930,6 +1930,6 @@ Country='POL' AND Name IN ('Warszawa', 'Wroclaw') OR
 Country='NOR' AND Name IN ('Oslo', 'Bergen') OR
 Country='ITA' AND Name IN ('Napoli', 'Venezia');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	City	range	CountryName,Name	Name	35	NULL	20	Using index condition; Using where
+1	SIMPLE	City	range	CountryName,Name	CountryName	38	NULL	20	Using index condition; Using where
 DROP DATABASE world;
 set session optimizer_switch='index_merge_sort_intersection=default';
diff --git a/mysql-test/main/range_vs_index_merge_innodb.result b/mysql-test/main/range_vs_index_merge_innodb.result
index 6b6b77232e254cbef54d983e64ad7318fedfccb6..03b43431fe782f831177c1245fdeb2cd88475274 100644
--- a/mysql-test/main/range_vs_index_merge_innodb.result
+++ b/mysql-test/main/range_vs_index_merge_innodb.result
@@ -1085,7 +1085,7 @@ EXPLAIN SELECT Name, Country, Population FROM City WHERE
 (Name='Samara' AND Country='RUS') OR  
 (Name='Seattle' AND Country='USA');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CityName	35	NULL	27	Using index condition; Using where
+1	SIMPLE	City	index_merge	Country,CountryPopulation,CountryName,CityName	CountryName,CityName	38,35	NULL	27	Using sort_union(CountryName,CityName); Using where
 SELECT Name, Country, Population FROM City WHERE
 (Name='Manila' AND Country='PHL') OR
 (Name='Addis Abeba' AND Country='ETH') OR          
@@ -1116,32 +1116,32 @@ SELECT Name, Country, Population FROM City WHERE
 (Name='Seattle' AND Country='USA');
 Name	Country	Population
 Addis Abeba	ETH	2495000
-Ankara	TUR	3038159
-Bangalore	IND	2660088
-Basel	CHE	166700
-Caracas	VEN	1975294
-Dakar	SEN	785071
-Delhi	IND	7206704
-Dresden	DEU	476668
+Manila	PHL	1581082
 Jakarta	IDN	9604900
+Delhi	IND	7206704
+Bangalore	IND	2660088
+Teheran	IRN	6758845
+Roma	ITA	2643581
+Venezia	ITA	277305
+Tokyo	JPN	7980230
+Toronto	CAN	688275
+Vancouver	CAN	514008
+Peking	CHN	7472000
+Seoul	KOR	9981619
 Kaunas	LTU	412639
+Rabat	MAR	623457
+Tijuana	MEX	1212232
 Lagos	NGA	1518000
-Lugansk	UKR	469000
-Manila	PHL	1581082
 Paris	FRA	2125246
-Peking	CHN	7472000
+Dresden	DEU	476668
+Dakar	SEN	785071
+Basel	CHE	166700
 Praha	CZE	1181126
-Rabat	MAR	623457
-Roma	ITA	2643581
+Ankara	TUR	3038159
+Lugansk	UKR	469000
+Caracas	VEN	1975294
 Samara	RUS	1156100
 Seattle	USA	563374
-Seoul	KOR	9981619
-Teheran	IRN	6758845
-Tijuana	MEX	1212232
-Tokyo	JPN	7980230
-Toronto	CAN	688275
-Vancouver	CAN	514008
-Venezia	ITA	277305
 set optimizer_switch='index_merge=off';
 EXPLAIN SELECT Name, Country, Population FROM City WHERE
 (Name='Manila' AND Country='PHL') OR
@@ -1172,7 +1172,7 @@ EXPLAIN SELECT Name, Country, Population FROM City WHERE
 (Name='Samara' AND Country='RUS') OR  
 (Name='Seattle' AND Country='USA');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CityName	35	NULL	27	Using index condition; Using where
+1	SIMPLE	City	range	Country,CountryPopulation,CountryName,CityName	CityName	35	NULL	28	Using index condition; Using where
 SELECT Name, Country, Population FROM City WHERE
 (Name='Manila' AND Country='PHL') OR
 (Name='Addis Abeba' AND Country='ETH') OR          
@@ -1906,23 +1906,23 @@ Country='NOR' AND Name IN ('Oslo', 'Bergen') OR
 Country='ITA' AND Name IN ('Napoli', 'Venezia');
 ID	Name	Country	Population
 175	Antwerpen	BEL	446525
-2808	Bergen	NOR	230948
+176	Gent	BEL	224180
 3068	Berlin	DEU	3386667
 3087	Bonn	DEU	301048
-2918	Braga	PRT	90535
-176	Gent	BEL	224180
 3242	Lahti	FIN	96921
-3580	Moscow	RUS	8389200
+2974	Paris	FRA	2125246
 1466	Napoli	ITA	1002619
+1474	Venezia	ITA	277305
+2808	Bergen	NOR	230948
 2807	Oslo	NOR	508726
-2974	Paris	FRA	2125246
+2928	Warszawa	POL	1615369
+2931	Wroclaw	POL	636765
+2918	Braga	PRT	90535
 2915	Porto	PRT	273060
+3580	Moscow	RUS	8389200
 3581	St Petersburg	RUS	4694000
 3048	Stockholm	SWE	750348
 3051	Uppsala	SWE	189569
-1474	Venezia	ITA	277305
-2928	Warszawa	POL	1615369
-2931	Wroclaw	POL	636765
 explain select * from City
 where
 Country='FIN' AND Name IN ('Lahti','Imatra') OR
@@ -1936,7 +1936,7 @@ Country='POL' AND Name IN ('Warszawa', 'Wroclaw') OR
 Country='NOR' AND Name IN ('Oslo', 'Bergen') OR
 Country='ITA' AND Name IN ('Napoli', 'Venezia');
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	City	range	CountryName,Name	Name	35	NULL	20	Using index condition; Using where
+1	SIMPLE	City	range	CountryName,Name	CountryName	38	NULL	20	Using index condition; Using where
 DROP DATABASE world;
 set session optimizer_switch='index_merge_sort_intersection=default';
 set global innodb_stats_persistent= @innodb_stats_persistent_save;
diff --git a/mysql-test/main/statistics.result b/mysql-test/main/statistics.result
index 5f6a7cd7bb3b01ed694e54cc97402d84e896632d..787f01945320e187523645ebbf03262ab760253f 100644
--- a/mysql-test/main/statistics.result
+++ b/mysql-test/main/statistics.result
@@ -1760,3 +1760,110 @@ DROP TABLE t1;
 # End of 10.2 tests
 #
 set histogram_size=@save_hist_size, histogram_type=@save_hist_type;
+#
+# Start of 10.4 tests
+#
+#
+# Test analyze_sample_percentage system variable.
+#
+set @save_use_stat_tables=@@use_stat_tables;
+set @save_analyze_sample_percentage=@@analyze_sample_percentage;
+set @save_hist_size=@@histogram_size;
+set session rand_seed1=42;
+set session rand_seed2=62;
+set use_stat_tables=PREFERABLY;
+set histogram_size=10;
+CREATE TABLE t1 (id int);
+INSERT INTO t1 (id) VALUES (1), (1), (1), (1), (1), (1), (1);
+INSERT INTO t1 (id) SELECT id FROM t1;
+INSERT INTO t1 SELECT id+1 FROM t1;
+INSERT INTO t1 SELECT id+2 FROM t1;
+INSERT INTO t1 SELECT id+4 FROM t1;
+INSERT INTO t1 SELECT id+8 FROM t1;
+INSERT INTO t1 SELECT id+16 FROM t1;
+INSERT INTO t1 SELECT id+32 FROM t1;
+INSERT INTO t1 SELECT id+64 FROM t1;
+INSERT INTO t1 SELECT id+128 FROM t1;
+INSERT INTO t1 SELECT id+256 FROM t1;
+INSERT INTO t1 SELECT id+512 FROM t1;
+INSERT INTO t1 SELECT id+1024 FROM t1;
+INSERT INTO t1 SELECT id+2048 FROM t1;
+INSERT INTO t1 SELECT id+4096 FROM t1;
+INSERT INTO t1 SELECT id+9192 FROM t1;
+#
+# This query will should show a full table scan analysis.
+#
+ANALYZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	OK
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+table_name	column_name	min_value	max_value	nulls_ratio	avg_length	avg_frequency	DECODE_HISTOGRAM(hist_type, histogram)
+t1	id	1	17384	0.0000	4.0000	14.0000	0.15705,0.15711,0.21463,0.15705,0.15711,0.15706
+set analyze_sample_percentage=0.1;
+#
+# This query will show an innacurate avg_frequency value.
+#
+ANALYZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	Table is already up to date
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+table_name	column_name	min_value	max_value	nulls_ratio	avg_length	avg_frequency	DECODE_HISTOGRAM(hist_type, histogram)
+t1	id	111	17026	0.0000	4.0000	10.4739	0.13649,0.14922,0.16921,0.21141,0.18355,0.15012
+#
+# This query will show a better avg_frequency value.
+#
+set analyze_sample_percentage=25;
+ANALYZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	Table is already up to date
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+table_name	column_name	min_value	max_value	nulls_ratio	avg_length	avg_frequency	DECODE_HISTOGRAM(hist_type, histogram)
+t1	id	1	17384	0.0000	4.0000	14.0401	0.15566,0.15590,0.15729,0.21538,0.15790,0.15787
+set analyze_sample_percentage=0;
+#
+# Test self adjusting sampling level.
+#
+ANALYZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	Table is already up to date
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+table_name	column_name	min_value	max_value	nulls_ratio	avg_length	avg_frequency	DECODE_HISTOGRAM(hist_type, histogram)
+t1	id	1	17384	0.0000	4.0000	13.9812	0.15860,0.15767,0.21515,0.15573,0.15630,0.15654
+#
+# Test record estimation is working properly.
+#
+select count(*) from t1;
+count(*)
+229376
+explain select * from t1;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	229060	
+set analyze_sample_percentage=100;
+ANALYZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	analyze	status	Engine-independent statistics collected
+test.t1	analyze	status	Table is already up to date
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+table_name	column_name	min_value	max_value	nulls_ratio	avg_length	avg_frequency	DECODE_HISTOGRAM(hist_type, histogram)
+t1	id	1	17384	0.0000	4.0000	14.0000	0.15705,0.15711,0.21463,0.15705,0.15711,0.15706
+explain select * from t1;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	229376	
+drop table t1;
+set analyze_sample_percentage=@save_analyze_sample_percentage;
+set histogram_size=@save_hist_size;
+set use_stat_tables=@save_use_stat_tables;
diff --git a/mysql-test/main/statistics.test b/mysql-test/main/statistics.test
index 78439e3e68af7b70364f0f4cdefc1a331fa98e28..880f2987b50a6e08d1e14668981093d3f8539424 100644
--- a/mysql-test/main/statistics.test
+++ b/mysql-test/main/statistics.test
@@ -901,3 +901,94 @@ DROP TABLE t1;
 --echo #
 set histogram_size=@save_hist_size, histogram_type=@save_hist_type;
 
+
+
+--echo #
+--echo # Start of 10.4 tests
+--echo #
+
+--echo #
+--echo # Test analyze_sample_percentage system variable.
+--echo #
+set @save_use_stat_tables=@@use_stat_tables;
+set @save_analyze_sample_percentage=@@analyze_sample_percentage;
+set @save_hist_size=@@histogram_size;
+
+set session rand_seed1=42;
+set session rand_seed2=62;
+
+set use_stat_tables=PREFERABLY;
+set histogram_size=10;
+
+CREATE TABLE t1 (id int);
+INSERT INTO t1 (id) VALUES (1), (1), (1), (1), (1), (1), (1);
+INSERT INTO t1 (id) SELECT id FROM t1;
+INSERT INTO t1 SELECT id+1 FROM t1;
+INSERT INTO t1 SELECT id+2 FROM t1;
+INSERT INTO t1 SELECT id+4 FROM t1;
+INSERT INTO t1 SELECT id+8 FROM t1;
+INSERT INTO t1 SELECT id+16 FROM t1;
+INSERT INTO t1 SELECT id+32 FROM t1;
+INSERT INTO t1 SELECT id+64 FROM t1;
+INSERT INTO t1 SELECT id+128 FROM t1;
+INSERT INTO t1 SELECT id+256 FROM t1;
+INSERT INTO t1 SELECT id+512 FROM t1;
+INSERT INTO t1 SELECT id+1024 FROM t1;
+INSERT INTO t1 SELECT id+2048 FROM t1;
+INSERT INTO t1 SELECT id+4096 FROM t1;
+INSERT INTO t1 SELECT id+9192 FROM t1;
+
+--echo #
+--echo # This query will should show a full table scan analysis.
+--echo #
+ANALYZE TABLE t1;
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+       DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+
+set analyze_sample_percentage=0.1;
+
+--echo #
+--echo # This query will show an innacurate avg_frequency value.
+--echo #
+ANALYZE TABLE t1;
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+       DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+
+--echo #
+--echo # This query will show a better avg_frequency value.
+--echo #
+set analyze_sample_percentage=25;
+ANALYZE TABLE t1;
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+       DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+
+
+set analyze_sample_percentage=0;
+--echo #
+--echo # Test self adjusting sampling level.
+--echo #
+ANALYZE TABLE t1;
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+       DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+-- echo #
+-- echo # Test record estimation is working properly.
+-- echo #
+select count(*) from t1;
+explain select * from t1;
+
+set analyze_sample_percentage=100;
+ANALYZE TABLE t1;
+select table_name, column_name, min_value, max_value, nulls_ratio, avg_length, avg_frequency,
+       DECODE_HISTOGRAM(hist_type, histogram)
+from mysql.column_stats;
+explain select * from t1;
+
+
+drop table t1;
+set analyze_sample_percentage=@save_analyze_sample_percentage;
+set histogram_size=@save_hist_size;
+set use_stat_tables=@save_use_stat_tables;
diff --git a/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result b/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result
index c6b782edaf95d78b251de241f1de08d99581b34a..74f4fd1ee07d56990062984848afc2a36958701f 100644
--- a/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result
+++ b/mysql-test/suite/sys_vars/r/optimizer_switch_basic.result
@@ -1,25 +1,25 @@
 SET @start_global_value = @@global.optimizer_switch;
 SELECT @start_global_value;
 @start_global_value
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 select @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 select @@session.optimizer_switch;
 @@session.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 show global variables like 'optimizer_switch';
 Variable_name	Value
-optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 show session variables like 'optimizer_switch';
 Variable_name	Value
-optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+optimizer_switch	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 select * from information_schema.global_variables where variable_name='optimizer_switch';
 VARIABLE_NAME	VARIABLE_VALUE
-OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 select * from information_schema.session_variables where variable_name='optimizer_switch';
 VARIABLE_NAME	VARIABLE_VALUE
-OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+OPTIMIZER_SWITCH	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 set global optimizer_switch=10;
 set session optimizer_switch=5;
 select @@global.optimizer_switch;
@@ -69,4 +69,4 @@ ERROR 42000: Variable 'optimizer_switch' can't be set to the value of 'foobar'
 SET @@global.optimizer_switch = @start_global_value;
 SELECT @@global.optimizer_switch;
 @@global.optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
diff --git a/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result b/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result
index 6428c4b72ce35b94d8babdde5c2a4bbe50684540..816adba7e59195f9c70b336200e6fe7f43fb2ee7 100644
--- a/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result
+++ b/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result
@@ -40,6 +40,20 @@ NUMERIC_BLOCK_SIZE	NULL
 ENUM_VALUE_LIST	DEFAULT,COPY,INPLACE,NOCOPY,INSTANT
 READ_ONLY	NO
 COMMAND_LINE_ARGUMENT	OPTIONAL
+VARIABLE_NAME	ANALYZE_SAMPLE_PERCENTAGE
+SESSION_VALUE	100.000000
+GLOBAL_VALUE	100.000000
+GLOBAL_VALUE_ORIGIN	COMPILE-TIME
+DEFAULT_VALUE	100.000000
+VARIABLE_SCOPE	SESSION
+VARIABLE_TYPE	DOUBLE
+VARIABLE_COMMENT	Percentage of rows from the table ANALYZE TABLE will sample to collect table statistics. Set to 0 to let MariaDB decide what percentage of rows to sample.
+NUMERIC_MIN_VALUE	0
+NUMERIC_MAX_VALUE	100
+NUMERIC_BLOCK_SIZE	NULL
+ENUM_VALUE_LIST	NULL
+READ_ONLY	NO
+COMMAND_LINE_ARGUMENT	REQUIRED
 VARIABLE_NAME	AUTOCOMMIT
 SESSION_VALUE	ON
 GLOBAL_VALUE	ON
@@ -881,10 +895,10 @@ ENUM_VALUE_LIST	NULL
 READ_ONLY	NO
 COMMAND_LINE_ARGUMENT	NULL
 VARIABLE_NAME	EQ_RANGE_INDEX_DIVE_LIMIT
-SESSION_VALUE	10
-GLOBAL_VALUE	10
+SESSION_VALUE	200
+GLOBAL_VALUE	200
 GLOBAL_VALUE_ORIGIN	COMPILE-TIME
-DEFAULT_VALUE	10
+DEFAULT_VALUE	200
 VARIABLE_SCOPE	SESSION
 VARIABLE_TYPE	INT UNSIGNED
 VARIABLE_COMMENT	The optimizer will use existing index statistics instead of doing index dives for equality ranges if the number of equality ranges for the index is larger than or equal to this number. If set to 0, index dives are always used.
@@ -2743,10 +2757,10 @@ ENUM_VALUE_LIST	NULL
 READ_ONLY	NO
 COMMAND_LINE_ARGUMENT	REQUIRED
 VARIABLE_NAME	OPTIMIZER_SWITCH
-SESSION_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
-GLOBAL_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+SESSION_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+GLOBAL_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 GLOBAL_VALUE_ORIGIN	COMPILE-TIME
-DEFAULT_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+DEFAULT_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 VARIABLE_SCOPE	SESSION
 VARIABLE_TYPE	FLAGSET
 VARIABLE_COMMENT	Fine-tune the optimizer behavior
diff --git a/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result b/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
index f9bc7d3a0287c1a493a3e001428a0eba67bf758f..ea0f569ab1b5849cb3b355cbbd3b780c9b1270ec 100644
--- a/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
+++ b/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
@@ -40,6 +40,20 @@ NUMERIC_BLOCK_SIZE	NULL
 ENUM_VALUE_LIST	DEFAULT,COPY,INPLACE,NOCOPY,INSTANT
 READ_ONLY	NO
 COMMAND_LINE_ARGUMENT	OPTIONAL
+VARIABLE_NAME	ANALYZE_SAMPLE_PERCENTAGE
+SESSION_VALUE	100.000000
+GLOBAL_VALUE	100.000000
+GLOBAL_VALUE_ORIGIN	COMPILE-TIME
+DEFAULT_VALUE	100.000000
+VARIABLE_SCOPE	SESSION
+VARIABLE_TYPE	DOUBLE
+VARIABLE_COMMENT	Percentage of rows from the table ANALYZE TABLE will sample to collect table statistics. Set to 0 to let MariaDB decide what percentage of rows to sample.
+NUMERIC_MIN_VALUE	0
+NUMERIC_MAX_VALUE	100
+NUMERIC_BLOCK_SIZE	NULL
+ENUM_VALUE_LIST	NULL
+READ_ONLY	NO
+COMMAND_LINE_ARGUMENT	REQUIRED
 VARIABLE_NAME	AUTOCOMMIT
 SESSION_VALUE	ON
 GLOBAL_VALUE	ON
@@ -895,10 +909,10 @@ ENUM_VALUE_LIST	NULL
 READ_ONLY	NO
 COMMAND_LINE_ARGUMENT	NULL
 VARIABLE_NAME	EQ_RANGE_INDEX_DIVE_LIMIT
-SESSION_VALUE	10
-GLOBAL_VALUE	10
+SESSION_VALUE	200
+GLOBAL_VALUE	200
 GLOBAL_VALUE_ORIGIN	COMPILE-TIME
-DEFAULT_VALUE	10
+DEFAULT_VALUE	200
 VARIABLE_SCOPE	SESSION
 VARIABLE_TYPE	INT UNSIGNED
 VARIABLE_COMMENT	The optimizer will use existing index statistics instead of doing index dives for equality ranges if the number of equality ranges for the index is larger than or equal to this number. If set to 0, index dives are always used.
@@ -2967,10 +2981,10 @@ ENUM_VALUE_LIST	NULL
 READ_ONLY	NO
 COMMAND_LINE_ARGUMENT	REQUIRED
 VARIABLE_NAME	OPTIMIZER_SWITCH
-SESSION_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
-GLOBAL_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+SESSION_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+GLOBAL_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 GLOBAL_VALUE_ORIGIN	COMPILE-TIME
-DEFAULT_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+DEFAULT_VALUE	index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 VARIABLE_SCOPE	SESSION
 VARIABLE_TYPE	FLAGSET
 VARIABLE_COMMENT	Fine-tune the optimizer behavior
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt
index 890b8ff8b8626156f310589a04907aa2a966ab77..ecca723b9e463f745449e55941e733f2df6d8f32 100644
--- a/sql/CMakeLists.txt
+++ b/sql/CMakeLists.txt
@@ -448,12 +448,6 @@ IF(WIN32 AND TARGET mysqld AND NOT CMAKE_CROSSCOMPILING)
     ALL
     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/initdb.dep
   )
-  INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/data DESTINATION . 
-  COMPONENT DataFiles 
-  PATTERN "initdb.dep" EXCLUDE  
-  PATTERN "bootstrap.sql" EXCLUDE
-  PATTERN "aria*" EXCLUDE
-  )
 ELSE()
   # Not windows or cross compiling, just install an empty directory
   INSTALL(FILES ${DUMMY_FILE} DESTINATION data/mysql COMPONENT DataFiles)
diff --git a/sql/mysql_install_db.cc b/sql/mysql_install_db.cc
index 718e49969e0984c9d1cb653ff90a2bcd6f5cbf53..93a3b54bdc21848c86d58ccc04d3420dd5f96165 100644
--- a/sql/mysql_install_db.cc
+++ b/sql/mysql_install_db.cc
@@ -39,7 +39,8 @@ struct IUnknown;
 
 extern "C" const char* mysql_bootstrap_sql[];
 
-char default_os_user[]= "NT AUTHORITY\\NetworkService";
+static char default_os_user[]= "NT AUTHORITY\\NetworkService";
+static char default_datadir[MAX_PATH];
 static int create_db_instance();
 static uint opt_silent;
 static char datadir_buffer[FN_REFLEN];
@@ -169,8 +170,27 @@ int main(int argc, char **argv)
     exit(error);
   if (!opt_datadir)
   {
-    my_print_help(my_long_options);
-    die("parameter --datadir=# is mandatory");
+    /*
+      Figure out default data directory. It "data" directory, next to "bin" directory, where
+      mysql_install_db.exe resides.
+    */
+    strcpy(default_datadir, self_name);
+    p = strrchr(default_datadir, FN_LIBCHAR);
+    if (p)
+    {
+      *p= 0;
+      p= strrchr(default_datadir, FN_LIBCHAR);
+      if (p)
+        *p= 0;
+    }
+    if (!p)
+    {
+      die("--datadir option not provided, and default datadir not found");
+      my_print_help(my_long_options);
+    }
+    strncat(default_datadir, "\\data", sizeof(default_datadir));
+    opt_datadir= default_datadir;
+    printf("Default data directory is %s\n",opt_datadir);
   }
 
   /* Print some help on errors */
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 76a952becee9e3955074c99db5fa6101f7bb0332..7ead7363bb17351bea703f96c1203f3c4092897c 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -622,6 +622,7 @@ typedef struct system_variables
   ulong optimizer_selectivity_sampling_limit;
   ulong optimizer_use_condition_selectivity;
   ulong use_stat_tables;
+  double sample_percentage;
   ulong histogram_size;
   ulong histogram_type;
   ulong preload_buff_size;
diff --git a/sql/sql_priv.h b/sql/sql_priv.h
index 0f28f5b82e146f7356dfe7254066d8b7b8e52f68..3744dfce7707cb7e4c45c9c82b224c79a8dae174 100644
--- a/sql/sql_priv.h
+++ b/sql/sql_priv.h
@@ -262,9 +262,10 @@
                                     OPTIMIZER_SWITCH_ORDERBY_EQ_PROP | \
                                     OPTIMIZER_SWITCH_COND_PUSHDOWN_FOR_DERIVED | \
                                     OPTIMIZER_SWITCH_SPLIT_MATERIALIZED | \
-                                    OPTIMIZER_SWITCH_COND_PUSHDOWN_FOR_SUBQUERY |\
+                                    OPTIMIZER_SWITCH_COND_PUSHDOWN_FOR_SUBQUERY | \
                                     OPTIMIZER_SWITCH_USE_ROWID_FILTER | \
-                                    OPTIMIZER_SWITCH_COND_PUSHDOWN_FROM_HAVING)
+                                    OPTIMIZER_SWITCH_COND_PUSHDOWN_FROM_HAVING | \
+                                    OPTIMIZER_SWITCH_OPTIMIZE_JOIN_BUFFER_SIZE)
 
 /*
   Replication uses 8 bytes to store SQL_MODE in the binary log. The day you
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index db214a1fe280533be8e92a01371e3be1aa50491f..8683368e818d858d67a79857094ade7e46cf3c2f 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -324,8 +324,8 @@ class Column_statistics_collected :public Column_statistics
 public:
 
   inline void init(THD *thd, Field * table_field);
-  inline bool add(ha_rows rowno);
-  inline void finish(ha_rows rows); 
+  inline bool add();
+  inline void finish(ha_rows rows, double sample_fraction);
   inline void cleanup();
 };
 
@@ -1540,6 +1540,8 @@ class Histogram_builder
   uint curr_bucket;        /* number of the current bucket to be built     */
   ulonglong count;         /* number of values retrieved                   */
   ulonglong count_distinct;    /* number of distinct values retrieved      */
+  /* number of distinct values that occured only once  */
+  ulonglong count_distinct_single_occurence;
 
 public: 
   Histogram_builder(Field *col, uint col_len, ha_rows rows)
@@ -1553,14 +1555,21 @@ class Histogram_builder
     bucket_capacity= (double) records / (hist_width + 1);
     curr_bucket= 0;
     count= 0;
-    count_distinct= 0;    
+    count_distinct= 0;
+    count_distinct_single_occurence= 0;
   }
 
-  ulonglong get_count_distinct() { return count_distinct; }
+  ulonglong get_count_distinct() const { return count_distinct; }
+  ulonglong get_count_single_occurence() const
+  {
+    return count_distinct_single_occurence;
+  }
 
   int next(void *elem, element_count elem_cnt)
   {
     count_distinct++;
+    if (elem_cnt == 1)
+      count_distinct_single_occurence++;
     count+= elem_cnt;
     if (curr_bucket == hist_width)
       return 0;
@@ -1574,7 +1583,7 @@ class Histogram_builder
              count > bucket_capacity * (curr_bucket + 1))
       {
         histogram->set_prev_value(curr_bucket);
-	curr_bucket++;
+        curr_bucket++;
       }
     }
     return 0;
@@ -1590,9 +1599,18 @@ int histogram_build_walk(void *elem, element_count elem_cnt, void *arg)
   return hist_builder->next(elem, elem_cnt);
 }
 
-C_MODE_END
 
 
+static int count_distinct_single_occurence_walk(void *elem,
+                                                element_count count, void *arg)
+{
+  ((ulonglong*)arg)[0]+= 1;
+  if (count == 1)
+    ((ulonglong*)arg)[1]+= 1;
+  return 0;
+}
+
+C_MODE_END
 /*
   The class Count_distinct_field is a helper class used to calculate
   the number of distinct values for a column. The class employs the
@@ -1611,6 +1629,9 @@ class Count_distinct_field: public Sql_alloc
   Unique *tree;       /* The helper object to contain distinct values */
   uint tree_key_length; /* The length of the keys for the elements of 'tree */
 
+  ulonglong distincts;
+  ulonglong distincts_single_occurence;
+
 public:
   
   Count_distinct_field() {}
@@ -1662,30 +1683,40 @@ class Count_distinct_field: public Sql_alloc
   {
     return tree->unique_add(table_field->ptr);
   }
-  
+
   /*
     @brief
     Calculate the number of elements accumulated in the container of 'tree'
   */
-  ulonglong get_value()
-  {
-    ulonglong count;
-    if (tree->elements == 0)
-      return (ulonglong) tree->elements_in_tree();
-    count= 0;  
-    tree->walk(table_field->table, count_distinct_walk, (void*) &count);
-    return count;
+  void walk_tree()
+  {
+    ulonglong counts[2] = {0, 0};
+    tree->walk(table_field->table,
+               count_distinct_single_occurence_walk, counts);
+    distincts= counts[0];
+    distincts_single_occurence= counts[1];
   }
 
   /*
     @brief
-    Build the histogram for the elements accumulated in the container of 'tree'
+    Calculate a histogram of the tree
   */
-  ulonglong get_value_with_histogram(ha_rows rows)
+   void walk_tree_with_histogram(ha_rows rows)
   {
     Histogram_builder hist_builder(table_field, tree_key_length, rows);
     tree->walk(table_field->table,  histogram_build_walk, (void *) &hist_builder);
-    return hist_builder.get_count_distinct();
+    distincts= hist_builder.get_count_distinct();
+    distincts_single_occurence= hist_builder.get_count_single_occurence();
+  }
+
+  ulonglong get_count_distinct()
+  {
+    return distincts;
+  }
+
+  ulonglong get_count_distinct_single_occurence()
+  {
+    return distincts_single_occurence;
   }
 
   /*
@@ -2483,7 +2514,7 @@ void Column_statistics_collected::init(THD *thd, Field *table_field)
 */
 
 inline
-bool Column_statistics_collected::add(ha_rows rowno)
+bool Column_statistics_collected::add()
 {
 
   bool err= 0;
@@ -2492,9 +2523,11 @@ bool Column_statistics_collected::add(ha_rows rowno)
   else
   {
     column_total_length+= column->value_length();
-    if (min_value && column->update_min(min_value, rowno == nulls))
+    if (min_value && column->update_min(min_value,
+                                        is_null(COLUMN_STAT_MIN_VALUE)))
       set_not_null(COLUMN_STAT_MIN_VALUE);
-    if (max_value && column->update_max(max_value, rowno == nulls))
+    if (max_value && column->update_max(max_value,
+                                        is_null(COLUMN_STAT_MAX_VALUE)))
       set_not_null(COLUMN_STAT_MAX_VALUE);
     if (count_distinct)
       err= count_distinct->add();
@@ -2512,7 +2545,7 @@ bool Column_statistics_collected::add(ha_rows rowno)
 */
 
 inline
-void Column_statistics_collected::finish(ha_rows rows)
+void Column_statistics_collected::finish(ha_rows rows, double sample_fraction)
 {
   double val;
 
@@ -2530,16 +2563,44 @@ void Column_statistics_collected::finish(ha_rows rows)
   }
   if (count_distinct)
   {
-    ulonglong distincts;
     uint hist_size= count_distinct->get_hist_size();
+
+    /* Compute cardinality statistics and optionally histogram. */
     if (hist_size == 0)
-      distincts= count_distinct->get_value();
+      count_distinct->walk_tree();
     else
-      distincts= count_distinct->get_value_with_histogram(rows - nulls);
+      count_distinct->walk_tree_with_histogram(rows - nulls);
+
+    ulonglong distincts= count_distinct->get_count_distinct();
+    ulonglong distincts_single_occurence=
+      count_distinct->get_count_distinct_single_occurence();
+
     if (distincts)
     {
-      val= (double) (rows - nulls) / distincts;
-      set_avg_frequency(val); 
+      /*
+       We use the unsmoothed first-order jackknife estimator" to estimate
+       the number of distinct values.
+       With a sufficient large percentage of rows sampled (80%), we revert back
+       to computing the avg_frequency off of the raw data.
+      */
+      if (sample_fraction > 0.8)
+        val= (double) (rows - nulls) / distincts;
+      else
+      {
+        if (nulls == 1)
+          distincts_single_occurence+= 1;
+        if (nulls)
+          distincts+= 1;
+        double fraction_single_occurence=
+          static_cast<double>(distincts_single_occurence) / rows;
+        double total_number_of_rows= rows / sample_fraction;
+        double estimate_total_distincts= total_number_of_rows /
+                (distincts /
+                 (1.0 - (1.0 - sample_fraction) * fraction_single_occurence));
+        val = std::fmax(estimate_total_distincts * (rows - nulls) / rows, 1.0);
+      }
+
+      set_avg_frequency(val);
       set_not_null(COLUMN_STAT_AVG_FREQUENCY);
     }
     else
@@ -2727,12 +2788,28 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
   Field *table_field;
   ha_rows rows= 0;
   handler *file=table->file;
+  double sample_fraction= thd->variables.sample_percentage / 100;
+  const ha_rows MIN_THRESHOLD_FOR_SAMPLING= 50000;
 
   DBUG_ENTER("collect_statistics_for_table");
 
   table->collected_stats->cardinality_is_null= TRUE;
   table->collected_stats->cardinality= 0;
 
+  if (thd->variables.sample_percentage == 0)
+  {
+    if (file->records() < MIN_THRESHOLD_FOR_SAMPLING)
+    {
+      sample_fraction= 1;
+    }
+    else
+    {
+      sample_fraction= std::fmin(
+                  (MIN_THRESHOLD_FOR_SAMPLING + 4096 *
+                   log(200 * file->records())) / file->records(), 1);
+    }
+  }
+
   for (field_ptr= table->field; *field_ptr; field_ptr++)
   {
     table_field= *field_ptr;   
@@ -2745,7 +2822,7 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
 
   /* Perform a full table scan to collect statistics on 'table's columns */
   if (!(rc= file->ha_rnd_init(TRUE)))
-  {  
+  {
     DEBUG_SYNC(table->in_use, "statistics_collection_start");
 
     while ((rc= file->ha_rnd_next(table->record[0])) != HA_ERR_END_OF_FILE)
@@ -2756,17 +2833,20 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
       if (rc)
         break;
 
-      for (field_ptr= table->field; *field_ptr; field_ptr++)
+      if (thd_rnd(thd) <= sample_fraction)
       {
-        table_field= *field_ptr;
-        if (!bitmap_is_set(table->read_set, table_field->field_index))
-          continue;  
-        if ((rc= table_field->collected_stats->add(rows)))
+        for (field_ptr= table->field; *field_ptr; field_ptr++)
+        {
+          table_field= *field_ptr;
+          if (!bitmap_is_set(table->read_set, table_field->field_index))
+            continue;
+          if ((rc= table_field->collected_stats->add()))
+            break;
+        }
+        if (rc)
           break;
+        rows++;
       }
-      if (rc)
-        break;
-      rows++;
     }
     file->ha_rnd_end();
   }
@@ -2780,7 +2860,8 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
   if (!rc)
   {
     table->collected_stats->cardinality_is_null= FALSE;
-    table->collected_stats->cardinality= rows;
+    table->collected_stats->cardinality=
+      static_cast<ha_rows>(rows / sample_fraction);
   }
 
   bitmap_clear_all(table->write_set);
@@ -2791,7 +2872,7 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
       continue;
     bitmap_set_bit(table->write_set, table_field->field_index); 
     if (!rc)
-      table_field->collected_stats->finish(rows);
+      table_field->collected_stats->finish(rows, sample_fraction);
     else
       table_field->collected_stats->cleanup();
   }
diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc
index 9826107b9968d3aea5b0ece2494108c102f23fe9..766e655b20a1b30cabe531f617044660ce263a4f 100644
--- a/sql/sys_vars.cc
+++ b/sql/sys_vars.cc
@@ -350,6 +350,15 @@ static Sys_var_long Sys_pfs_connect_attrs_size(
 
 #endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */
 
+static Sys_var_double Sys_analyze_sample_percentage(
+       "analyze_sample_percentage",
+       "Percentage of rows from the table ANALYZE TABLE will sample "
+       "to collect table statistics. Set to 0 to let MariaDB decide "
+       "what percentage of rows to sample.",
+       SESSION_VAR(sample_percentage),
+       CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 100),
+       DEFAULT(100));
+
 static Sys_var_ulong Sys_auto_increment_increment(
        "auto_increment_increment",
        "Auto-increment columns are incremented by this",
@@ -2724,7 +2733,7 @@ static Sys_var_uint Sys_eq_range_index_dive_limit(
        "ranges for the index is larger than or equal to this number. "
        "If set to 0, index dives are always used.",
        SESSION_VAR(eq_range_index_dive_limit), CMD_LINE(REQUIRED_ARG),
-       VALID_RANGE(0, UINT_MAX32), DEFAULT(10),
+       VALID_RANGE(0, UINT_MAX32), DEFAULT(200),
        BLOCK_SIZE(1));
 
 static Sys_var_ulong Sys_range_alloc_block_size(
diff --git a/storage/tokudb/mysql-test/tokudb/r/ext_key_1_innodb.result b/storage/tokudb/mysql-test/tokudb/r/ext_key_1_innodb.result
index b156f6620993e977378c561819b228b2a213cbd1..b24c2fa9ce2fc43897428f8d7e3a6d92415c73c2 100644
--- a/storage/tokudb/mysql-test/tokudb/r/ext_key_1_innodb.result
+++ b/storage/tokudb/mysql-test/tokudb/r/ext_key_1_innodb.result
@@ -1,7 +1,7 @@
 drop table if exists t;
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 create table t (id int not null, x int not null, y int not null, primary key(id), key(x)) engine=innodb;
 insert into t values (0,0,0),(1,1,1),(2,2,2),(3,2,3),(4,2,4);
 explain select x,id from t force index (x) where x=0 and id=0;
diff --git a/storage/tokudb/mysql-test/tokudb/r/ext_key_1_tokudb.result b/storage/tokudb/mysql-test/tokudb/r/ext_key_1_tokudb.result
index 0c0f8dbd76f02eb41ddfbfc85ce97d8defc7bf3e..8b7f4d8357cb25b98812533675e03294c743616f 100644
--- a/storage/tokudb/mysql-test/tokudb/r/ext_key_1_tokudb.result
+++ b/storage/tokudb/mysql-test/tokudb/r/ext_key_1_tokudb.result
@@ -1,7 +1,7 @@
 drop table if exists t;
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 create table t (id int not null, x int not null, y int not null, primary key(id), key(x)) engine=tokudb;
 insert into t values (0,0,0),(1,1,1),(2,2,2),(3,2,3),(4,2,4);
 explain select x,id from t force index (x) where x=0 and id=0;
diff --git a/storage/tokudb/mysql-test/tokudb/r/ext_key_2_innodb.result b/storage/tokudb/mysql-test/tokudb/r/ext_key_2_innodb.result
index 1193a27a5d893d8d1fdfdaeff3af417882ed0208..8e95644cdb764b2020b16fa43943e2184a88d16c 100644
--- a/storage/tokudb/mysql-test/tokudb/r/ext_key_2_innodb.result
+++ b/storage/tokudb/mysql-test/tokudb/r/ext_key_2_innodb.result
@@ -1,7 +1,7 @@
 drop table if exists t;
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 create table t (a int not null, b int not null, c int not null, d int not null, primary key(a,b), key(c,a)) engine=innodb;
 insert into t values (0,0,0,0),(0,1,0,1);
 explain select c,a,b from t where c=0 and a=0 and b=1;
diff --git a/storage/tokudb/mysql-test/tokudb/r/ext_key_2_tokudb.result b/storage/tokudb/mysql-test/tokudb/r/ext_key_2_tokudb.result
index 21284ed4514772895a3f82047c2b8298c6790fb8..740e78f3510fdbd291d358b8c6804217ab8e4486 100644
--- a/storage/tokudb/mysql-test/tokudb/r/ext_key_2_tokudb.result
+++ b/storage/tokudb/mysql-test/tokudb/r/ext_key_2_tokudb.result
@@ -1,7 +1,7 @@
 drop table if exists t;
 select @@optimizer_switch;
 @@optimizer_switch
-index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
+index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
 create table t (a int not null, b int not null, c int not null, d int not null, primary key(a,b), key(c,a)) engine=tokudb;
 insert into t values (0,0,0,0),(0,1,0,1);
 explain select c,a,b from t where c=0 and a=0 and b=1;