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.

395 lines
12 KiB

  1. #include "mysql_priv.h"
  2. #include "sql_select.h"
  3. /****************************************************************************
  4. * Index Condition Pushdown code starts
  5. ***************************************************************************/
  6. /*
  7. Check if given expression uses only table fields covered by the given index
  8. SYNOPSIS
  9. uses_index_fields_only()
  10. item Expression to check
  11. tbl The table having the index
  12. keyno The index number
  13. other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
  14. DESCRIPTION
  15. Check if given expression only uses fields covered by index #keyno in the
  16. table tbl. The expression can use any fields in any other tables.
  17. The expression is guaranteed not to be AND or OR - those constructs are
  18. handled outside of this function.
  19. RETURN
  20. TRUE Yes
  21. FALSE No
  22. */
  23. bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno,
  24. bool other_tbls_ok)
  25. {
  26. if (item->const_item())
  27. return TRUE;
  28. /*
  29. Don't push down the triggered conditions. Nested outer joins execution
  30. code may need to evaluate a condition several times (both triggered and
  31. untriggered), and there is no way to put thi
  32. TODO: Consider cloning the triggered condition and using the copies for:
  33. 1. push the first copy down, to have most restrictive index condition
  34. possible
  35. 2. Put the second copy into tab->select_cond.
  36. */
  37. if (item->type() == Item::FUNC_ITEM &&
  38. ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
  39. return FALSE;
  40. if (!(item->used_tables() & tbl->map))
  41. return other_tbls_ok;
  42. Item::Type item_type= item->type();
  43. switch (item_type) {
  44. case Item::FUNC_ITEM:
  45. {
  46. /* This is a function, apply condition recursively to arguments */
  47. Item_func *item_func= (Item_func*)item;
  48. Item **child;
  49. Item **item_end= (item_func->arguments()) + item_func->argument_count();
  50. for (child= item_func->arguments(); child != item_end; child++)
  51. {
  52. if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
  53. return FALSE;
  54. }
  55. return TRUE;
  56. }
  57. case Item::COND_ITEM:
  58. {
  59. /*
  60. This is a AND/OR condition. Regular AND/OR clauses are handled by
  61. make_cond_for_index() which will chop off the part that can be
  62. checked with index. This code is for handling non-top-level AND/ORs,
  63. e.g. func(x AND y).
  64. */
  65. List_iterator<Item> li(*((Item_cond*)item)->argument_list());
  66. Item *item;
  67. while ((item=li++))
  68. {
  69. if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
  70. return FALSE;
  71. }
  72. return TRUE;
  73. }
  74. case Item::FIELD_ITEM:
  75. {
  76. Item_field *item_field= (Item_field*)item;
  77. if (item_field->field->table != tbl)
  78. return TRUE;
  79. /*
  80. The below is probably a repetition - the first part checks the
  81. other two, but let's play it safe:
  82. */
  83. return item_field->field->part_of_key.is_set(keyno) &&
  84. item_field->field->type() != MYSQL_TYPE_GEOMETRY &&
  85. item_field->field->type() != MYSQL_TYPE_BLOB;
  86. }
  87. case Item::REF_ITEM:
  88. return uses_index_fields_only(item->real_item(), tbl, keyno,
  89. other_tbls_ok);
  90. default:
  91. return FALSE; /* Play it safe, don't push unknown non-const items */
  92. }
  93. }
  94. #define ICP_COND_USES_INDEX_ONLY 10
  95. /*
  96. Get a part of the condition that can be checked using only index fields
  97. SYNOPSIS
  98. make_cond_for_index()
  99. cond The source condition
  100. table The table that is partially available
  101. keyno The index in the above table. Only fields covered by the index
  102. are available
  103. other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
  104. DESCRIPTION
  105. Get a part of the condition that can be checked when for the given table
  106. we have values only of fields covered by some index. The condition may
  107. refer to other tables, it is assumed that we have values of all of their
  108. fields.
  109. Example:
  110. make_cond_for_index(
  111. "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
  112. t2, keyno(t2.key1))
  113. will return
  114. "cond(t1.field) AND cond(t2.key2)"
  115. RETURN
  116. Index condition, or NULL if no condition could be inferred.
  117. */
  118. Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
  119. bool other_tbls_ok)
  120. {
  121. if (!cond)
  122. return NULL;
  123. if (cond->type() == Item::COND_ITEM)
  124. {
  125. uint n_marked= 0;
  126. if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
  127. {
  128. table_map used_tables= 0;
  129. Item_cond_and *new_cond=new Item_cond_and;
  130. if (!new_cond)
  131. return (COND*) 0;
  132. List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
  133. Item *item;
  134. while ((item=li++))
  135. {
  136. Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
  137. if (fix)
  138. {
  139. new_cond->argument_list()->push_back(fix);
  140. used_tables|= fix->used_tables();
  141. }
  142. if (test(item->marker == ICP_COND_USES_INDEX_ONLY))
  143. {
  144. n_marked++;
  145. item->marker= 0;
  146. }
  147. }
  148. if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
  149. cond->marker= ICP_COND_USES_INDEX_ONLY;
  150. switch (new_cond->argument_list()->elements) {
  151. case 0:
  152. return (COND*) 0;
  153. case 1:
  154. new_cond->used_tables_cache= used_tables;
  155. return new_cond->argument_list()->head();
  156. default:
  157. new_cond->quick_fix_field();
  158. new_cond->used_tables_cache= used_tables;
  159. return new_cond;
  160. }
  161. }
  162. else /* It's OR */
  163. {
  164. Item_cond_or *new_cond=new Item_cond_or;
  165. if (!new_cond)
  166. return (COND*) 0;
  167. List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
  168. Item *item;
  169. while ((item=li++))
  170. {
  171. Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
  172. if (!fix)
  173. return (COND*) 0;
  174. new_cond->argument_list()->push_back(fix);
  175. if (test(item->marker == ICP_COND_USES_INDEX_ONLY))
  176. {
  177. n_marked++;
  178. item->marker= 0;
  179. }
  180. }
  181. if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
  182. cond->marker= ICP_COND_USES_INDEX_ONLY;
  183. new_cond->quick_fix_field();
  184. new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
  185. new_cond->top_level_item();
  186. return new_cond;
  187. }
  188. }
  189. if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
  190. return (COND*) 0;
  191. cond->marker= ICP_COND_USES_INDEX_ONLY;
  192. return cond;
  193. }
  194. Item *make_cond_remainder(Item *cond, bool exclude_index)
  195. {
  196. if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
  197. return 0; /* Already checked */
  198. if (cond->type() == Item::COND_ITEM)
  199. {
  200. table_map tbl_map= 0;
  201. if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
  202. {
  203. /* Create new top level AND item */
  204. Item_cond_and *new_cond=new Item_cond_and;
  205. if (!new_cond)
  206. return (COND*) 0;
  207. List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
  208. Item *item;
  209. while ((item=li++))
  210. {
  211. Item *fix= make_cond_remainder(item, exclude_index);
  212. if (fix)
  213. {
  214. new_cond->argument_list()->push_back(fix);
  215. tbl_map |= fix->used_tables();
  216. }
  217. }
  218. switch (new_cond->argument_list()->elements) {
  219. case 0:
  220. return (COND*) 0;
  221. case 1:
  222. return new_cond->argument_list()->head();
  223. default:
  224. new_cond->quick_fix_field();
  225. ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
  226. return new_cond;
  227. }
  228. }
  229. else /* It's OR */
  230. {
  231. Item_cond_or *new_cond=new Item_cond_or;
  232. if (!new_cond)
  233. return (COND*) 0;
  234. List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
  235. Item *item;
  236. while ((item=li++))
  237. {
  238. Item *fix= make_cond_remainder(item, FALSE);
  239. if (!fix)
  240. return (COND*) 0;
  241. new_cond->argument_list()->push_back(fix);
  242. tbl_map |= fix->used_tables();
  243. }
  244. new_cond->quick_fix_field();
  245. ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
  246. new_cond->top_level_item();
  247. return new_cond;
  248. }
  249. }
  250. return cond;
  251. }
  252. /*
  253. Try to extract and push the index condition
  254. SYNOPSIS
  255. push_index_cond()
  256. tab A join tab that has tab->table->file and its condition
  257. in tab->select_cond
  258. keyno Index for which extract and push the condition
  259. DESCRIPTION
  260. Try to extract and push the index condition down to table handler
  261. */
  262. void push_index_cond(JOIN_TAB *tab, uint keyno)
  263. {
  264. DBUG_ENTER("push_index_cond");
  265. Item *idx_cond;
  266. bool do_index_cond_pushdown=
  267. ((tab->table->file->index_flags(keyno, 0, 1) &
  268. HA_DO_INDEX_COND_PUSHDOWN) &&
  269. optimizer_flag(tab->join->thd, OPTIMIZER_SWITCH_INDEX_COND_PUSHDOWN));
  270. /*
  271. Do not try index condition pushdown on indexes which have partially-covered
  272. columns. Unpacking from a column prefix into index tuple is not a supported
  273. operation in some engines, see e.g. MySQL BUG#42991.
  274. TODO: a better solution would be not to consider partially-covered columns
  275. as parts of the index and still produce/check index condition for
  276. fully-covered index columns.
  277. */
  278. KEY *key_info= tab->table->key_info + keyno;
  279. for (uint kp= 0; kp < key_info->key_parts; kp++)
  280. {
  281. if ((key_info->key_part[kp].key_part_flag & HA_PART_KEY_SEG))
  282. {
  283. do_index_cond_pushdown= FALSE;
  284. break;
  285. }
  286. }
  287. if (do_index_cond_pushdown)
  288. {
  289. DBUG_EXECUTE("where",
  290. print_where(tab->select_cond, "full cond", QT_ORDINARY););
  291. idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
  292. tab->icp_other_tables_ok);
  293. DBUG_EXECUTE("where",
  294. print_where(idx_cond, "idx cond", QT_ORDINARY););
  295. if (idx_cond)
  296. {
  297. Item *idx_remainder_cond= 0;
  298. tab->pre_idx_push_select_cond= tab->select->cond;
  299. /*
  300. For BKA cache we store condition to special BKA cache field
  301. because evaluation of the condition requires additional operations
  302. before the evaluation. This condition is used in
  303. JOIN_CACHE_BKA[_UNIQUE]::skip_index_tuple() functions.
  304. */
  305. if (tab->use_join_cache &&
  306. /*
  307. if cache is used then the value is TRUE only
  308. for BKA[_UNIQUE] cache (see check_join_cache_usage func).
  309. */
  310. tab->icp_other_tables_ok &&
  311. (idx_cond->used_tables() &
  312. ~(tab->table->map | tab->join->const_table_map)))
  313. tab->cache_idx_cond= idx_cond;
  314. else
  315. idx_remainder_cond= tab->table->file->idx_cond_push(keyno, idx_cond);
  316. /*
  317. Disable eq_ref's "lookup cache" if we've pushed down an index
  318. condition.
  319. TODO: This check happens to work on current ICP implementations, but
  320. there may exist a compliant implementation that will not work
  321. correctly with it. Sort this out when we stabilize the condition
  322. pushdown APIs.
  323. */
  324. if (idx_remainder_cond != idx_cond)
  325. tab->ref.disable_cache= TRUE;
  326. Item *row_cond= tab->idx_cond_fact_out ?
  327. make_cond_remainder(tab->select_cond, TRUE) :
  328. tab->pre_idx_push_select_cond;
  329. DBUG_EXECUTE("where",
  330. print_where(row_cond, "remainder cond", QT_ORDINARY););
  331. if (row_cond)
  332. {
  333. if (!idx_remainder_cond)
  334. tab->select_cond= row_cond;
  335. else
  336. {
  337. COND *new_cond= new Item_cond_and(row_cond, idx_remainder_cond);
  338. tab->select_cond= new_cond;
  339. tab->select_cond->quick_fix_field();
  340. ((Item_cond_and*)tab->select_cond)->used_tables_cache=
  341. row_cond->used_tables() | idx_remainder_cond->used_tables();
  342. }
  343. }
  344. else
  345. tab->select_cond= idx_remainder_cond;
  346. if (tab->select)
  347. {
  348. DBUG_EXECUTE("where",
  349. print_where(tab->select->cond,
  350. "select_cond",
  351. QT_ORDINARY););
  352. tab->select->cond= tab->select_cond;
  353. tab->select->pre_idx_push_select_cond= tab->pre_idx_push_select_cond;
  354. }
  355. }
  356. }
  357. DBUG_VOID_RETURN;
  358. }