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.

368 lines
16 KiB

  1. SET SESSION STORAGE_ENGINE='InnoDB';
  2. set @save_optimizer_switch_for_stat_tables_test=@@optimizer_switch;
  3. set optimizer_switch='extended_keys=on';
  4. set @save_use_stat_tables=@@use_stat_tables;
  5. set use_stat_tables='preferably';
  6. DROP DATABASE IF EXISTS dbt3_s001;
  7. CREATE DATABASE dbt3_s001;
  8. use dbt3_s001;
  9. set @save_optimizer_switch=@@optimizer_switch;
  10. set optimizer_switch='extended_keys=off';
  11. select * from mysql.table_stat;
  12. db_name table_name cardinality
  13. dbt3_s001 customer 150
  14. dbt3_s001 lineitem 6005
  15. dbt3_s001 nation 25
  16. dbt3_s001 orders 1500
  17. dbt3_s001 part 200
  18. dbt3_s001 partsupp 700
  19. dbt3_s001 region 5
  20. dbt3_s001 supplier 10
  21. select * from mysql.index_stat;
  22. db_name table_name index_name prefix_arity avg_frequency
  23. dbt3_s001 customer PRIMARY 1 1.0000
  24. dbt3_s001 customer i_c_nationkey 1 6.0000
  25. dbt3_s001 lineitem PRIMARY 1 4.0033
  26. dbt3_s001 lineitem PRIMARY 2 1.0000
  27. dbt3_s001 lineitem i_l_shipdate 1 2.6500
  28. dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250
  29. dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786
  30. dbt3_s001 lineitem i_l_partkey 1 30.0250
  31. dbt3_s001 lineitem i_l_suppkey 1 600.5000
  32. dbt3_s001 lineitem i_l_receiptdate 1 2.6477
  33. dbt3_s001 lineitem i_l_orderkey 1 4.0033
  34. dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033
  35. dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404
  36. dbt3_s001 lineitem i_l_commitdate 1 2.7160
  37. dbt3_s001 nation PRIMARY 1 1.0000
  38. dbt3_s001 nation i_n_regionkey 1 5.0000
  39. dbt3_s001 orders PRIMARY 1 1.0000
  40. dbt3_s001 orders i_o_orderdate 1 1.3321
  41. dbt3_s001 orders i_o_custkey 1 15.0000
  42. dbt3_s001 part PRIMARY 1 1.0000
  43. dbt3_s001 part i_p_retailprice 1 1.0000
  44. dbt3_s001 partsupp PRIMARY 1 3.5000
  45. dbt3_s001 partsupp PRIMARY 2 1.0000
  46. dbt3_s001 partsupp i_ps_partkey 1 3.5000
  47. dbt3_s001 partsupp i_ps_suppkey 1 70.0000
  48. dbt3_s001 region PRIMARY 1 1.0000
  49. dbt3_s001 supplier PRIMARY 1 1.0000
  50. dbt3_s001 supplier i_s_nationkey 1 1.1111
  51. set optimizer_switch=@save_optimizer_switch;
  52. set @save_optimizer_switch=@@optimizer_switch;
  53. set optimizer_switch='index_condition_pushdown=off';
  54. EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue
  55. from customer, orders, lineitem, supplier, nation, region
  56. where c_custkey = o_custkey and l_orderkey = o_orderkey
  57. and l_suppkey = s_suppkey and c_nationkey = s_nationkey
  58. and s_nationkey = n_nationkey and n_regionkey = r_regionkey
  59. and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01'
  60. and o_orderdate < date '1995-01-01' + interval '1' year
  61. group by n_name
  62. order by revenue desc;
  63. id select_type table type possible_keys key key_len ref rows Extra
  64. 1 SIMPLE orders range PRIMARY,i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 211 Using where; Using temporary; Using filesort
  65. 1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
  66. 1 SIMPLE nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1
  67. 1 SIMPLE supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.customer.c_nationkey 1 Using index
  68. 1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join)
  69. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where
  70. 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. n_name revenue
  80. PERU 321915.8715
  81. ARGENTINA 69817.1451
  82. set optimizer_switch=@save_optimizer_switch;
  83. delete from mysql.index_stat;
  84. select * from mysql.table_stat;
  85. db_name table_name cardinality
  86. dbt3_s001 customer 150
  87. dbt3_s001 lineitem 6005
  88. dbt3_s001 nation 25
  89. dbt3_s001 orders 1500
  90. dbt3_s001 part 200
  91. dbt3_s001 partsupp 700
  92. dbt3_s001 region 5
  93. dbt3_s001 supplier 10
  94. select * from mysql.index_stat;
  95. db_name table_name index_name prefix_arity avg_frequency
  96. dbt3_s001 customer PRIMARY 1 1.0000
  97. dbt3_s001 customer i_c_nationkey 1 6.0000
  98. dbt3_s001 customer i_c_nationkey 2 1.0000
  99. dbt3_s001 lineitem PRIMARY 1 4.0033
  100. dbt3_s001 lineitem PRIMARY 2 1.0000
  101. dbt3_s001 lineitem i_l_shipdate 1 2.6500
  102. dbt3_s001 lineitem i_l_shipdate 2 1.0149
  103. dbt3_s001 lineitem i_l_shipdate 3 1.0000
  104. dbt3_s001 lineitem i_l_suppkey_partkey 1 30.0250
  105. dbt3_s001 lineitem i_l_suppkey_partkey 2 8.5786
  106. dbt3_s001 lineitem i_l_suppkey_partkey 3 1.0030
  107. dbt3_s001 lineitem i_l_suppkey_partkey 4 1.0000
  108. dbt3_s001 lineitem i_l_partkey 1 30.0250
  109. dbt3_s001 lineitem i_l_partkey 2 1.0089
  110. dbt3_s001 lineitem i_l_partkey 3 1.0000
  111. dbt3_s001 lineitem i_l_suppkey 1 600.5000
  112. dbt3_s001 lineitem i_l_suppkey 2 1.2073
  113. dbt3_s001 lineitem i_l_suppkey 3 1.0000
  114. dbt3_s001 lineitem i_l_receiptdate 1 2.6477
  115. dbt3_s001 lineitem i_l_receiptdate 2 1.0152
  116. dbt3_s001 lineitem i_l_receiptdate 3 1.0000
  117. dbt3_s001 lineitem i_l_orderkey 1 4.0033
  118. dbt3_s001 lineitem i_l_orderkey 2 1.0000
  119. dbt3_s001 lineitem i_l_orderkey_quantity 1 4.0033
  120. dbt3_s001 lineitem i_l_orderkey_quantity 2 1.0404
  121. dbt3_s001 lineitem i_l_orderkey_quantity 3 1.0000
  122. dbt3_s001 lineitem i_l_commitdate 1 2.7160
  123. dbt3_s001 lineitem i_l_commitdate 2 1.0364
  124. dbt3_s001 lineitem i_l_commitdate 3 1.0000
  125. dbt3_s001 nation PRIMARY 1 1.0000
  126. dbt3_s001 nation i_n_regionkey 1 5.0000
  127. dbt3_s001 nation i_n_regionkey 2 1.0000
  128. dbt3_s001 orders PRIMARY 1 1.0000
  129. dbt3_s001 orders i_o_orderdate 1 1.3321
  130. dbt3_s001 orders i_o_orderdate 2 1.0000
  131. dbt3_s001 orders i_o_custkey 1 15.0000
  132. dbt3_s001 orders i_o_custkey 2 1.0000
  133. dbt3_s001 part PRIMARY 1 1.0000
  134. dbt3_s001 part i_p_retailprice 1 1.0000
  135. dbt3_s001 part i_p_retailprice 2 1.0000
  136. dbt3_s001 partsupp PRIMARY 1 3.5000
  137. dbt3_s001 partsupp PRIMARY 2 1.0000
  138. dbt3_s001 partsupp i_ps_partkey 1 3.5000
  139. dbt3_s001 partsupp i_ps_partkey 2 1.0000
  140. dbt3_s001 partsupp i_ps_suppkey 1 70.0000
  141. dbt3_s001 partsupp i_ps_suppkey 2 1.0000
  142. dbt3_s001 region PRIMARY 1 1.0000
  143. dbt3_s001 supplier PRIMARY 1 1.0000
  144. dbt3_s001 supplier i_s_nationkey 1 1.1111
  145. dbt3_s001 supplier i_s_nationkey 2 1.0000
  146. select * from mysql.table_stat where table_name='orders';
  147. db_name table_name cardinality
  148. dbt3_s001 orders 1500
  149. select * from mysql.index_stat where table_name='orders';
  150. db_name table_name index_name prefix_arity avg_frequency
  151. dbt3_s001 orders PRIMARY 1 1.0000
  152. dbt3_s001 orders i_o_orderdate 1 1.3321
  153. dbt3_s001 orders i_o_orderdate 2 1.0000
  154. dbt3_s001 orders i_o_custkey 1 15.0000
  155. dbt3_s001 orders i_o_custkey 2 1.0000
  156. select (select cardinality from mysql.table_stat where table_name='orders') /
  157. (select avg_frequency from mysql.index_stat
  158. where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct;
  159. n_distinct
  160. 1126.0416
  161. select count(distinct o_orderdate) from orders;
  162. count(distinct o_orderdate)
  163. 1126
  164. select (select cardinality from mysql.table_stat where table_name='orders') /
  165. (select avg_frequency from mysql.index_stat
  166. where index_name='i_o_custkey' and prefix_arity=1) as n_distinct;
  167. n_distinct
  168. 100.0000
  169. select count(distinct o_custkey) from orders;
  170. count(distinct o_custkey)
  171. 100
  172. show index from orders;
  173. Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
  174. orders 0 PRIMARY 1 o_orderkey A 1500 NULL NULL BTREE
  175. orders 1 i_o_orderdate 1 o_orderDATE A 1126 NULL NULL YES BTREE
  176. orders 1 i_o_custkey 1 o_custkey A 100 NULL NULL YES BTREE
  177. select index_name, column_name, cardinality from information_schema.statistics
  178. where table_name='orders';
  179. index_name column_name cardinality
  180. PRIMARY o_orderkey 1500
  181. i_o_orderdate o_orderDATE 1126
  182. i_o_custkey o_custkey 100
  183. set @save_optimizer_switch=@@optimizer_switch;
  184. set optimizer_switch='index_condition_pushdown=off';
  185. EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue
  186. from customer, orders, lineitem, supplier, nation, region
  187. where c_custkey = o_custkey and l_orderkey = o_orderkey
  188. and l_suppkey = s_suppkey and c_nationkey = s_nationkey
  189. and s_nationkey = n_nationkey and n_regionkey = r_regionkey
  190. and r_name = 'AMERICA' and o_orderdate >= date '1995-01-01'
  191. and o_orderdate < date '1995-01-01' + interval '1' year
  192. group by n_name
  193. order by revenue desc;
  194. id select_type table type possible_keys key key_len ref rows Extra
  195. 1 SIMPLE orders range PRIMARY,i_o_orderdate,i_o_custkey i_o_orderdate 4 NULL 211 Using where; Using temporary; Using filesort
  196. 1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
  197. 1 SIMPLE nation eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1
  198. 1 SIMPLE supplier ref PRIMARY,i_s_nationkey i_s_nationkey 5 dbt3_s001.customer.c_nationkey 1 Using index
  199. 1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join)
  200. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where
  201. 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. n_name revenue
  211. PERU 321915.8715
  212. ARGENTINA 69817.1451
  213. set optimizer_switch=@save_optimizer_switch;
  214. EXPLAIN select o_year,
  215. sum(case when nation = 'UNITED STATES' then volume else 0 end) /
  216. sum(volume) as mkt_share
  217. from (select extract(year from o_orderdate) as o_year,
  218. l_extendedprice * (1-l_discount) as volume,
  219. n2.n_name as nation
  220. from part, supplier, lineitem, orders, customer,
  221. nation n1, nation n2, region
  222. where p_partkey = l_partkey and s_suppkey = l_suppkey
  223. and l_orderkey = o_orderkey and o_custkey = c_custkey
  224. and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey
  225. and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey
  226. and o_orderdate between date '1995-01-01' and date '1996-12-31'
  227. and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations
  228. group by o_year
  229. order by o_year;
  230. id select_type table type possible_keys key key_len ref rows Extra
  231. 1 SIMPLE orders ALL PRIMARY,i_o_orderdate,i_o_custkey NULL NULL NULL 1500 Using where; Using temporary; Using filesort
  232. 1 SIMPLE customer eq_ref PRIMARY,i_c_nationkey PRIMARY 4 dbt3_s001.orders.o_custkey 1 Using where
  233. 1 SIMPLE n1 eq_ref PRIMARY,i_n_regionkey PRIMARY 4 dbt3_s001.customer.c_nationkey 1
  234. 1 SIMPLE region ALL PRIMARY NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join)
  235. 1 SIMPLE lineitem ref PRIMARY,i_l_suppkey_partkey,i_l_partkey,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity PRIMARY 4 dbt3_s001.orders.o_orderkey 4 Using where
  236. 1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_partkey 1 Using where
  237. 1 SIMPLE supplier eq_ref PRIMARY,i_s_nationkey PRIMARY 4 dbt3_s001.lineitem.l_suppkey 1 Using where
  238. 1 SIMPLE n2 eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1
  239. select o_year,
  240. sum(case when nation = 'UNITED STATES' then volume else 0 end) /
  241. sum(volume) as mkt_share
  242. from (select extract(year from o_orderdate) as o_year,
  243. l_extendedprice * (1-l_discount) as volume,
  244. n2.n_name as nation
  245. from part, supplier, lineitem, orders, customer,
  246. nation n1, nation n2, region
  247. where p_partkey = l_partkey and s_suppkey = l_suppkey
  248. and l_orderkey = o_orderkey and o_custkey = c_custkey
  249. and c_nationkey = n1.n_nationkey and n1.n_regionkey = r_regionkey
  250. and r_name = 'AMERICA' and s_nationkey = n2.n_nationkey
  251. and o_orderdate between date '1995-01-01' and date '1996-12-31'
  252. and p_type = 'STANDARD BRUSHED STEEL' ) as all_nations
  253. group by o_year
  254. order by o_year;
  255. o_year mkt_share
  256. 1995 0.4495521838895718
  257. 1996 0.024585468215352495
  258. EXPLAIN select nation, o_year, sum(amount) as sum_profit
  259. from (select n_name as nation,
  260. extract(year from o_orderdate) as o_year,
  261. l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
  262. from part, supplier, lineitem, partsupp, orders, nation
  263. where s_suppkey = l_suppkey and ps_suppkey = l_suppkey
  264. and ps_partkey = l_partkey and p_partkey = l_partkey
  265. and o_orderkey = l_orderkey and s_nationkey = n_nationkey
  266. and p_name like '%green%') as profit
  267. group by nation, o_year
  268. order by nation, o_year desc;
  269. id select_type table type possible_keys key key_len ref rows Extra
  270. 1 SIMPLE supplier index PRIMARY,i_s_nationkey i_s_nationkey 5 NULL 10 Using where; Using index; Using temporary; Using filesort
  271. 1 SIMPLE nation eq_ref PRIMARY PRIMARY 4 dbt3_s001.supplier.s_nationkey 1
  272. 1 SIMPLE partsupp ref PRIMARY,i_ps_partkey,i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 70
  273. 1 SIMPLE part eq_ref PRIMARY PRIMARY 4 dbt3_s001.partsupp.ps_partkey 1 Using where
  274. 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
  275. 1 SIMPLE orders eq_ref PRIMARY PRIMARY 4 dbt3_s001.lineitem.l_orderkey 1
  276. select nation, o_year, sum(amount) as sum_profit
  277. from (select n_name as nation,
  278. extract(year from o_orderdate) as o_year,
  279. l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
  280. from part, supplier, lineitem, partsupp, orders, nation
  281. where s_suppkey = l_suppkey and ps_suppkey = l_suppkey
  282. and ps_partkey = l_partkey and p_partkey = l_partkey
  283. and o_orderkey = l_orderkey and s_nationkey = n_nationkey
  284. and p_name like '%green%') as profit
  285. group by nation, o_year
  286. order by nation, o_year desc;
  287. nation o_year sum_profit
  288. ARGENTINA 1997 18247.873399999993
  289. ARGENTINA 1996 7731.089399999995
  290. ARGENTINA 1995 134490.5697
  291. ARGENTINA 1994 36767.101500000004
  292. ARGENTINA 1993 35857.08
  293. ARGENTINA 1992 35740
  294. ETHIOPIA 1998 2758.7801999999992
  295. ETHIOPIA 1997 19419.294599999997
  296. ETHIOPIA 1995 51231.87439999999
  297. ETHIOPIA 1994 3578.9478999999974
  298. ETHIOPIA 1992 1525.8234999999986
  299. IRAN 1998 37817.229600000006
  300. IRAN 1997 52643.77359999999
  301. IRAN 1996 70143.7761
  302. IRAN 1995 84094.58260000001
  303. IRAN 1994 18140.925599999995
  304. IRAN 1993 78655.1676
  305. IRAN 1992 87142.23960000002
  306. IRAQ 1998 22860.8082
  307. IRAQ 1997 93676.24359999999
  308. IRAQ 1996 45103.3242
  309. IRAQ 1994 36010.728599999995
  310. IRAQ 1993 33221.9399
  311. IRAQ 1992 47755.05900000001
  312. KENYA 1998 44194.831999999995
  313. KENYA 1997 57578.36259999999
  314. KENYA 1996 59195.90210000001
  315. KENYA 1995 79262.6278
  316. KENYA 1994 102360.66609999999
  317. KENYA 1993 128422.0196
  318. KENYA 1992 181517.2089
  319. MOROCCO 1998 41797.823199999984
  320. MOROCCO 1997 23685.801799999994
  321. MOROCCO 1996 62115.19579999998
  322. MOROCCO 1995 42442.64300000001
  323. MOROCCO 1994 48655.878000000004
  324. MOROCCO 1993 22926.744400000003
  325. MOROCCO 1992 32239.8088
  326. PERU 1998 86999.36459999997
  327. PERU 1997 121110.41070000001
  328. PERU 1996 177040.40759999995
  329. PERU 1995 122247.94520000002
  330. PERU 1994 88046.25329999998
  331. PERU 1993 49379.813799999996
  332. PERU 1992 80646.86050000001
  333. UNITED KINGDOM 1998 50577.25560000001
  334. UNITED KINGDOM 1997 114288.8605
  335. UNITED KINGDOM 1996 147684.46480000002
  336. UNITED KINGDOM 1995 225267.65759999998
  337. UNITED KINGDOM 1994 140595.5864
  338. UNITED KINGDOM 1993 322548.49210000003
  339. UNITED KINGDOM 1992 67747.88279999999
  340. UNITED STATES 1998 3957.0431999999996
  341. UNITED STATES 1997 94729.5704
  342. UNITED STATES 1996 79297.85670000002
  343. UNITED STATES 1995 62201.23360000001
  344. UNITED STATES 1994 43075.629899999985
  345. UNITED STATES 1993 27168.486199999996
  346. UNITED STATES 1992 34092.366
  347. set @save_optimizer_switch=@@optimizer_switch;
  348. set optimizer_switch='extended_keys=on';
  349. EXPLAIN select o_orderkey, p_partkey
  350. from part, lineitem, orders
  351. where p_retailprice > 1100 and o_orderdate='1997-01-01'
  352. and o_orderkey=l_orderkey and p_partkey=l_partkey;
  353. id select_type table type possible_keys key key_len ref rows Extra
  354. 1 SIMPLE part range PRIMARY,i_p_retailprice i_p_retailprice 9 NULL 1 Using where; Using index
  355. 1 SIMPLE orders ref PRIMARY,i_o_orderdate i_o_orderdate 4 const 1 Using index
  356. 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
  357. select o_orderkey, p_partkey
  358. from part, lineitem, orders
  359. where p_retailprice > 1100 and o_orderdate='1997-01-01'
  360. and o_orderkey=l_orderkey and p_partkey=l_partkey;
  361. o_orderkey p_partkey
  362. 5895 200
  363. set optimizer_switch=@save_optimizer_switch;
  364. DROP DATABASE dbt3_s001;
  365. use test;
  366. set use_stat_tables=@save_use_stat_tables;
  367. set optimizer_switch=@save_optimizer_switch_for_stat_tables_test;
  368. SET SESSION STORAGE_ENGINE=DEFAULT;