hint

package
v1.1.0-beta.0...-a878e1f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TiDBMergeJoin is hint enforce merge join.
	TiDBMergeJoin = "tidb_smj"
	// HintSMJ is hint enforce merge join.
	HintSMJ = "merge_join"
	// HintNoMergeJoin is the hint to enforce the query not to use merge join.
	HintNoMergeJoin = "no_merge_join"

	// TiDBBroadCastJoin indicates applying broadcast join by force.
	TiDBBroadCastJoin = "tidb_bcj"
	// HintBCJ indicates applying broadcast join by force.
	HintBCJ = "broadcast_join"
	// HintShuffleJoin indicates applying shuffle join by force.
	HintShuffleJoin = "shuffle_join"

	// HintStraightJoin causes TiDB to join tables in the order in which they appear in the FROM clause.
	HintStraightJoin = "straight_join"
	// HintLeading specifies the set of tables to be used as the prefix in the execution plan.
	HintLeading = "leading"

	// TiDBIndexNestedLoopJoin is hint enforce index nested loop join.
	TiDBIndexNestedLoopJoin = "tidb_inlj"
	// HintINLJ is hint enforce index nested loop join.
	HintINLJ = "inl_join"
	// HintINLHJ is hint enforce index nested loop hash join.
	HintINLHJ = "inl_hash_join"
	// HintINLMJ is hint enforce index nested loop merge join.
	HintINLMJ = "inl_merge_join"
	// HintNoIndexJoin is the hint to enforce the query not to use index join.
	HintNoIndexJoin = "no_index_join"
	// HintNoIndexHashJoin is the hint to enforce the query not to use index hash join.
	HintNoIndexHashJoin = "no_index_hash_join"
	// HintNoIndexMergeJoin is the hint to enforce the query not to use index merge join.
	HintNoIndexMergeJoin = "no_index_merge_join"
	// TiDBHashJoin is hint enforce hash join.
	TiDBHashJoin = "tidb_hj"
	// HintNoHashJoin is the hint to enforce the query not to use hash join.
	HintNoHashJoin = "no_hash_join"
	// HintHJ is hint enforce hash join.
	HintHJ = "hash_join"
	// HintHashJoinBuild is hint enforce hash join's build side
	HintHashJoinBuild = "hash_join_build"
	// HintHashJoinProbe is hint enforce hash join's probe side
	HintHashJoinProbe = "hash_join_probe"
	// HintHashAgg is hint enforce hash aggregation.
	HintHashAgg = "hash_agg"
	// HintStreamAgg is hint enforce stream aggregation.
	HintStreamAgg = "stream_agg"
	// HintMPP1PhaseAgg enforces the optimizer to use the mpp-1phase aggregation.
	HintMPP1PhaseAgg = "mpp_1phase_agg"
	// HintMPP2PhaseAgg enforces the optimizer to use the mpp-2phase aggregation.
	HintMPP2PhaseAgg = "mpp_2phase_agg"
	// HintUseIndex is hint enforce using some indexes.
	HintUseIndex = "use_index"
	// HintIgnoreIndex is hint enforce ignoring some indexes.
	HintIgnoreIndex = "ignore_index"
	// HintForceIndex make optimizer to use this index even if it thinks a table scan is more efficient.
	HintForceIndex = "force_index"
	// HintOrderIndex is hint enforce using some indexes and keep the index's order.
	HintOrderIndex = "order_index"
	// HintNoOrderIndex is hint enforce using some indexes and not keep the index's order.
	HintNoOrderIndex = "no_order_index"
	// HintAggToCop is hint enforce pushing aggregation to coprocessor.
	HintAggToCop = "agg_to_cop"
	// HintReadFromStorage is hint enforce some tables read from specific type of storage.
	HintReadFromStorage = "read_from_storage"
	// HintTiFlash is a label represents the tiflash storage type.
	HintTiFlash = "tiflash"
	// HintTiKV is a label represents the tikv storage type.
	HintTiKV = "tikv"
	// HintIndexMerge is a hint to enforce using some indexes at the same time.
	HintIndexMerge = "use_index_merge"
	// HintTimeRange is a hint to specify the time range for metrics summary tables
	HintTimeRange = "time_range"
	// HintIgnorePlanCache is a hint to enforce ignoring plan cache
	HintIgnorePlanCache = "ignore_plan_cache"
	// HintLimitToCop is a hint enforce pushing limit or topn to coprocessor.
	HintLimitToCop = "limit_to_cop"
	// HintMerge is a hint which can switch turning inline for the CTE.
	HintMerge = "merge"
	// HintSemiJoinRewrite is a hint to force we rewrite the semi join operator as much as possible.
	HintSemiJoinRewrite = "semi_join_rewrite"
	// HintNoDecorrelate indicates a LogicalApply not to be decorrelated.
	HintNoDecorrelate = "no_decorrelate"

	// HintMemoryQuota sets the memory limit for a query
	HintMemoryQuota = "memory_quota"
	// HintUseToja is a hint to optimize `in (select ...)` subquery into `join`
	HintUseToja = "use_toja"
	// HintNoIndexMerge is a hint to disable index merge
	HintNoIndexMerge = "no_index_merge"
	// HintMaxExecutionTime specifies the max allowed execution time in milliseconds
	HintMaxExecutionTime = "max_execution_time"

	// HintFlagSemiJoinRewrite corresponds to HintSemiJoinRewrite.
	HintFlagSemiJoinRewrite uint64 = 1 << iota
	// HintFlagNoDecorrelate corresponds to HintNoDecorrelate.
	HintFlagNoDecorrelate
)

