sql或类似的查询永远返回结果

时间:2017-08-08 03:18:00

标签: sql-server sql-like

这是我在where子句中使用OR LIKE的查询。

SELECT DISTINCT TOP 10 coalesce(cl2desc, hmdesc, gendesc, procdesc) as description,acctcode,uacs FROM 
hpatchrg as t4 INNER JOIN 
hcharge AS t5 ON t4.chargcode = t5.chrgcode LEFT JOIN 
hmisc AS t6 ON t6.hmcode = t4.itemcode AND t5.chrgtable = 'MISCE' LEFT JOIN 
hdmhdr AS t7 ON (t7.dmdcomb +''+ convert(varchar, t7.dmdctr)) = t4.itemcode AND t5.chrgtable = 'DRUGS' LEFT JOIN 
hdruggrp AS t8 ON t7.grpcode = t8.grpcode LEFT JOIN 
hgen AS t9 ON t9.gencode = t8.gencode LEFT JOIN 
hprocm AS t10 ON t4.itemcode = t10.proccode AND t5.chrgtable = 'EXAMI' LEFT JOIN 
hclass2 AS t11 ON t4.itemcode = t11.cl2comb AND t5.chrgtable = 'NONDR' 


WHERE 
(cl2desc LIKE '%cbc%') OR 
(hmdesc LIKE '%cbc%') OR
(gendesc LIKE '%cbc%') OR
(procdesc LIKE '%cbc%') 

除了我这样做时,这需要永远回报:

SELECT DISTINCT TOP 10 coalesce(cl2desc, hmdesc, gendesc, procdesc) as description,acctcode,uacs FROM 
hpatchrg as t4 INNER JOIN 
hcharge AS t5 ON t4.chargcode = t5.chrgcode LEFT JOIN 
hmisc AS t6 ON t6.hmcode = t4.itemcode AND t5.chrgtable = 'MISCE' LEFT JOIN 
hdmhdr AS t7 ON (t7.dmdcomb +''+ convert(varchar, t7.dmdctr)) = t4.itemcode AND t5.chrgtable = 'DRUGS' LEFT JOIN 
hdruggrp AS t8 ON t7.grpcode = t8.grpcode LEFT JOIN 
hgen AS t9 ON t9.gencode = t8.gencode LEFT JOIN 
hprocm AS t10 ON t4.itemcode = t10.proccode AND t5.chrgtable = 'EXAMI' LEFT JOIN 
hclass2 AS t11 ON t4.itemcode = t11.cl2comb AND t5.chrgtable = 'NONDR' 


WHERE 
(cl2desc LIKE '%cbc%')

还尝试更改(cl2desc LIKE '%cbc%')与其他值

我得到的结果非常快。

  

所以我假设问题出在where子句

中的OR LIKE

在where子句中使用LIKE进行OR的正确方法是什么

1 个答案:

答案 0 :(得分:1)

评论太久了。

嗯,对于你拥有的每一列在where子句中,它必须在每一行上进行匹配。因此,如果在where子句中每个表的行数,则取总和...这是检查逻辑的次数。除非您使用全文索引并使用CONTAINS之类的东西,否则不能使用索引。 我希望此查询速度慢。

仅使用尾随%表示您正在使用您提供的值搜索列值STARTS的位置。因此,引擎不必检查列中的所有可能位置。

同样使用TOP而不使用ORDER BY并不能保证每次返回的结果集都是相同的。因此,不使用ORDER BY意味着您不关心返回哪10行或任何数字。

相关问题