优化我的三级MySQL查询

时间:2013-10-25 19:20:02

标签: mysql query-optimization

我有一个下面详细介绍的SQL,并希望有人可以帮助我优化这一点,因为我觉得它有点笨拙......感谢任何帮助,谢谢。

SELECT * FROM test.datalist
Where UserName Like '%SearchString%'
Or UserName In (
                Select Synonym
                From synonyms
                Where Synonym Like '%SearchString%'
                OR ListRef = (  Select ListRef 
                                From synonyms
                                Where Synonym Like '%SearchString%')
                );

感谢所有建议。

1 个答案:

答案 0 :(得分:0)

如果您已经与Where Synonym Like '%SearchString%'匹配,则子查询的下半部分没有任何意义,无法找到同一个表的ListRef来获取Synonym

Where Synonym Like '%SearchString%'
OR ListRef = (  Select ListRef 
                                From synonyms
                                Where Synonym Like '%SearchString%')

另外你可以尝试加入

SELECT * FROM datalist d
JOIN synonyms s ON (d.UserName =s.Synonym)    
Where d.UserName Like '%SearchString%' or s.Synonym Like '%SearchString%'

我已根据d.UserName =s.Synonym加入您的表格,如果您在这些表格之间存在特定关系,则应用该表格,并为您的列添加适当的索引