Hint flags listed here are used by PlanBuilder.subQueryHintFlags.

View Source
const (
	// PreferINLJ indicates that the optimizer prefers to use index nested loop join.
	PreferINLJ uint = 1 << iota
	// PreferINLHJ indicates that the optimizer prefers to use index nested loop hash join.
	PreferINLHJ
	// PreferINLMJ indicates that the optimizer prefers to use index nested loop merge join.
	PreferINLMJ
	// PreferHJBuild indicates that the optimizer prefers to use hash join.
	PreferHJBuild
	// PreferHJProbe indicates that the optimizer prefers to use hash join.
	PreferHJProbe
	// PreferHashJoin indicates that the optimizer prefers to use hash join.
	PreferHashJoin
	// PreferNoHashJoin indicates that the optimizer prefers not to use hash join.
	PreferNoHashJoin
	// PreferMergeJoin indicates that the optimizer prefers to use merge join.
	PreferMergeJoin
	// PreferNoMergeJoin indicates that the optimizer prefers not to use merge join.
	PreferNoMergeJoin
	// PreferNoIndexJoin indicates that the optimizer prefers not to use index join.
	PreferNoIndexJoin
	// PreferNoIndexHashJoin indicates that the optimizer prefers not to use index hash join.
	PreferNoIndexHashJoin
	// PreferNoIndexMergeJoin indicates that the optimizer prefers not to use index merge join.
	PreferNoIndexMergeJoin
	// PreferBCJoin indicates that the optimizer prefers to use broadcast join.
	PreferBCJoin
	// PreferShuffleJoin indicates that the optimizer prefers to use shuffle join.
	PreferShuffleJoin
	// PreferRewriteSemiJoin indicates that the optimizer prefers to rewrite semi join.
	PreferRewriteSemiJoin

	// PreferLeftAsINLJInner indicates that the optimizer prefers to use left child as inner child of index nested loop join.
	PreferLeftAsINLJInner
	// PreferRightAsINLJInner indicates that the optimizer prefers to use right child as inner child of index nested loop join.
	PreferRightAsINLJInner
	// PreferLeftAsINLHJInner indicates that the optimizer prefers to use left child as inner child of index nested loop hash join.
	PreferLeftAsINLHJInner
	// PreferRightAsINLHJInner indicates that the optimizer prefers to use right child as inner child of index nested loop hash join.
	PreferRightAsINLHJInner
	// PreferLeftAsINLMJInner indicates that the optimizer prefers to use left child as inner child of index nested loop merge join.
	PreferLeftAsINLMJInner
	// PreferRightAsINLMJInner indicates that the optimizer prefers to use right child as inner child of index nested loop merge join.
	PreferRightAsINLMJInner
	// PreferLeftAsHJBuild indicates that the optimizer prefers to use left child as build child of hash join.
	PreferLeftAsHJBuild
	// PreferRightAsHJBuild indicates that the optimizer prefers to use right child as build child of hash join.
	PreferRightAsHJBuild
	// PreferLeftAsHJProbe indicates that the optimizer prefers to use left child as probe child of hash join.
	PreferLeftAsHJProbe
	// PreferRightAsHJProbe indicates that the optimizer prefers to use right child as probe child of hash join.
	PreferRightAsHJProbe

	// PreferHashAgg indicates that the optimizer prefers to use hash aggregation.
	PreferHashAgg
	// PreferStreamAgg indicates that the optimizer prefers to use stream aggregation.
	PreferStreamAgg
	// PreferMPP1PhaseAgg indicates that the optimizer prefers to use 1-phase aggregation.
	PreferMPP1PhaseAgg
	// PreferMPP2PhaseAgg indicates that the optimizer prefers to use 2-phase aggregation.
	PreferMPP2PhaseAgg
)
View Source
const (
	// PreferTiKV indicates that the optimizer prefers to use TiKV layer.
	PreferTiKV = 1 << iota
	// PreferTiFlash indicates that the optimizer prefers to use TiFlash layer.
	PreferTiFlash
)

