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.

339 lines
15 KiB

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