优化选择

时间:2017-03-23 14:24:47

标签: mysql sql database oracle

如何优化我的选择:

SELECT Id, FirstName, LastName, Country
  FROM Customer
 WHERE (Country ="x" and Id ="y") 
    or (Country ="x1" and Id ="y1")
    or (Country ="x2" and Id ="y2")
    or (Country ="x3" and Id ="y3")
    or (Country ="x4" and Id ="y4")
    or (Country ="x5" and Id ="y5")

1 个答案:

答案 0 :(得分:1)

我建议使用元组编写它:

SELECT Id, FirstName, LastName, Country
FROM Customer c
WHERE (Country, id) in ( ('x', 'y'), ('x1', 'y1'), . . . )

(这应该适用于MySQL和Oracle,但不适用于所有数据库。)

然后数据库应该能够在Customer(Country, Id)上轻松使用索引。它也许能够将该索引与原始版本一起使用。