You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

967 lines
41 KiB

Updated optimizer costs in multi_range_read_info_const() and sql_select.cc - multi_range_read_info_const now uses the new records_in_range interface - Added handler::avg_io_cost() - Don't calculate avg_io_cost() in get_sweep_read_cost if avg_io_cost is not 1.0. In this case we trust the avg_io_cost() from the handler. - Changed test_quick_select to use TIME_FOR_COMPARE instead of TIME_FOR_COMPARE_IDX to align this with the rest of the code. - Fixed bug when using test_if_cheaper_ordering where we didn't use keyread if index was changed - Fixed a bug where we didn't use index only read when using order-by-index - Added keyread_time() to HEAP. The default keyread_time() was optimized for blocks and not suitable for HEAP. The effect was the HEAP prefered table scans over ranges for btree indexes. - Fixed get_sweep_read_cost() for HEAP tables - Ensure that range and ref have same cost for simple ranges Added a small cost (MULTI_RANGE_READ_SETUP_COST) to ranges to ensure we favior ref for range for simple queries. - Fixed that matching_candidates_in_table() uses same number of records as the rest of the optimizer - Added avg_io_cost() to JT_EQ_REF cost. This helps calculate the cost for HEAP and temporary tables better. A few tests changed because of this. - heap::read_time() and heap::keyread_time() adjusted to not add +1. This was to ensure that handler::keyread_time() doesn't give higher cost for heap tables than for normal tables. One effect of this is that heap and derived tables stored in heap will prefer key access as this is now regarded as cheap. - Changed cost for index read in sql_select.cc to match multi_range_read_info_const(). All index cost calculation is now done trough one function. - 'ref' will now use quick_cost for keys if it exists. This is done so that for '=' ranges, 'ref' is prefered over 'range'. - scan_time() now takes avg_io_costs() into account - get_delayed_table_estimates() uses block_size and avg_io_cost() - Removed default argument to test_if_order_by_key(); simplifies code
6 years ago
  1. SET SESSION DEFAULT_STORAGE_ENGINE='InnoDB';
  2. set @save_optimizer_switch_for_stat_tables_test=@@optimizer_switch;
  3. set optimizer_switch='extended_keys=on';
  4. set @innodb_stats_persistent_save= @@innodb_stats_persistent;
  5. set @innodb_stats_persistent_sample_pages_save=
  6. @@innodb_stats_persistent_sample_pages;
  7. set global innodb_stats_persistent= 1;
  8. set global innodb_stats_persistent_sample_pages=100;
  9. select @@global.use_stat_tables;
  10. @@global.use_stat_tables
  11. COMPLEMENTARY
  12. select @@session.use_stat_tables;
  13. @@session.use_stat_tables
  14. COMPLEMENTARY
  15. set @save_use_stat_tables=@@use_stat_tables;
  16. set @save_histogram_size=@@global.histogram_size;
  17. set @@global.histogram_size=0,@@local.histogram_size=0;
  18. set optimizer_use_condition_selectivity=4;
  19. set use_stat_tables='preferably';
  20. set @save_histogram_type=@@histogram_type;
  21. set histogram_type='single_prec_hb';
  22. DROP DATABASE IF EXISTS dbt3_s001;
  23. CREATE DATABASE dbt3_s001;
  24. use dbt3_s001;
  25. set @save_optimizer_switch=@@optimizer_switch;
  26. set optimizer_switch='extended_keys=off';
  27. select * from mysql.table_stats;
  28. db_name table_name cardinality
  29. dbt3_s001 customer 150
  30. dbt3_s001 lineitem 6005
  31. dbt3_s001 nation 25
  32. dbt3_s001 orders 1500
  33. dbt3_s001 part 200
  34. dbt3_s001 partsupp 700
  35. dbt3_s001 region 5
  36. dbt3_s001 supplier 10
  37. select * from mysql.index_stats;
  38. db_name table_name index_name prefix_arity avg_frequency
  39. dbt3_s001 customer PRIMARY 1 1.0000
  40. dbt3_s001 customer i_c_nationkey 1 6.0000
  41. dbt3_s001 lineitem PRIMARY 1 4.0033
  42. dbt3_s001 lineitem PRIMARY 2 1.0000
  43. dbt3_s001 lineitem i_l_shipdate 1 2.6500
  44. dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250
  45. dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786
  46. dbt3_s001 lineitem i_l_partkey 1 30.0250
  47. dbt3_s001 lineitem i_l_suppkey 1 600.5000
  48. dbt3_s001 lineitem i_l_receiptdate 1 2.6477
  49. dbt3_s001 lineitem i_l_orderkey 1 4.0033
  50. dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033
  51. dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404
  52. dbt3_s001 lineitem i_l_commitdate 1 2.7160
  53. dbt3_s001 nation PRIMARY 1 1.0000
  54. dbt3_s001 nation i_n_regionkey 1 5.0000
  55. dbt3_s001 orders PRIMARY 1 1.0000
  56. dbt3_s001 orders i_o_orderdate 1 1.3321
  57. dbt3_s001 orders i_o_custkey 1 15.0000
  58. dbt3_s001 part PRIMARY 1 1.0000
  59. dbt3_s001 part i_p_retailprice 1 1.0000
  60. dbt3_s001 partsupp PRIMARY 1 3.5000
  61. dbt3_s001 partsupp PRIMARY 2 1.0000
  62. dbt3_s001 partsupp i_ps_partkey 1 3.5000
  63. dbt3_s001 partsupp i_ps_suppkey 1 70.0000
  64. dbt3_s001 region PRIMARY 1 1.0000
  65. dbt3_s001 supplier PRIMARY 1 1.0000
  66. dbt3_s001 supplier i_s_nationkey 1 1.1111
  67. set optimizer_switch=@save_optimizer_switch;
  68. set @save_optimizer_switch=@@optimizer_switch;
  69. set optimizer_switch='index_condition_pushdown=off';
  70. EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue
  71. from customer, orders, lineitem, supplier, nation, region
  72. where c_custkey = o_custkey and l_orderkey = o_orderkey
  73. and l_suppkey = s_suppkey and c_nationkey = s_nationkey
  74. and s_nationkey = n_nationkey and n_regionkey = r_regionkey
  75. and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01'
  76. and o_orderdate < date '1995-01-01' + interval '1' year
  77. group by n_name
  78. order by revenue desc;
  79. id select_type table type possible_keys key key_len ref rows Extra
  80. 1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using temporary; Using filesort
  81. 1 SIMPLE nation ref PRIMARY,i_n_regionkey i_n_regionkey 5 dbt3_s001.region.r_regionkey 5
  82. 1 SIMPLE supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1 Using index
  83. 1 SIMPLE customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 6 Using index
  84. 1 SIMPLE orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 15 (14%) Using where; Using rowid filter
  85. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 9 dbt3_s001.supplier.s_suppkey,dbt3_s001.orders.o_orderkey 1
  86. select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue
  87. from customer, orders, lineitem, supplier, nation, region
  88. where c_custkey = o_custkey and l_orderkey = o_orderkey
  89. and l_suppkey = s_suppkey and c_nationkey = s_nationkey
  90. and s_nationkey = n_nationkey and n_regionkey = r_regionkey
  91. and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01'
  92. and o_orderdate < date '1995-01-01' + interval '1' year
  93. group by n_name
  94. order by revenue desc;
  95. n_name revenue
  96. PERU 321915.87150000007
  97. ARGENTINA 69817.1451
  98. set optimizer_switch=@save_optimizer_switch;
  99. delete from mysql.index_stats;
  100. select * from mysql.table_stats;
  101. db_name table_name cardinality
  102. dbt3_s001 customer 150
  103. dbt3_s001 lineitem 6005
  104. dbt3_s001 nation 25
  105. dbt3_s001 orders 1500
  106. dbt3_s001 part 200
  107. dbt3_s001 partsupp 700
  108. dbt3_s001 region 5
  109. dbt3_s001 supplier 10
  110. select * from mysql.index_stats;
  111. db_name table_name index_name prefix_arity avg_frequency
  112. dbt3_s001 customer PRIMARY 1 1.0000
  113. dbt3_s001 customer i_c_nationkey 1 6.0000
  114. dbt3_s001 customer i_c_nationkey 2 1.0000
  115. dbt3_s001 lineitem PRIMARY 1 4.0033
  116. dbt3_s001 lineitem PRIMARY 2 1.0000
  117. dbt3_s001 lineitem i_l_shipdate 1 2.6500
  118. dbt3_s001 lineitem i_l_shipdate 2 1.0149
  119. dbt3_s001 lineitem i_l_shipdate 3 1.0000
  120. dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250
  121. dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786
  122. dbt3_s001 lineitem i_l_suppkey_partkey 3 1.0030
  123. dbt3_s001 lineitem i_l_suppkey_partkey 4 1.0000
  124. dbt3_s001 lineitem i_l_partkey 1 30.0250
  125. dbt3_s001 lineitem i_l_partkey 2 1.0089
  126. dbt3_s001 lineitem i_l_partkey 3 1.0000
  127. dbt3_s001 lineitem i_l_suppkey 1 600.5000
  128. dbt3_s001 lineitem i_l_suppkey 2 1.2073
  129. dbt3_s001 lineitem i_l_suppkey 3 1.0000
  130. dbt3_s001 lineitem i_l_receiptdate 1 2.6477
  131. dbt3_s001 lineitem i_l_receiptdate 2 1.0152
  132. dbt3_s001 lineitem i_l_receiptdate 3 1.0000
  133. dbt3_s001 lineitem i_l_orderkey 1 4.0033
  134. dbt3_s001 lineitem i_l_orderkey 2 1.0000
  135. dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033
  136. dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404
  137. dbt3_s001 lineitem i_l_orderkey_quantity 3 1.0000
  138. dbt3_s001 lineitem i_l_commitdate 1 2.7160
  139. dbt3_s001 lineitem i_l_commitdate 2 1.0364
  140. dbt3_s001 lineitem i_l_commitdate 3 1.0000
  141. dbt3_s001 nation PRIMARY 1 1.0000
  142. dbt3_s001 nation i_n_regionkey 1 5.0000
  143. dbt3_s001 nation i_n_regionkey 2 1.0000
  144. dbt3_s001 orders PRIMARY 1 1.0000
  145. dbt3_s001 orders i_o_orderdate 1 1.3321
  146. dbt3_s001 orders i_o_orderdate 2 1.0000
  147. dbt3_s001 orders i_o_custkey 1 15.0000
  148. dbt3_s001 orders i_o_custkey 2 1.0000
  149. dbt3_s001 part PRIMARY 1 1.0000
  150. dbt3_s001 part i_p_retailprice 1 1.0000
  151. dbt3_s001 part i_p_retailprice 2 1.0000
  152. dbt3_s001 partsupp PRIMARY 1 3.5000
  153. dbt3_s001 partsupp PRIMARY 2 1.0000
  154. dbt3_s001 partsupp i_ps_partkey 1 3.5000
  155. dbt3_s001 partsupp i_ps_partkey 2 1.0000
  156. dbt3_s001 partsupp i_ps_suppkey 1 70.0000
  157. dbt3_s001 partsupp i_ps_suppkey 2 1.0000
  158. dbt3_s001 region PRIMARY 1 1.0000
  159. dbt3_s001 supplier PRIMARY 1 1.0000
  160. dbt3_s001 supplier i_s_nationkey 1 1.1111
  161. dbt3_s001 supplier i_s_nationkey 2 1.0000
  162. select * from mysql.table_stats where table_name='orders';
  163. db_name table_name cardinality
  164. dbt3_s001 orders 1500
  165. select * from mysql.index_stats where table_name='orders';
  166. db_name table_name index_name prefix_arity avg_frequency
  167. dbt3_s001 orders PRIMARY 1 1.0000
  168. dbt3_s001 orders i_o_orderdate 1 1.3321
  169. dbt3_s001 orders i_o_orderdate 2 1.0000
  170. dbt3_s001 orders i_o_custkey 1 15.0000
  171. dbt3_s001 orders i_o_custkey 2 1.0000
  172. select (select cardinality from mysql.table_stats where table_name='orders') /
  173. (select avg_frequency from mysql.index_stats
  174. where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct;
  175. n_distinct
  176. 1126.0416
  177. select count(distinct o_orderdate) from orders;
  178. count(distinct o_orderdate)
  179. 1126
  180. select (select cardinality from mysql.table_stats where table_name='orders') /
  181. (select avg_frequency from mysql.index_stats
  182. where index_name='i_o_custkey' and prefix_arity=1) as n_distinct;
  183. n_distinct
  184. 100.0000
  185. select count(distinct o_custkey) from orders;
  186. count(distinct o_custkey)
  187. 100
  188. show index from orders;
  189. Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
  190. orders 0 PRIMARY 1 o_orderkey A 1500 NULL NULL BTREE NO
  191. orders 1 i_o_orderdate 1 o_orderDATE A 1126 NULL NULL YES BTREE NO
  192. orders 1 i_o_custkey 1 o_custkey A 100 NULL NULL YES BTREE NO
  193. select index_name, column_name, cardinality from information_schema.statistics
  194. where table_name='orders';
  195. index_name column_name cardinality
  196. PRIMARY o_orderkey 1500
  197. i_o_orderdate o_orderDATE 1126
  198. i_o_custkey o_custkey 100
  199. set @save_optimizer_switch=@@optimizer_switch;
  200. set optimizer_switch='index_condition_pushdown=off';
  201. EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue
  202. from customer, orders, lineitem, supplier, nation, region
  203. where c_custkey = o_custkey and l_orderkey = o_orderkey
  204. and l_suppkey = s_suppkey and c_nationkey = s_nationkey
  205. and s_nationkey = n_nationkey and n_regionkey = r_regionkey
  206. and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01'
  207. and o_orderdate < date '1995-01-01' + interval '1' year
  208. group by n_name
  209. order by revenue desc;
  210. id select_type table type possible_keys key key_len ref rows Extra
  211. 1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using temporary; Using filesort
  212. 1 SIMPLE nation ref PRIMARY,i_n_regionkey i_n_regionkey 5 dbt3_s001.region.r_regionkey 5
  213. 1 SIMPLE supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.nation.n_nationkey 1 Using index
  214. 1 SIMPLE customer ref PRIMARY,i_c_nationkey i_c_nationkey 5 dbt3_s001.nation.n_nationkey 6 Using index
  215. 1 SIMPLE orders ref|filter PRIMARY,i_o_orderdate,i_o_custkey i_o_custkey|i_o_orderdate 5|4 dbt3_s001.customer.c_custkey 15 (14%) Using where; Using rowid filter
  216. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 9 dbt3_s001.supplier.s_suppkey,dbt3_s001.orders.o_orderkey 1
  217. select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue
  218. from customer, orders, lineitem, supplier, nation, region
  219. where c_custkey = o_custkey and l_orderkey = o_orderkey
  220. and l_suppkey = s_suppkey and c_nationkey = s_nationkey
  221. and s_nationkey = n_nationkey and n_regionkey = r_regionkey
  222. and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01'
  223. and o_orderdate < date '1995-01-01' + interval '1' year
  224. group by n_name
  225. order by revenue desc;
  226. n_name revenue
  227. PERU 321915.87150000007
  228. ARGENTINA 69817.1451
  229. set optimizer_switch=@save_optimizer_switch;
  230. EXPLAIN select o_year,
  231. sum(case when nation = 'UNITED STATES' then volume else 0 end) /
  232. sum(volume) as mkt_share
  233. from (select extract(year from o_orderdate) as o_year,
  234. l_extendedprice * (1-l_discount) as volume,
  235. n2.n_name as nation
  236. from part, supplier, lineitem, orders, customer,
  237. nation n1, nation n2, region
  238. where p_partkey = l_partkey and s_suppkey = l_suppkey
  239. and l_orderkey = o_orderkey and o_custkey = c_custkey
  240. and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey
  241. and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey
  242. and o_orderdate between date '1995-01-01' and date '1996-12-31'
  243. and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations
  244. group by o_year
  245. order by o_year;
  246. id select_type table type possible_keys key key_len ref rows Extra
  247. 1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using temporary; Using filesort
  248. 1 SIMPLE part ALL PRIMARY NULL NULL NULL 200 Using where; Using join buffer (flat, BNL join)
  249. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_partkey 5 dbt3_s001.part.p_partkey 30 Using where
  250. 1 SIMPLE supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
  251. 1 SIMPLE n2 eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1
  252. 1 SIMPLE orders eq_ref PRIMARY,i_o_orderdate,i_o_custkey PRIMARY 4 dbt3_s001.lineitem.l_orderkey 1 Using where
  253. 1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
  254. 1 SIMPLE n1 eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1 Using where
  255. select o_year,
  256. sum(case when nation = 'UNITED STATES' then volume else 0 end) /
  257. sum(volume) as mkt_share
  258. from (select extract(year from o_orderdate) as o_year,
  259. l_extendedprice * (1-l_discount) as volume,
  260. n2.n_name as nation
  261. from part, supplier, lineitem, orders, customer,
  262. nation n1, nation n2, region
  263. where p_partkey = l_partkey and s_suppkey = l_suppkey
  264. and l_orderkey = o_orderkey and o_custkey = c_custkey
  265. and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey
  266. and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey
  267. and o_orderdate between date '1995-01-01' and date '1996-12-31'
  268. and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations
  269. group by o_year
  270. order by o_year;
  271. o_year mkt_share
  272. 1995 0.4495521838895718
  273. 1996 0.024585468215352495
  274. EXPLAIN select nation, o_year, sum(amount) as sum_profit
  275. from (select n_name as nation,
  276. extract(year from o_orderdate) as o_year,
  277. l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
  278. from part, supplier, lineitem, partsupp, orders, nation
  279. where s_suppkey = l_suppkey and ps_suppkey = l_suppkey
  280. and ps_partkey = l_partkey and p_partkey = l_partkey
  281. and o_orderkey = l_orderkey and s_nationkey = n_nationkey
  282. and p_name like '%green%') as profit
  283. group by nation, o_year
  284. order by nation, o_year desc;
  285. id select_type table type possible_keys key key_len ref rows Extra
  286. 1 SIMPLE supplier index PRIMARY,i_s_nationkey i_s_nationkey 5 NULL 10 Using where; Using index; Using temporary; Using filesort
  287. 1 SIMPLE nation eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1
  288. 1 SIMPLE partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70
  289. 1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
  290. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey_partkey 10 dbt3_s001.partsupp.ps_partkey,dbt3_s001.supplier.s_suppkey 8
  291. 1 SIMPLE orders eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_orderkey 1
  292. select nation, o_year, sum(amount) as sum_profit
  293. from (select n_name as nation,
  294. extract(year from o_orderdate) as o_year,
  295. l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
  296. from part, supplier, lineitem, partsupp, orders, nation
  297. where s_suppkey = l_suppkey and ps_suppkey = l_suppkey
  298. and ps_partkey = l_partkey and p_partkey = l_partkey
  299. and o_orderkey = l_orderkey and s_nationkey = n_nationkey
  300. and p_name like '%green%') as profit
  301. group by nation, o_year
  302. order by nation, o_year desc;
  303. nation o_year sum_profit
  304. ARGENTINA 1997 18247.873399999993
  305. ARGENTINA 1996 7731.089399999995
  306. ARGENTINA 1995 134490.5697
  307. ARGENTINA 1994 36767.101500000004
  308. ARGENTINA 1993 35857.08
  309. ARGENTINA 1992 35740
  310. ETHIOPIA 1998 2758.7801999999992
  311. ETHIOPIA 1997 19419.294599999997
  312. ETHIOPIA 1995 51231.87439999999
  313. ETHIOPIA 1994 3578.9478999999974
  314. ETHIOPIA 1992 1525.8234999999986
  315. IRAN 1998 37817.229600000006
  316. IRAN 1997 52643.77359999999
  317. IRAN 1996 70143.7761
  318. IRAN 1995 84094.58260000001
  319. IRAN 1994 18140.925599999995
  320. IRAN 1993 78655.1676
  321. IRAN 1992 87142.23960000002
  322. IRAQ 1998 22860.8082
  323. IRAQ 1997 93676.24359999999
  324. IRAQ 1996 45103.3242
  325. IRAQ 1994 36010.728599999995
  326. IRAQ 1993 33221.9399
  327. IRAQ 1992 47755.05900000001
  328. KENYA 1998 44194.831999999995
  329. KENYA 1997 57578.36259999999
  330. KENYA 1996 59195.90210000001
  331. KENYA 1995 79262.6278
  332. KENYA 1994 102360.66609999999
  333. KENYA 1993 128422.0196
  334. KENYA 1992 181517.2089
  335. MOROCCO 1998 41797.823199999984
  336. MOROCCO 1997 23685.801799999994
  337. MOROCCO 1996 62115.19579999998
  338. MOROCCO 1995 42442.64300000001
  339. MOROCCO 1994 48655.878000000004
  340. MOROCCO 1993 22926.744400000003
  341. MOROCCO 1992 32239.8088
  342. PERU 1998 86999.36459999997
  343. PERU 1997 121110.41070000001
  344. PERU 1996 177040.40759999995
  345. PERU 1995 122247.94520000002
  346. PERU 1994 88046.25329999998
  347. PERU 1993 49379.813799999996
  348. PERU 1992 80646.86050000001
  349. UNITED KINGDOM 1998 50577.25560000001
  350. UNITED KINGDOM 1997 114288.8605
  351. UNITED KINGDOM 1996 147684.46480000002
  352. UNITED KINGDOM 1995 225267.65759999998
  353. UNITED KINGDOM 1994 140595.5864
  354. UNITED KINGDOM 1993 322548.49210000003
  355. UNITED KINGDOM 1992 67747.88279999999
  356. UNITED STATES 1998 3957.0431999999996
  357. UNITED STATES 1997 94729.5704
  358. UNITED STATES 1996 79297.85670000002
  359. UNITED STATES 1995 62201.23360000001
  360. UNITED STATES 1994 43075.629899999985
  361. UNITED STATES 1993 27168.486199999996
  362. UNITED STATES 1992 34092.366
  363. set @save_optimizer_switch=@@optimizer_switch;
  364. set optimizer_switch='extended_keys=on';
  365. EXPLAIN select o_orderkey, p_partkey
  366. from part, lineitem, orders
  367. where p_retailprice > 1100 and o_orderdate='1997-01-01'
  368. and o_orderkey=l_orderkey and p_partkey=l_partkey;
  369. id select_type table type possible_keys key key_len ref rows Extra
  370. 1 SIMPLE part range PRIMARY,i_p_retailprice i_p_retailprice 9 NULL 1 Using where; Using index
  371. 1 SIMPLE orders ref PRIMARY,i_o_orderdate i_o_orderdate 4 const 1 Using index
  372. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_orderkey,i_l_orderkey_quantity i_l_partkey 9 dbt3_s001.part.p_partkey,dbt3_s001.orders.o_orderkey 1 Using index
  373. select o_orderkey, p_partkey
  374. from part, lineitem, orders
  375. where p_retailprice > 1100 and o_orderdate='1997-01-01'
  376. and o_orderkey=l_orderkey and p_partkey=l_partkey;
  377. o_orderkey p_partkey
  378. 5895 200
  379. set optimizer_switch=@save_optimizer_switch;
  380. DROP DATABASE dbt3_s001;
  381. use test;
  382. #
  383. # Bug mdev-473: ANALYZE table locked for write
  384. #
  385. set use_stat_tables='complementary';
  386. create table t1 (i int);
  387. lock table t1 write;
  388. analyze table t1;
  389. Table Op Msg_type Msg_text
  390. test.t1 analyze status Engine-independent statistics collected
  391. test.t1 analyze status OK
  392. alter table t1 add column a varchar(8);
  393. drop table t1;
  394. #
  395. # Bug mdev-487: memory leak in ANALYZE with stat tables
  396. #
  397. SET use_stat_tables = 'preferably';
  398. CREATE TABLE t1 (a INT);
  399. INSERT INTO t1 VALUES (1),(2);
  400. DELETE FROM t1 WHERE a=1;
  401. ANALYZE TABLE t1;
  402. Table Op Msg_type Msg_text
  403. test.t1 analyze status Engine-independent statistics collected
  404. test.t1 analyze status OK
  405. DROP TABLE t1;
  406. #
  407. # Bug mdev-518: corrupted/missing statistical tables
  408. #
  409. CREATE TABLE t1 (i int) ENGINE=MyISAM;
  410. INSERT INTO t1 VALUES (1),(2);
  411. FLUSH TABLE t1;
  412. SET use_stat_tables='never';
  413. EXPLAIN SELECT * FROM t1;
  414. id select_type table type possible_keys key key_len ref rows Extra
  415. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  416. FLUSH TABLES;
  417. SET use_stat_tables='preferably';
  418. EXPLAIN SELECT * FROM t1;
  419. id select_type table type possible_keys key key_len ref rows Extra
  420. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  421. DROP TABLE t1;
  422. set use_stat_tables=@save_use_stat_tables;
  423. #
  424. # Bug mdev-5204: invalid impossible where after reading const tables
  425. # when use_stat_tables = 'preferably'
  426. #
  427. set use_stat_tables = 'preferably';
  428. CREATE TABLE t1 (id int PRIMARY KEY) ENGINE=MyISAM;
  429. INSERT INTO t1 VALUES (1),(2);
  430. ANALYZE TABLE t1;
  431. Table Op Msg_type Msg_text
  432. test.t1 analyze status Engine-independent statistics collected
  433. test.t1 analyze status OK
  434. CREATE TABLE t2 (name char(3)) ENGINE=MyISAM;
  435. ANALYZE TABLE t2;
  436. Table Op Msg_type Msg_text
  437. test.t2 analyze status Engine-independent statistics collected
  438. test.t2 analyze status Table is already up to date
  439. INSERT INTO t2 VALUES ('USA'),('AUS');
  440. SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1;
  441. id name
  442. 1 AUS
  443. EXPLAIN
  444. SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1;
  445. id select_type table type possible_keys key key_len ref rows Extra
  446. 1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index
  447. 1 SIMPLE t2 ALL NULL NULL NULL NULL 1 Using where
  448. ANALYZE TABLE t2;
  449. Table Op Msg_type Msg_text
  450. test.t2 analyze status Engine-independent statistics collected
  451. test.t2 analyze status OK
  452. SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1;
  453. id name
  454. 1 AUS
  455. EXPLAIN
  456. SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1;
  457. id select_type table type possible_keys key key_len ref rows Extra
  458. 1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index
  459. 1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where
  460. DROP TABLE t1,t2;
  461. #
  462. # MDEV-7370: Server deadlocks on renaming a table for which persistent statistics exists
  463. #
  464. drop database if exists db1;
  465. drop database if exists db1;
  466. create database db1;
  467. create database db2;
  468. use db1;
  469. #
  470. # First, run the original testcase:
  471. #
  472. create table t1 (i int);
  473. insert into t1 values (10),(20);
  474. analyze table t1 persistent for all;
  475. Table Op Msg_type Msg_text
  476. db1.t1 analyze status Engine-independent statistics collected
  477. db1.t1 analyze status OK
  478. rename table t1 to db2.t1;
  479. # Verify that stats in the old database are gone:
  480. select * from mysql.column_stats where db_name='db1' and table_name='t1';
  481. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  482. select * from mysql.table_stats where db_name='db1' and table_name='t1';
  483. db_name table_name cardinality
  484. # Verify that stats are present in the new database:
  485. select * from mysql.column_stats where db_name='db2' and table_name='t1';
  486. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  487. db2 t1 i 10 20 0.0000 4.0000 1.0000 0 NULL NULL
  488. select * from mysql.table_stats where db_name='db2' and table_name='t1';
  489. db_name table_name cardinality
  490. db2 t1 2
  491. #
  492. # Now, try with more than one column and with indexes:
  493. #
  494. use test;
  495. create table t1(a int primary key);
  496. insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  497. use db1;
  498. create table t2 (a int, b int, c int, key IDX1(a), key IDX2(a,b));
  499. insert into t2 select a/10, a/2, a from test.t1;
  500. analyze table t2 persistent for all;
  501. Table Op Msg_type Msg_text
  502. db1.t2 analyze status Engine-independent statistics collected
  503. db1.t2 analyze status OK
  504. alter table t2 rename db2.t2;
  505. # Verify that stats in the old database are gone:
  506. select * from mysql.table_stats where db_name='db1' and table_name='t2';
  507. db_name table_name cardinality
  508. select * from mysql.column_stats where db_name='db1' and table_name='t2';
  509. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  510. select * from mysql.index_stats where db_name='db1' and table_name='t2';
  511. db_name table_name index_name prefix_arity avg_frequency
  512. # Verify that stats are present in the new database:
  513. select * from mysql.table_stats where db_name='db2' and table_name='t2';
  514. db_name table_name cardinality
  515. db2 t2 10
  516. select * from mysql.column_stats where db_name='db2' and table_name='t2';
  517. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  518. db2 t2 a 0 1 0.0000 4.0000 5.0000 0 NULL NULL
  519. db2 t2 b 0 5 0.0000 4.0000 1.6667 0 NULL NULL
  520. db2 t2 c 0 9 0.0000 4.0000 1.0000 0 NULL NULL
  521. select * from mysql.index_stats where db_name='db2' and table_name='t2';
  522. db_name table_name index_name prefix_arity avg_frequency
  523. db2 t2 IDX1 1 5.0000
  524. db2 t2 IDX2 1 5.0000
  525. db2 t2 IDX2 2 1.6667
  526. use db2;
  527. #
  528. # Now, rename within the same database and verify:
  529. #
  530. rename table t2 to t3;
  531. # No stats under old name:
  532. select * from mysql.table_stats where db_name='db2' and table_name='t2';
  533. db_name table_name cardinality
  534. select * from mysql.column_stats where db_name='db2' and table_name='t2';
  535. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  536. select * from mysql.index_stats where db_name='db2' and table_name='t2';
  537. db_name table_name index_name prefix_arity avg_frequency
  538. # Stats under the new name:
  539. select * from mysql.table_stats where db_name='db2' and table_name='t3';
  540. db_name table_name cardinality
  541. db2 t3 10
  542. select * from mysql.column_stats where db_name='db2' and table_name='t3';
  543. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  544. db2 t3 a 0 1 0.0000 4.0000 5.0000 0 NULL NULL
  545. db2 t3 b 0 5 0.0000 4.0000 1.6667 0 NULL NULL
  546. db2 t3 c 0 9 0.0000 4.0000 1.0000 0 NULL NULL
  547. select * from mysql.index_stats where db_name='db2' and table_name='t3';
  548. db_name table_name index_name prefix_arity avg_frequency
  549. db2 t3 IDX1 1 5.0000
  550. db2 t3 IDX2 1 5.0000
  551. db2 t3 IDX2 2 1.6667
  552. use test;
  553. drop database db1;
  554. drop database db2;
  555. drop table t1;
  556. #
  557. # MDEV-16552: [10.0] ASAN global-buffer-overflow in is_stat_table / statistics_for_tables_is_needed
  558. #
  559. SET use_stat_tables = PREFERABLY;
  560. SELECT CONVERT_TZ( '1991-09-20 10:11:02', '+00:00', 'GMT' );
  561. CONVERT_TZ( '1991-09-20 10:11:02', '+00:00', 'GMT' )
  562. NULL
  563. set use_stat_tables=@save_use_stat_tables;
  564. #
  565. # MDEV-16757: manual addition of min/max statistics for BLOB
  566. #
  567. SET use_stat_tables= PREFERABLY;
  568. CREATE TABLE t1 (pk INT PRIMARY KEY, t TEXT);
  569. INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
  570. ANALYZE TABLE t1;
  571. Table Op Msg_type Msg_text
  572. test.t1 analyze status Engine-independent statistics collected
  573. test.t1 analyze Warning Engine-independent statistics are not collected for column 't'
  574. test.t1 analyze status OK
  575. SELECT * FROM mysql.column_stats;
  576. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  577. test t1 pk 1 2 0.0000 4.0000 1.0000 0 NULL NULL
  578. DELETE FROM mysql.column_stats
  579. WHERE db_name='test' AND table_name='t1' AND column_name='t';
  580. INSERT INTO mysql.column_stats VALUES
  581. ('test','t1','t','bar','foo', 0.0, 3.0, 1.0, 0, NULL, NULL);
  582. SELECT * FROM mysql.column_stats;
  583. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  584. test t1 pk 1 2 0.0000 4.0000 1.0000 0 NULL NULL
  585. test t1 t bar foo 0.0000 3.0000 1.0000 0 NULL NULL
  586. SELECT pk FROM t1;
  587. pk
  588. 1
  589. 2
  590. DROP TABLE t1;
  591. set use_stat_tables=@save_use_stat_tables;
  592. #
  593. # MDEV-16760: CREATE OR REPLACE TABLE after ANALYZE TABLE
  594. #
  595. SET use_stat_tables= PREFERABLY;
  596. CREATE TABLE t1 (pk int PRIMARY KEY, c varchar(32));
  597. INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
  598. ANALYZE TABLE t1;
  599. Table Op Msg_type Msg_text
  600. test.t1 analyze status Engine-independent statistics collected
  601. test.t1 analyze status OK
  602. SELECT * FROM t1;
  603. pk c
  604. 1 foo
  605. 2 bar
  606. SELECT * FROM mysql.column_stats;
  607. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  608. test t1 pk 1 2 0.0000 4.0000 1.0000 0 NULL NULL
  609. test t1 c bar foo 0.0000 3.0000 1.0000 0 NULL NULL
  610. CREATE OR REPLACE TABLE t1 (pk int PRIMARY KEY, a char(7));
  611. SELECT * FROM t1;
  612. pk a
  613. SELECT * FROM mysql.column_stats;
  614. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  615. DROP TABLE t1;
  616. set use_stat_tables=@save_use_stat_tables;
  617. #
  618. # MDEV-17023: Crash during read_histogram_for_table with optimizer_use_condition_selectivity set to 4
  619. #
  620. set @save_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity;
  621. set @@optimizer_use_condition_selectivity=4;
  622. set @@use_stat_tables= PREFERABLY;
  623. explain
  624. SELECT * FROM INFORMATION_SCHEMA.PROFILING, mysql.user;
  625. id select_type table type possible_keys key key_len ref rows Extra
  626. 1 SIMPLE PROFILING ALL NULL NULL NULL NULL NULL
  627. 1 SIMPLE global_priv ALL NULL NULL NULL NULL 5 Using join buffer (flat, BNL join)
  628. set @@optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
  629. set use_stat_tables=@save_use_stat_tables;
  630. #
  631. # MDEV-17734: AddressSanitizer: use-after-poison in create_key_parts_for_pseudo_indexes
  632. #
  633. set @@use_stat_tables= PREFERABLY;
  634. set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity;
  635. set @@optimizer_use_condition_selectivity=4;
  636. set @save_use_stat_tables= @@use_stat_tables;
  637. create table t1 (a int, b int);
  638. insert into t1(a,b) values (1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(3,8),(3,9),(3,9),(4,10);
  639. analyze table t1 persistent for columns (a) indexes ();
  640. Table Op Msg_type Msg_text
  641. test.t1 analyze status Engine-independent statistics collected
  642. test.t1 analyze status OK
  643. select * from t1 where a=1 and b=3;
  644. a b
  645. 1 3
  646. set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
  647. set use_stat_tables=@save_use_stat_tables;
  648. drop table t1;
  649. #
  650. # MDEV-16711:CREATE OR REPLACE TABLE introducing BLOB column
  651. #
  652. SET use_stat_tables= PREFERABLY;
  653. CREATE TABLE t1 (pk INT PRIMARY KEY, t CHAR(60));
  654. INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
  655. ANALYZE TABLE t1;
  656. Table Op Msg_type Msg_text
  657. test.t1 analyze status Engine-independent statistics collected
  658. test.t1 analyze status OK
  659. CREATE OR REPLACE TABLE t1 (pk INT PRIMARY KEY, t TEXT);
  660. SELECT MAX(pk) FROM t1;
  661. MAX(pk)
  662. NULL
  663. DROP TABLE t1;
  664. set use_stat_tables=@save_use_stat_tables;
  665. #
  666. # MDEV-17605: SHOW INDEXES with use_stat_tables='preferably'
  667. #
  668. set use_stat_tables='preferably';
  669. CREATE DATABASE dbt3_s001;
  670. use dbt3_s001;
  671. set @save_optimizer_switch=@@optimizer_switch;
  672. set optimizer_switch='extended_keys=off';
  673. select * from mysql.table_stats;
  674. db_name table_name cardinality
  675. dbt3_s001 lineitem 6005
  676. select * from mysql.index_stats;
  677. db_name table_name index_name prefix_arity avg_frequency
  678. dbt3_s001 lineitem PRIMARY 1 4.0033
  679. dbt3_s001 lineitem PRIMARY 2 1.0000
  680. dbt3_s001 lineitem i_l_shipdate 1 2.6500
  681. dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250
  682. dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786
  683. dbt3_s001 lineitem i_l_partkey 1 30.0250
  684. dbt3_s001 lineitem i_l_suppkey 1 600.5000
  685. dbt3_s001 lineitem i_l_receiptdate 1 2.6477
  686. dbt3_s001 lineitem i_l_orderkey 1 4.0033
  687. dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033
  688. dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404
  689. dbt3_s001 lineitem i_l_commitdate 1 2.7160
  690. SHOW INDEXES FROM lineitem;
  691. Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
  692. lineitem 0 PRIMARY 1 l_orderkey A 1500 NULL NULL BTREE NO
  693. lineitem 0 PRIMARY 2 l_linenumber A 6005 NULL NULL BTREE NO
  694. lineitem 1 i_l_shipdate 1 l_shipDATE A 2266 NULL NULL YES BTREE NO
  695. lineitem 1 i_l_suppkey_partkey 1 l_partkey A 200 NULL NULL YES BTREE NO
  696. lineitem 1 i_l_suppkey_partkey 2 l_suppkey A 699 NULL NULL YES BTREE NO
  697. lineitem 1 i_l_partkey 1 l_partkey A 200 NULL NULL YES BTREE NO
  698. lineitem 1 i_l_suppkey 1 l_suppkey A 10 NULL NULL YES BTREE NO
  699. lineitem 1 i_l_receiptdate 1 l_receiptDATE A 2268 NULL NULL YES BTREE NO
  700. lineitem 1 i_l_orderkey 1 l_orderkey A 1500 NULL NULL BTREE NO
  701. lineitem 1 i_l_orderkey_quantity 1 l_orderkey A 1500 NULL NULL BTREE NO
  702. lineitem 1 i_l_orderkey_quantity 2 l_quantity A 5771 NULL NULL YES BTREE NO
  703. lineitem 1 i_l_commitdate 1 l_commitDATE A 2210 NULL NULL YES BTREE NO
  704. SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE table_name='lineitem';
  705. TABLE_CATALOG TABLE_SCHEMA TABLE_NAME NON_UNIQUE INDEX_SCHEMA INDEX_NAME SEQ_IN_INDEX COLUMN_NAME COLLATION CARDINALITY SUB_PART PACKED NULLABLE INDEX_TYPE COMMENT INDEX_COMMENT IGNORED
  706. def dbt3_s001 lineitem 0 dbt3_s001 PRIMARY 1 l_orderkey A 1500 NULL NULL BTREE NO
  707. def dbt3_s001 lineitem 0 dbt3_s001 PRIMARY 2 l_linenumber A 6005 NULL NULL BTREE NO
  708. def dbt3_s001 lineitem 1 dbt3_s001 i_l_shipdate 1 l_shipDATE A 2266 NULL NULL YES BTREE NO
  709. def dbt3_s001 lineitem 1 dbt3_s001 i_l_suppkey_partkey 1 l_partkey A 200 NULL NULL YES BTREE NO
  710. def dbt3_s001 lineitem 1 dbt3_s001 i_l_suppkey_partkey 2 l_suppkey A 699 NULL NULL YES BTREE NO
  711. def dbt3_s001 lineitem 1 dbt3_s001 i_l_partkey 1 l_partkey A 200 NULL NULL YES BTREE NO
  712. def dbt3_s001 lineitem 1 dbt3_s001 i_l_suppkey 1 l_suppkey A 10 NULL NULL YES BTREE NO
  713. def dbt3_s001 lineitem 1 dbt3_s001 i_l_receiptdate 1 l_receiptDATE A 2268 NULL NULL YES BTREE NO
  714. def dbt3_s001 lineitem 1 dbt3_s001 i_l_orderkey 1 l_orderkey A 1500 NULL NULL BTREE NO
  715. def dbt3_s001 lineitem 1 dbt3_s001 i_l_orderkey_quantity 1 l_orderkey A 1500 NULL NULL BTREE NO
  716. def dbt3_s001 lineitem 1 dbt3_s001 i_l_orderkey_quantity 2 l_quantity A 5771 NULL NULL YES BTREE NO
  717. def dbt3_s001 lineitem 1 dbt3_s001 i_l_commitdate 1 l_commitDATE A 2210 NULL NULL YES BTREE NO
  718. SELECT
  719. COUNT(DISTINCT l_orderkey), COUNT(DISTINCT l_orderkey,l_linenumber),
  720. COUNT(DISTINCT l_shipDATE),
  721. COUNT(DISTINCT l_partkey), COUNT(DISTINCT l_partkey,l_suppkey),
  722. COUNT(DISTINCT l_suppkey), COUNT(DISTINCT l_receiptDATE),
  723. COUNT(DISTINCT l_orderkey, l_quantity), COUNT(DISTINCT l_commitDATE)
  724. FROM lineitem;
  725. COUNT(DISTINCT l_orderkey) COUNT(DISTINCT l_orderkey,l_linenumber) COUNT(DISTINCT l_shipDATE) COUNT(DISTINCT l_partkey) COUNT(DISTINCT l_partkey,l_suppkey) COUNT(DISTINCT l_suppkey) COUNT(DISTINCT l_receiptDATE) COUNT(DISTINCT l_orderkey, l_quantity) COUNT(DISTINCT l_commitDATE)
  726. 1500 6005 2266 200 700 10 2268 5772 2211
  727. set optimizer_switch=@save_optimizer_switch;
  728. DROP DATABASE dbt3_s001;
  729. USE test;
  730. delete from mysql.table_stats;
  731. delete from mysql.column_stats;
  732. delete from mysql.index_stats;
  733. #
  734. # MDEV-19352: Server crash in alloc_histograms_for_table_share upon query from information schema
  735. #
  736. use test;
  737. set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity;
  738. set @@optimizer_use_condition_selectivity= 4;
  739. set use_stat_tables='preferably';
  740. CREATE TABLE t1 (a INT);
  741. CREATE TABLE t2 (b INT);
  742. CREATE VIEW v AS SELECT * FROM t1 JOIN t2;
  743. INSERT INTO t2 SELECT * FROM x;
  744. ERROR 42S02: Table 'test.x' doesn't exist
  745. select * from information_schema.tables where table_name='v';
  746. TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE ENGINE VERSION ROW_FORMAT TABLE_ROWS AVG_ROW_LENGTH DATA_LENGTH MAX_DATA_LENGTH INDEX_LENGTH DATA_FREE AUTO_INCREMENT CREATE_TIME UPDATE_TIME CHECK_TIME TABLE_COLLATION CHECKSUM CREATE_OPTIONS TABLE_COMMENT MAX_INDEX_LENGTH TEMPORARY
  747. def test v VIEW NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL VIEW NULL NULL
  748. set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
  749. drop table t1,t2;
  750. drop view v;
  751. #
  752. # MDEV-19407: Assertion `field->table->stats_is_read' failed in is_eits_usable
  753. #
  754. set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity;
  755. set @@optimizer_use_condition_selectivity= 1;
  756. set @@use_stat_tables='never';
  757. create table t1(pk int);
  758. insert into t1 values (4),(3);
  759. set @@optimizer_use_condition_selectivity= 4;
  760. set use_stat_tables='preferably';
  761. INSERT INTO t1 SELECT * FROM x;
  762. ERROR 42S02: Table 'test.x' doesn't exist
  763. CREATE TABLE t2 SELECT pk FROM t1 WHERE pk>2;
  764. select * from t2;
  765. pk
  766. 4
  767. 3
  768. drop table t1,t2;
  769. create table t1(a int,b int, key k1(a) );
  770. insert into t1 values(1,1),(2,2),(3,3);
  771. analyze table t1;
  772. Table Op Msg_type Msg_text
  773. test.t1 analyze status Engine-independent statistics collected
  774. test.t1 analyze status OK
  775. select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1;
  776. db_name table_name index_name prefix_arity avg_frequency a b
  777. test t1 k1 1 1.0000 2 2
  778. test t1 k1 1 1.0000 3 3
  779. drop table t1;
  780. set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
  781. set @save_optimizer_switch=@@optimizer_switch;
  782. set use_stat_tables=@save_use_stat_tables;
  783. #
  784. # MDEV-18899: Server crashes in Field::set_warning_truncated_wrong_value
  785. #
  786. set names utf8;
  787. set @save_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity;
  788. set optimizer_use_condition_selectivity=4;
  789. set use_stat_tables=preferably;
  790. set histogram_size=255;
  791. create table t1 ( a varchar(255) character set utf8);
  792. insert into t1 values (REPEAT('ӥ',255)), (REPEAT('ç',255));
  793. analyze table t1;
  794. Table Op Msg_type Msg_text
  795. test.t1 analyze status Engine-independent statistics collected
  796. test.t1 analyze status OK
  797. select HEX(RIGHT(min_value, 1)), length(min_value) from mysql.column_stats where db_name='test' and table_name='t1';
  798. HEX(RIGHT(min_value, 1)) length(min_value)
  799. A7 254
  800. select HEX(RIGHT(max_value, 1)), length(max_value) from mysql.column_stats where db_name='test' and table_name='t1';
  801. HEX(RIGHT(max_value, 1)) length(max_value)
  802. A5 254
  803. analyze select * from t1 where a >= 'ӥ';
  804. id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
  805. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2 2.00 50.00 50.00 Using where
  806. set @save_sql_mode= @@sql_mode;
  807. set sql_mode='ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  808. update mysql.column_stats set min_value= REPEAT('ӥ',255) where db_name='test' and table_name='t1';
  809. Warnings:
  810. Warning 1265 Data truncated for column 'min_value' at row 1
  811. select HEX(RIGHT(min_value, 1)), length(min_value) from mysql.column_stats where db_name='test' and table_name='t1';
  812. HEX(RIGHT(min_value, 1)) length(min_value)
  813. D3 255
  814. analyze select * from t1 where a >= 'ӥ';
  815. id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
  816. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2 2.00 50.00 50.00 Using where
  817. set names latin1;
  818. drop table t1;
  819. CREATE TABLE t1 (col1 date);
  820. INSERT INTO t1 VALUES('2004-01-01'),('2004-02-29');
  821. INSERT INTO t1 VALUES('0000-10-31');
  822. analyze table t1;
  823. Table Op Msg_type Msg_text
  824. test.t1 analyze status Engine-independent statistics collected
  825. test.t1 analyze status OK
  826. update mysql.column_stats set min_value='2004-0-31123' where db_name='test' and table_name='t1';
  827. select min_value from mysql.column_stats where db_name='test' and table_name='t1';
  828. min_value
  829. 2004-0-31123
  830. select * from t1;
  831. col1
  832. 2004-01-01
  833. 2004-02-29
  834. 0000-10-31
  835. set @@sql_mode= @save_sql_mode;
  836. set @@use_stat_tables=@save_use_stat_tables;
  837. set @@histogram_size= @save_histogram_size;
  838. set @@histogram_type=@save_histogram_type;
  839. set @@optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
  840. drop table t1;
  841. #
  842. # MDEV-20589: Server still crashes in Field::set_warning_truncated_wrong_value
  843. #
  844. set names utf8;
  845. create table t1 ( a varchar(255) character set utf8);
  846. insert into t1 values (REPEAT('ӥ',255)), (REPEAT('ç',255));
  847. set use_stat_tables='preferably';
  848. analyze table t1 persistent for all;
  849. Table Op Msg_type Msg_text
  850. test.t1 analyze status Engine-independent statistics collected
  851. test.t1 analyze status OK
  852. set @save_sql_mode= @@sql_mode;
  853. set sql_mode='ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  854. update mysql.column_stats set min_value= REPEAT('ӥ',256) where db_name='test' and table_name='t1';
  855. Warnings:
  856. Warning 1265 Data truncated for column 'min_value' at row 1
  857. set @@sql_mode= @save_sql_mode;
  858. select length(a) from t1 where a=REPEAT('ӥ',255);
  859. length(a)
  860. 510
  861. set names latin1;
  862. set @@use_stat_tables=@save_use_stat_tables;
  863. drop table t1;
  864. #
  865. # MDEV-23753: SIGSEGV in Column_stat::store_stat_fields
  866. #
  867. CREATE TABLE t1 (a INT, b INT) PARTITION BY HASH (b) PARTITIONS 2;
  868. LOCK TABLES t1 WRITE;
  869. ANALYZE TABLE t1 PERSISTENT FOR COLUMNS (a) INDEXES ();
  870. Table Op Msg_type Msg_text
  871. test.t1 analyze status Engine-independent statistics collected
  872. test.t1 analyze status OK
  873. ANALYZE TABLE t1 PERSISTENT FOR COLUMNS (nonexisting) INDEXES (nonexisting);
  874. Table Op Msg_type Msg_text
  875. test.t1 analyze status Engine-independent statistics collected
  876. test.t1 analyze error Invalid argument
  877. DROP TABLE t1;
  878. # please keep this at the last
  879. set @@global.histogram_size=@save_histogram_size;
  880. # Start of 10.4 tests
  881. set histogram_size=0;
  882. #
  883. # MDEV-17255: New optimizer defaults and ANALYZE TABLE
  884. #
  885. create table t1 (a int, b int);
  886. insert into t1(a,b) values (1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(3,8),(3,9),(3,9),(4,10);
  887. set use_stat_tables= preferably_for_queries;
  888. #
  889. # with use_stat_tables= PREFERABLY_FOR_QUERIES
  890. # analyze table t1 will not collect statistics
  891. #
  892. analyze table t1;
  893. Table Op Msg_type Msg_text
  894. test.t1 analyze status OK
  895. select * from mysql.column_stats;
  896. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  897. analyze
  898. select * from t1 where a = 1 and b=3;
  899. id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
  900. 1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 100.00 10.00 Using where
  901. #
  902. # with use_stat_tables= PREFERABLY_FOR_QUERIES
  903. # analyze table t1 will collect statistics if we use PERSISTENT
  904. # for columns, indexes or everything
  905. #
  906. analyze table t1 persistent for columns (a) indexes ();
  907. Table Op Msg_type Msg_text
  908. test.t1 analyze status Engine-independent statistics collected
  909. test.t1 analyze status OK
  910. select * from mysql.column_stats;
  911. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  912. test t1 a 1 4 0.0000 4.0000 2.5000 0 NULL NULL
  913. # filtered shows that we used the data from stat tables
  914. analyze
  915. select * from t1 where a = 1 and b=3;
  916. id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
  917. 1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 25.00 10.00 Using where
  918. #
  919. # with use_stat_tables= PREFERABLY
  920. # analyze table t1 will collect statistics
  921. #
  922. set use_stat_tables=PREFERABLY;
  923. analyze table t1;
  924. Table Op Msg_type Msg_text
  925. test.t1 analyze status Engine-independent statistics collected
  926. test.t1 analyze status OK
  927. select * from mysql.column_stats;
  928. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  929. test t1 a 1 4 0.0000 4.0000 2.5000 0 NULL NULL
  930. test t1 b 2 10 0.0000 4.0000 1.1111 0 NULL NULL
  931. # filtered shows that we used the data from stat tables
  932. analyze
  933. select * from t1 where a=1 and b=3;
  934. id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
  935. 1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10.00 10.00 10.00 Using where
  936. drop table t1;
  937. set @@global.histogram_size=@save_histogram_size;
  938. # End of 10.4 tests
  939. set global innodb_stats_persistent= @innodb_stats_persistent_save;
  940. set global innodb_stats_persistent_sample_pages=
  941. @innodb_stats_persistent_sample_pages_save;
  942. set optimizer_switch=@save_optimizer_switch_for_stat_tables_test;
  943. #
  944. # MDEV-22851: Engine independent index statistics are incorrect for large tables on Windows.
  945. #
  946. CREATE TABLE t1(a INT) ENGINE=INNODB;
  947. INSERT INTO t1 SELECT 1 FROM seq_1_to_60000;
  948. SET @save_use_stat_tables= @@use_stat_tables;
  949. SET use_stat_tables= preferably;
  950. SELECT count(*) FROM t1;
  951. count(*)
  952. 60000
  953. CREATE INDEX idx ON t1(a);
  954. ANALYZE TABLE t1 PERSISTENT FOR COLUMNS (a) INDEXES (idx);
  955. Table Op Msg_type Msg_text
  956. test.t1 analyze status Engine-independent statistics collected
  957. test.t1 analyze status OK
  958. SELECT * FROM mysql.index_stats where table_name='t1';
  959. db_name table_name index_name prefix_arity avg_frequency
  960. test t1 idx 1 60000.0000
  961. SELECT * FROM mysql.column_stats where table_name='t1';
  962. db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
  963. test t1 a 1 1 0.0000 4.0000 60000.0000 0 NULL NULL
  964. SET use_stat_tables= @save_use_stat_tables;
  965. DROP TABLE t1;
  966. # end of 10.1 tests
  967. SET SESSION DEFAULT_STORAGE_ENGINE=DEFAULT;