You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -26,7 +26,7 @@ If the statistics of column `a` in table `t` are missing, the query will wait fo
26
26
27
27
Although it might seem straightforward to implement Sync Load by loading the statistics before executing the query, the actual implementation is more complex. TiDB uses a sync load handler to manage this process. The sync load handler is a singleton in the TiDB server, responsible for loading statistics synchronously. When a query is executed, it submits a load request to the sync load handler. The handler then loads the statistics, and the optimizer checks if the statistics are available before proceeding with the query execution.
28
28
29
-
In TiDB, the query execution process is divided into two stages: optimization and execution. The optimization stage generates the execution plan, while the execution stage executes the plan. During optimization, the optimizer checks if the necessary statistics are available. If not, it submits a load request to the sync load handler. This process occurs during the logical optimization phase, specifically in the `CollectPredicateColumnsPoint` and `SyncWaitStatsLoadPoint` steps.
29
+
In TiDB, the query execution process is divided into two stages: optimization and execution. During optimization, the optimizer checks if the necessary statistics are available. If not, it submits a load request to the sync load handler. This process occurs during the logical optimization phase, specifically in the `CollectPredicateColumnsPoint` and `SyncWaitStatsLoadPoint` steps.
30
30
31
31
`CollectPredicateColumnsPoint` collects columns used in the query that require statistics and sends a request to the sync load handler. `SyncWaitStatsLoadPoint` waits for the statistics to be loaded, ensuring that all necessary statistics are available before the query execution proceeds.
32
32
@@ -66,28 +66,181 @@ type StatsLoad struct {
66
66
TimeoutItemsChchan *NeededItemTask
67
67
sync.Mutex
68
68
}
69
+
```
70
+
71
+
The core of the sync load handler is the `StatsLoad` structure, which contains two channels: `NeededItemsCh` and `TimeoutItemsCh`. The `NeededItemsCh` channel is used to submit load requests, while the `TimeoutItemsCh` channel is used to handle timeout events.
72
+
73
+
The handler implementation can be divided into three parts:
74
+
1. Send requests to the handler
75
+
2. Load statistics concurrently
76
+
3. Wait for the statistics to be loaded
69
77
70
-
typeNeededItemTaskstruct {
71
-
ToTimeout time.Time
72
-
ResultChchan stmtctx.StatsLoadResult
73
-
Item model.StatsLoadItem
74
-
Retryint
78
+
`statsSyncLoad` provides the `SendLoadRequests` method to allow the optimizer to send load requests to the handler.
The `SendLoadRequests` method first filters out columns that have already been loaded or are unnecessary. For the remaining columns, it:
76
118
77
-
typeTableItemIDstruct {
78
-
TableIDint64
79
-
IDint64
80
-
IsIndexbool
81
-
IsSyncLoadFailedbool
119
+
1. Creates a `NeededItemTask` for each column/index requiring statistics
120
+
2. Sends these tasks to the `NeededItemsCh` channel
121
+
3. Includes in each task:
122
+
- Column/index information
123
+
- A `ResultCh` channel for receiving loading results
124
+
- Timeout settings
125
+
126
+
This design ensures efficient handling of statistics loading requests while preventing duplicate loads for the different queries from different sessions.
127
+
128
+
A thing to note is that we maintain the `ResultCh` and `NeededItems` in the `stmtctx.StatementContext` to keep track of the loading status for each statement from each session. This is a key point to track the loading status for each statement (query).
129
+
130
+
After sending the load tasks, the optimizer waits for the statistics to be loaded. This process is implemented in the `WaitLoadFinished` method.
The `SyncWaitStatsLoad` method monitors the loading progress of statistics. It processes results from the `ResultCh` channels and handles any potential errors that occur during loading. The method operates under the fundamental premise that each result channel will receive exactly one result, and it will continue waiting until either:
183
+
184
+
1. All results are successfully received
185
+
2. A timeout occurs
83
186
84
-
typeStatsLoadItemstruct {
85
-
TableItemID
86
-
FullLoadbool
187
+
To handle the tasks, `statsSyncLoad` utilizes multiple sub-workers to load statistics concurrently. This design ensures efficient processing without blocking query execution.
From the code snippet above, we can see that the `SubLoadWorker` function is responsible for loading statistics concurrently. It processes tasks from the `NeededItemsCh` channel, loading statistics for each task. The worker continues processing until all statistics are loaded or the worker is terminated. Additionally, it incorporates a retry mechanism to handle potential errors during the loading process.
The `HandleOneTask` function processes a single task from either the `NeededItemsCh` or `TimeoutItemsCh` channel. It loads statistics for the task and sends the result back to the ResultCh channel. If an error occurs during the loading process, the function checks if the task is eligible for a retry. If so, the task is returned for retry; otherwise, the error is sent back to the `ResultCh` channel. The default retry limit is set to 2 attempts.
0 commit comments