Variables

View Source
var ErrWarnConflictingHint = dbterror.ClassOptimizer.NewStd(mysql.ErrWarnConflictingHint)

ErrWarnConflictingHint is a warning error.

Functions

func BindHint

func BindHint(stmt ast.StmtNode, hintsSet *HintsSet) ast.StmtNode

BindHint will add hints for stmt according to the hints in `hintsSet`.

func CheckBindingFromHistoryComplete

func CheckBindingFromHistoryComplete(node ast.Node, hintStr string) (complete bool, reason string)

CheckBindingFromHistoryComplete checks whether the ast and hint string from history is complete. For these complex queries, the auto-generated binding might be not complete: 1. query use tiFlash engine 2. query with sub query 3. query with more than 2 table join

func CollectUnmatchedHintWarnings

func CollectUnmatchedHintWarnings(hintInfo *PlanHints) (warnings []string)

CollectUnmatchedHintWarnings collects warnings for unmatched hints from this TableHintInfo.

func ExtractTableHintsFromStmtNode

func ExtractTableHintsFromStmtNode(node ast.Node, warnHandler hintWarnHandler) []*ast.TableOptimizerHint

ExtractTableHintsFromStmtNode extracts table hints from this node.

func ExtractUnmatchedTables

func ExtractUnmatchedTables(hintTables []HintedTable) []string

ExtractUnmatchedTables extracts unmatched tables from hintTables.

func GenerateQBName

func GenerateQBName(nodeType NodeType, qbOffset int) (model.CIStr, error)

GenerateQBName builds QBName from offset.

func RemoveDuplicatedHints

func RemoveDuplicatedHints(hints []*ast.TableOptimizerHint) []*ast.TableOptimizerHint

RemoveDuplicatedHints removes duplicated hints in this hit list.

func Restore2IndexHint

func Restore2IndexHint(hintType string, hintIndex HintedIndex) string

Restore2IndexHint restores index hint to string.

func Restore2JoinHint

func Restore2JoinHint(hintType string, hintTables []HintedTable) string

Restore2JoinHint restores join hint to string.

func Restore2StorageHint

func Restore2StorageHint(tiflashTables, tikvTables []HintedTable) string

Restore2StorageHint restores storage hint to string.

func RestoreIndexHint

func RestoreIndexHint(hint *ast.IndexHint) (string, error)

RestoreIndexHint returns string format of IndexHint.

func RestoreOptimizerHints

func RestoreOptimizerHints(hints []*ast.TableOptimizerHint) string

RestoreOptimizerHints restores these hints.

func RestoreTableOptimizerHint

func RestoreTableOptimizerHint(hint *ast.TableOptimizerHint) string

RestoreTableOptimizerHint returns string format of TableOptimizerHint.

Types

type HintedIndex

type HintedIndex struct {
	DBName     model.CIStr    // the database name
	TblName    model.CIStr    // the table name
	Partitions []model.CIStr  // partition information
	IndexHint  *ast.IndexHint // the original parser index hint structure
	// Matched indicates whether this index hint
	// has been successfully applied to a DataSource.
	// If an HintedIndex is not Matched after building
	// a Select statement, we will generate a warning for it.
	Matched bool
}

