The explicit specification of an index in a list query is actually slower, in what situations will it degrade to a full table scan?
The explicit index specification in list query is actually slower, the problem lies in the case when the hint points to an index that no longer matches the current query conditions. Hardcoding an index is equivalent to making the optimizer give up the right to choose the path based on statistical information: when the combination of conditions changes or the data distribution changes, the forced index may only have the usage of scanning back to the table, which is much more costly than the original covered index or range scan; when the conditions are completely invalid, this path degrades to a full table scan.
What does the optimizer choose the index based on?
Query the index to be used by the optimizer when no hint is provided, referring to the index structure, the distribution of field values, and the estimated number of rows hit. It does not pursue the 'most relevant index', but rather the 'total cost path'——including how many pages to read, whether to retrieve the rest of the fields row by row, and whether the result needs to be sorted again. An index that matches only by field name is not necessarily better than another index that includes sorted fields when the number of hit rows is large. Such judgments depend on statistical information and hints written statically in the code are not visible.
Who decides after the prompt is written in stone
The scope of the prompt has three different strengths of expression, all of which exclude other candidates. Once specified, the optimizer no longer compares costs and will not automatically change selections due to inappropriate indices.The risk point lies in the fact that the prompt is often added during a slow query: at that time, it indeed solved the problem, but the data volume, field distribution, and query conditions will continue to change, while the prompt will not change accordingly.PbootCMS in the V3.2.26 update log provides the fix for this type of issue, the query of the list tag degraded to full table scan due to index hints, and the new installed libraries are unifiedly changed to build indexes based on the title field - both sides are changed together, indicating that the cause is that the index structure is too tightly bound with the hints.
| Trigger condition | The result brought by the prompt | Phenomenon | Method of processing |
|---|---|---|---|
| Query added new filtering conditions | The specified index cannot be used for the new condition, scan first then filter | Response time increases exponentially | Remove the prompt first, let the optimizer choose by itself |
| The distribution of field values changes | Estimated number of rows hit increases, the cost of returning to the table increases | Fast for cold data, slow for hot data | Review the order of index fields |
| The sorting field is inconsistent with the index | Extra sorting is needed, the index is only used as a filter | 明显变慢 when performing large pagination | Rebuild index by combining sorting and filtering |
| Write the condition as a function or implicit conversion | Index column fails, directly degrade | Execution plan shows full table scan | Rewrite the condition, don't cover it with hints |
Which combination of conditions is most prone to degradation
The fourth type is the most common. **This type performs function operations or comparisons with inconsistent types on index columns, and the values on the columns must be converted to match, the index cannot be used to locate.The second type is a prefix wildcard fuzzy match, which cannot take advantage of the ordered structure when the pattern starts with a wildcard. The third type is filtering and sorting belonging to different indexes, the optimizer needs to choose between one index scan with sorting and another index scan without sorting, while the prompt will compress the choice space into one.The fourth type is a small field index on a wide table: when the number of matching rows is large, it is more expensive to retrieve the remaining fields row by row than to scan the entire table in order, which is exactly the situation where the forced index is most likely to fail.
The order of location and verification
The order should be to first look at the execution plan before making configuration changes, not relying on intuition or prompts. **Step 1: Take the current execution plan, confirm the actual used index, estimated row count, and whether there is additional sorting;Step two: remove the prompt and run it once again, compare the cost of the two runs; in many cases, the optimizer's chosen path is more optimal; Step three: check if the index column order covers the combination of filtering and sorting, and rebuild if necessary instead of adding hints;Observe the performance after the data volume changes in the fourth step, and change the writing that only applies to the current scale back to condition-driven. The efficiency of list queries has a direct impact on page loading speed, the official statement for AnQiCMS is that it has a significant improvement compared to traditional PHP programs, and can handle about 5 million PV on a single machine, these conclusions are about overall architectural capabilities, and still need to use execution plans to verify specific queries.AnQiCMS's data layer builds queries through GORM, index design should be considered together with field combinations and query conditions. The practice of hardcoding index names is more likely to become disconnected from the actual structure in this abstract layer.
Common questions
Q: After adding hints to the slow query, it is indeed faster, why should it be removed? A: That optimization solved the data distribution at that time. Keeping the hint will prevent the subsequent conditions from changing automatically, and manual review is required to avoid accidents.
How can you determine if it has degraded into a full table scan?\nAnswer: Look at the scan type and estimated row count in the execution plan, as well as whether it is accompanied by additional sorting; just looking at the response time will miss the differences caused by scale changes.
What does it mean to build a field index uniformly in the new library?\nAnswer: It means that the maintenance party tends to decouple the index structure from the query writing style, avoiding the tight binding of hints with the actual index.