SQL:与OR对比LIKE

时间:2016-02-22 14:37:05

标签: sql oracle11g hql query-performance

以下是否有任何性能差异?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="0"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:orientation="vertical">

<Button
    android:fontFamily="sans-serif"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/gameTypeButton"
    android:textSize="15sp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" />

</LinearLayout>

VS

NAME LIKE '%EXPRESSION_1%' 
OR NAME LIKE '%EXPRESSION_2%'
...
OR NAME LIKE '%EXPRESSION_N%'

3 个答案:

答案 0 :(得分:2)

IN版本可能更快,更快。

两个版本做同样的事情。但是,如果满足您的需求,IN版本可以利用NAME上的索引。 LIKE版本不能,因为模式以通配符开头。

您可以将其写为:

WHERE NAME LIKE 'EXPRESSION_%' 

如果这符合您的需求,它还可以利用NAME上的索引。

答案 1 :(得分:1)

您可以尝试使用

NAME LIKE '%EXPRESSION_%' 

就性能而言,IN比OR快。您还可以使用两个查询的执行计划确认性能。

同样如上所述,您显示的两个查询是不同的。

第一个查询:

NAME LIKE '%EXPRESSION_1%' 
OR NAME LIKE '%EXPRESSION_2%'
...
OR NAME LIKE '%EXPRESSION_N%'

将尝试获取具有样本数据的结果,如

EXPRESSION_123
XXXEXPRESSION_1234
EXPRESSION_2323
EXPRESSION_2......

而您的第二个查询将匹配与

完全匹配的记录
ACTUAL_VALUE_1,ACTUAL_VALUE_2.....

答案 2 :(得分:0)

如果使用的变量表达式可以根据给定的参数进行更改。然后使用

declare @Expression1 varchar(50)
Set @Expression2 = '%'+ @Expression1 +'%'


    NAME LIKE @Expression2

所以@ Expression1中的任何参数都会自动处理它。