HintedIndex indicates which index this hint should take effect on.

func (*HintedIndex) HintTypeString

func (hint *HintedIndex) HintTypeString() string

HintTypeString returns the string representation of the hint type.

func (*HintedIndex) IndexString

func (hint *HintedIndex) IndexString() string

IndexString formats the IndexHint as DBName.tableName[, indexNames].

func (*HintedIndex) Match

func (hint *HintedIndex) Match(dbName, tblName model.CIStr) bool

Match checks whether the hint is matched with the given dbName and tblName.

type HintedTable

type HintedTable struct {
	DBName       model.CIStr   // the database name
	TblName      model.CIStr   // the table name
	Partitions   []model.CIStr // partition information
	SelectOffset int           // the select block offset of this hint
	Matched      bool          // whether this hint is applied successfully
}

HintedTable indicates which table this hint should take effect on.

type HintsSet

type HintsSet struct {
	// contains filtered or unexported fields
}

HintsSet contains all hints of a query.

func CollectHint

func CollectHint(in ast.StmtNode) *HintsSet

CollectHint collects hints for a statement.

func ParseHintsSet

func ParseHintsSet(p *parser.Parser, sql, charset, collation, db string) (*HintsSet, ast.StmtNode, []error, error)

ParseHintsSet parses a SQL string, then collects and normalizes the HintsSet.

func (*HintsSet) ContainTableHint

func (hs *HintsSet) ContainTableHint(hint string) bool

ContainTableHint checks whether the table hint set contains a hint.

func (*HintsSet) GetStmtHints

func (hs *HintsSet) GetStmtHints() []*ast.TableOptimizerHint

GetStmtHints gets all statement-level hints.

func (*HintsSet) Restore

func (hs *HintsSet) Restore() (string, error)

Restore returns the string format of HintsSet.

type IndexJoinHints

type IndexJoinHints struct {
	INLJTables  []HintedTable
	INLHJTables []HintedTable
	INLMJTables []HintedTable
}

IndexJoinHints stores hint information about index nested loop join.

type NodeType

type NodeType int

NodeType indicates if the node is for SELECT / UPDATE / DELETE.

const (
	// TypeUpdate for Update.
	TypeUpdate NodeType = iota
	// TypeDelete for DELETE.
	TypeDelete
	// TypeSelect for SELECT.
	TypeSelect
	// TypeInvalid for unexpected statements.
	TypeInvalid
)

type PlanHints

type PlanHints struct {
	IndexJoin          IndexJoinHints // inlj_join, inlhj_join, inlmj_join
	NoIndexJoin        IndexJoinHints // no_inlj_join, no_inlhj_join, no_inlmj_join
	HashJoin           []HintedTable  // hash_join
	NoHashJoin         []HintedTable  // no_hash_join
	SortMergeJoin      []HintedTable  // merge_join
	NoMergeJoin        []HintedTable  // no_merge_join
	BroadcastJoin      []HintedTable  // bcj_join
	ShuffleJoin        []HintedTable  // shuffle_join
	IndexHintList      []HintedIndex  // use_index, ignore_index
	IndexMergeHintList []HintedIndex  // use_index_merge
	TiFlashTables      []HintedTable  // isolation_read_engines(xx=tiflash)
	TiKVTables         []HintedTable  // isolation_read_engines(xx=tikv)
	LeadingJoinOrder   []HintedTable  // leading
	HJBuild            []HintedTable  // hash_join_build
	HJProbe            []HintedTable  // hash_join_probe

	// Hints belows are not associated with any particular table.
	PreferAggType    uint // hash_agg, merge_agg, agg_to_cop and so on
	PreferAggToCop   bool
	PreferLimitToCop bool // limit_to_cop
	CTEMerge         bool // merge
	TimeRangeHint    ast.HintTimeRange
}

PlanHints are hints that are used to control the optimizer plan choices like 'use_index', 'hash_join'. TODO: move ignore_plan_cache, straight_join, no_decorrelate here.

func ParsePlanHints

