通过查询索引表以获得更好的性能

时间:2022-12-27 19:16:10

What index/indexes would you recommend to be created on the table for making the following type of query more efficient:

您建议在表中创建哪些索引/索引,以便使以下类型的查询更有效:

SELECT MAX(val1)
FROM table
WHERE val2 = 'x' OR val3 = 'y'

x and y are of course variable. val2 and val3 are almost always unique, some duplicates may occur.

x和y当然是可变的。val2和val3几乎总是唯一的,可能会发生重复。

1 个解决方案

#1


4  

have an index on val2+val1 and another on val3+val1 and write query like:

有关于val2+val1的索引和关于val3+val1的索引,并编写如下查询:

SELECT MAX(val1)
FROM (SELECT max(val1) FROM table where val2 = 'x'
      UNION ALL
      SELECT max(val1) FROM table val3 = 'y'
     ) dt

#1


4  

have an index on val2+val1 and another on val3+val1 and write query like:

有关于val2+val1的索引和关于val3+val1的索引,并编写如下查询:

SELECT MAX(val1)
FROM (SELECT max(val1) FROM table where val2 = 'x'
      UNION ALL
      SELECT max(val1) FROM table val3 = 'y'
     ) dt