且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在mysql查询中高效使用索引

更新时间:2023-11-27 23:30:16

您可以尝试使用 Top-N 查询来查找第一个候选,然后将该候选仅应用于实际模式:

you could try a Top-N query to find the first candidate, and then apply that candidate only to the actual pattern:

select 1 
  from (select c1 
          from junk 
         where c1 <= 'fxg87698x84'
         order by c1 desc limit 1) tmp 
 where 'fxg87698x84' like concat(c1, '%');

top-n 查询应该使用 c1 上的常规索引.

the top-n query should use a regular index on c1.

编辑:在我的博客中更详细地解释了这一点:http://blog.fatalmind.com/2010/09/29/finding-the-best-match-with-a-top-n-query/

EDIT: Explained that in more detail in my blog: http://blog.fatalmind.com/2010/09/29/finding-the-best-match-with-a-top-n-query/