2017-02-24 85 views
0
select * from abc as t where t.num = 8898 order by t.create_dt 
select * from abc as t where t.num like '8898' order by t.create_dt 
select * from abc as t where t.num like reverse('%8898') order by t.create_dt 

哪一個最快?最小化輸出結果的耗時

+0

測試它們並查看哪些數據和系統中速度最快。 –

回答

0

我會說,這將是更快

select * from abc as t where t.num = 8898 order by t.create_dt 

這是爲什麼?

您正在比較整數,但其他兩種方法實際上是比較一個字符。

這種方法是錯誤的,但實際上它是可用:

select * from abc as t where t.num like '8898' order by t.create_dt 

LIKE通常用於在數據庫中的字符串或varchar。

這種方法也是錯誤的,但它也可用:

select * from abc as t where t.num like reverse('%8898') order by t.create_dt 

對於整數,最好使用=,=!,>,<,>=,<= 至於字符串,它建議使用LIKE查找包含你的值完全匹配的字符串正在尋找。如果你想使用通配符來查找特定的字符串,可以這樣使用:LIKE '%something%'

0

假設num是一個數字,第一個應該不會慢於其他的。它可以從abc(num, create_dt)索引中受益。

第二個可能會很快,除了它會將num轉換爲字符串。如果沒有索引,這與第一個不同。但是,這可能會阻止使用索引。

第三個是幾乎相同的推理。