func ParsePlanHints(hints []*ast.TableOptimizerHint,
	currentLevel int, currentDB string,
	hintProcessor *QBHintHandler, straightJoinOrder bool,
	handlingExistsSubquery, notHandlingSubquery bool,
	warnHandler hintWarnHandler) (p *PlanHints, subQueryHintFlags uint64, err error)

ParsePlanHints parses *ast.TableOptimizerHint to PlanHints.

func (*PlanHints) IfPreferBroadcastJoin

func (pHints *PlanHints) IfPreferBroadcastJoin(tableNames ...*HintedTable) bool

IfPreferBroadcastJoin checks whether the join hint is broadcast join.

func (*PlanHints) IfPreferHJBuild

func (pHints *PlanHints) IfPreferHJBuild(tableNames ...*HintedTable) bool

IfPreferHJBuild checks whether the join hint is hash join build side.

func (*PlanHints) IfPreferHJProbe

func (pHints *PlanHints) IfPreferHJProbe(tableNames ...*HintedTable) bool

IfPreferHJProbe checks whether the join hint is hash join probe side.

func (*PlanHints) IfPreferHashJoin

func (pHints *PlanHints) IfPreferHashJoin(tableNames ...*HintedTable) bool

IfPreferHashJoin checks whether the join hint is hash join.

func (*PlanHints) IfPreferINLHJ

func (pHints *PlanHints) IfPreferINLHJ(tableNames ...*HintedTable) bool

IfPreferINLHJ checks whether the join hint is index nested loop hash join.

func (*PlanHints) IfPreferINLJ

func (pHints *PlanHints) IfPreferINLJ(tableNames ...*HintedTable) bool

IfPreferINLJ checks whether the join hint is index nested loop join.

func (*PlanHints) IfPreferINLMJ

func (pHints *PlanHints) IfPreferINLMJ(tableNames ...*HintedTable) bool

IfPreferINLMJ checks whether the join hint is index nested loop merge join.

func (*PlanHints) IfPreferMergeJoin

func (pHints *PlanHints) IfPreferMergeJoin(tableNames ...*HintedTable) bool

IfPreferMergeJoin checks whether the join hint is merge join.

func (*PlanHints) IfPreferNoHashJoin

func (pHints *PlanHints) IfPreferNoHashJoin(tableNames ...*HintedTable) bool

IfPreferNoHashJoin checks whether the join hint is no hash join.

func (*PlanHints) IfPreferNoIndexHashJoin

func (pHints *PlanHints) IfPreferNoIndexHashJoin(tableNames ...*HintedTable) bool

IfPreferNoIndexHashJoin checks whether the join hint is no index hash join.

func (*PlanHints) IfPreferNoIndexJoin

func (pHints *PlanHints) IfPreferNoIndexJoin(tableNames ...*HintedTable) bool

IfPreferNoIndexJoin checks whether the join hint is no index join.

func (*PlanHints) IfPreferNoIndexMergeJoin

func (pHints *PlanHints) IfPreferNoIndexMergeJoin(tableNames ...*HintedTable) bool

IfPreferNoIndexMergeJoin checks whether the join hint is no index merge join.

func (*PlanHints) IfPreferNoMergeJoin

func (pHints *PlanHints) IfPreferNoMergeJoin(tableNames ...*HintedTable) bool

IfPreferNoMergeJoin checks whether the join hint is no merge join.

func (*PlanHints) IfPreferShuffleJoin

func (pHints *PlanHints) IfPreferShuffleJoin(tableNames ...*HintedTable) bool

IfPreferShuffleJoin checks whether the join hint is shuffle join.

func (*PlanHints) IfPreferTiFlash

func (pHints *PlanHints) IfPreferTiFlash(tableName *HintedTable) *HintedTable

IfPreferTiFlash checks whether the hint hit the need of TiFlash.

func (*PlanHints) IfPreferTiKV

func (pHints *PlanHints) IfPreferTiKV(tableName *HintedTable) *HintedTable

IfPreferTiKV checks whether the hint hit the need of TiKV.

func (*PlanHints) MatchTableName

func (*PlanHints) MatchTableName(tables []*HintedTable, hintTables []HintedTable) bool

MatchTableName checks whether the hint hit the need. Only need either side matches one on the list. Even though you can put 2 tables on the list, it doesn't mean optimizer will reorder to make them join directly. Which it joins on with depend on sequence of traverse and without reorder, user might adjust themselves. This is similar to MySQL hints.

