README.plan 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. -----------
  2. PLAN clause
  3. -----------
  4. Function:
  5. Allows to specify a used-supplied access path for a select-based SQL statement.
  6. Syntax rules:
  7. PLAN ( { <stream_retrieval> | <sorted_streams> | <joined_streams> } )
  8. <stream_retrieval> ::= { <natural_scan> | <indexed_retrieval> | <navigational_scan> }
  9. <natural_scan> ::= <stream_alias> NATURAL
  10. <indexed_retrieval> ::= <stream_alias> INDEX ( <index_name> [, <index_name> ...] )
  11. <navigational_scan> ::= <stream_alias> ORDER <index_name> [ INDEX ( <index_name> [, <index_name> ...] ) ]
  12. <sorted_streams> ::= SORT ( <stream_retrieval> )
  13. <joined_streams> ::= JOIN ( <stream_retrieval>, <stream_retrieval> [, <stream_retrieval> ...] )
  14. | [SORT] MERGE ( <sorted_streams>, <sorted_streams> )
  15. Description:
  16. Natural scan means that all rows are fetched in their natural storage order,
  17. which requires to read all pages and validate any search criteria afterward.
  18. Indexed retrieval uses an index range scan to find rowids which match the given
  19. search criteria. The found matches are combined in a sparse bitmap which is sorted
  20. by page numbers, so every data page will be read only once. After that the table
  21. pages are read and required rows are fetched from them.
  22. Navigational scan uses an index to return rows in the given order, if such an
  23. operation is appropriate. The index b-tree is walked from the leftmost node to the
  24. rightmost one. If any search criteria is used on a column being ordered by, then the
  25. navigation is limited to some subtree path, depending on a predicate. If any search
  26. criteria is used on other columns which are indexed, then a range index scan is performed
  27. in advance and every fetched key has its rowid validated against the resulting bitmap.
  28. Then a data page is read and required row is fetched. Note that navigational scan produces
  29. random page I/O as reads are not optimized.
  30. A sort operation performs an external sort of the given stream retrieval.
  31. A join can be performed either via the nested loops algorithm (JOIN plan) or via
  32. the sort merge algorithm (MERGE plan). An inner nested loop join may contain as many
  33. streams as required to be joined (as all of them are equivalent), whilst an outer
  34. nested loops join always operates with two (outer and inner) streams, so you'll see
  35. nested JOIN clauses in the case of 3 or more outer streams joined. A sort merge operates
  36. with two input streams which are sorted beforehand, then they're merged in a single run.
  37. Example(s):
  38. SELECT RDB$RELATION_NAME
  39. FROM RDB$RELATIONS
  40. WHERE RDB$RELATION_NAME LIKE 'RDB$%'
  41. PLAN (RDB$RELATIONS NATURAL)
  42. ORDER BY RDB$RELATION_NAME
  43. SELECT R.RDB$RELATION_NAME, RF.RDB$FIELD_NAME
  44. FROM RDB$RELATIONS R
  45. JOIN RDB$RELATION_FIELDS RF ON R.RDB$RELATION_NAME = RF.RDB$RELATION_NAME
  46. PLAN MERGE (SORT (R NATURAL), SORT (RF NATURAL))
  47. Note(s):
  48. 1. A PLAN clause may be used in all select expressions, including subqueries,
  49. derived tables and view definitions. It can be also used in UPDATE and DELETE
  50. statements, because they're implicitly based on select expressions.
  51. 2. If a PLAN clause contains some invalid retrieval description, then either an
  52. error will be returned or this bad clause will be silently ignored, depending
  53. on severity of the issue.
  54. 3. ORDER <navigational_index> INDEX ( <filter_indices> ) kind of plan is reported
  55. by the engine and can be used in the user-supplied plans starting with FB 2.0.