0

我在下面有一个详细的 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%')
                );

收到所有建议。

4

1 回答 1

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如果你在这些表之间有一个特定的关系,应用那个,并为你的列设置适当的索引

于 2013-10-25T19:26:22.580 回答