type QBHintHandler

type QBHintHandler struct {
	QBNameToSelOffset map[string]int                    // map[QBName]SelectOffset
	QBOffsetToHints   map[int][]*ast.TableOptimizerHint // map[QueryBlockOffset]Hints

	// Used for the view's hint
	ViewQBNameToTable map[string][]ast.HintTable           // map[QBName]HintedTable
	ViewQBNameToHints map[string][]*ast.TableOptimizerHint // map[QBName]Hints
	ViewQBNameUsed    map[string]struct{}                  // map[QBName]Used
	// contains filtered or unexported fields
}

QBHintHandler is used to handle hints at different query blocks. See the 2 examples below: 1) `select /*+ use_index(@sel_2 t2, a) */ * from t1, (select a*2 as b from t2) tx where a>b`; 2) `select /*+ use_index(@qb_xxx t2, a) */ * from t1, (select /*+ qb_name(qb_xxx) */ a*2 as b from t2) tx where a>b`; In both cases, the `use_index` hint doesn't take effect directly, since a specific qb_name is specified, and this QBHintHandler is used to handle this cases.

func NewQBHintHandler

func NewQBHintHandler(warnHandler hintWarnHandler) *QBHintHandler

NewQBHintHandler creates a QBHintHandler.

func (*QBHintHandler) Enter

func (p *QBHintHandler) Enter(in ast.Node) (ast.Node, bool)

Enter implements Visitor interface.

func (*QBHintHandler) GetCurrentStmtHints

func (p *QBHintHandler) GetCurrentStmtHints(hints []*ast.TableOptimizerHint, currentOffset int) []*ast.TableOptimizerHint

GetCurrentStmtHints extracts all hints that take effects at current stmt.

func (*QBHintHandler) GetHintOffset

func (p *QBHintHandler) GetHintOffset(qbName model.CIStr, currentOffset int) int

GetHintOffset gets the offset of stmt that the hints take effects.

func (*QBHintHandler) HandleUnusedViewHints

func (p *QBHintHandler) HandleUnusedViewHints()

HandleUnusedViewHints handle the unused view hints.

func (*QBHintHandler) Leave

func (*QBHintHandler) Leave(in ast.Node) (ast.Node, bool)

Leave implements Visitor interface.

func (*QBHintHandler) MaxSelectStmtOffset

func (p *QBHintHandler) MaxSelectStmtOffset() int

MaxSelectStmtOffset returns the current stmt offset.

type StmtHints

type StmtHints struct {
	// Hint Information
	MemQuotaQuery           int64
	MaxExecutionTime        uint64
	ReplicaRead             byte
	AllowInSubqToJoinAndAgg bool
	NoIndexMergeHint        bool
	StraightJoinOrder       bool
	// EnableCascadesPlanner is use cascades planner for a single query only.
	EnableCascadesPlanner bool
	// ForceNthPlan indicates the PlanCounterTp number for finding physical plan.
	// -1 for disable.
	ForceNthPlan  int64
	ResourceGroup string

	// Hint flags
	HasAllowInSubqToJoinAndAggHint bool
	HasMemQuotaHint                bool
	HasReplicaReadHint             bool
	HasMaxExecutionTime            bool
	HasEnableCascadesPlannerHint   bool
	HasResourceGroup               bool
	SetVars                        map[string]string

	// the original table hints
	OriginalTableHints []*ast.TableOptimizerHint
}

StmtHints are hints that apply to the entire statement, like 'max_exec_time', 'memory_quota'.

func ParseStmtHints

func ParseStmtHints(hints []*ast.TableOptimizerHint,
	setVarHintChecker func(varName, hint string) (ok bool, warning error),
	replicaReadFollower byte) (
	stmtHints StmtHints, offs []int, warns []error)

ParseStmtHints parses statement hints.

func (*StmtHints) Clone

func (sh *StmtHints) Clone() *StmtHints

Clone the StmtHints struct and returns the pointer of the new one.

func (*StmtHints) TaskMapNeedBackUp

func (sh *StmtHints) TaskMapNeedBackUp() bool

TaskMapNeedBackUp indicates that whether we need to back up taskMap during physical optimizing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL