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

Abd*_*lah 4 mysql indexing performance

我的db在mysql v5.x上运行.我有一个表格T1有5列,C1列是主键.C1的类型为varchar(20).它包含大约2000行,其值如下:

fxg
axt3
tru56
and so on.. 
Run Code Online (Sandbox Code Playgroud)

现在我的应用程序的工作是读取输入数据并查找输入数据是否具有类似于表T1中列C1中的起始模式.例如:我的输入可能显示为:

    trx879478986
    fxg87698x84
    784xtr783utr
    axt3487ghty
... and so on
Run Code Online (Sandbox Code Playgroud)

所以对于上面的输入,我必须为'fxg87698x84'和'axt3487ghty'返回true,对其他输入则返回false.我使用的查询是:

select 1 from T1 where (? like concat(C1,'%'));
note: the ? is replaced by the input value got from the application.
Run Code Online (Sandbox Code Playgroud)

问题是我的输入很大(在30分钟内处理大约100万条记录),我的查询速度不够快.有关如何重写查询或强制使用索引的任何想法?即使我必须使用不同的对象结构,我也能做到,如果这有帮助的话.所以任何帮助将不胜感激.谢谢.

Mar*_*and 9

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

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

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

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