In a massive MySQL table with millions of rows, you may encounter the need to determine if a specific number appears in the last n rows. To do this efficiently, consider utilizing the following approach:
Starting with the concept proposed by @chaos, some crucial modifications enhance the query's performance:
With these modifications in place, the optimized query becomes:
SELECT `id` FROM ( SELECT `id`, `val` FROM `big_table` ORDER BY `id` DESC LIMIT $n ) AS t WHERE t.`val` = $certain_number;
This query efficiently searches for the occurrence of $certain_number within the last $n rows of the big_table, providing a fast and reliable solution for your data检索 needs.
The above is the detailed content of How Can I Efficiently Search for a Number in the Last N Rows of a Large MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!