Indexing a table for better performance by a query
What index/indexes would you recommend to be created on the开发者_Python百科 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.
have an index on val2
+val1
and another on val3
+val1
and write query like:
SELECT MAX(val1)
FROM (SELECT max(val1) FROM table where val2 = 'x'
UNION ALL
SELECT max(val1) FROM table val3 = 'y'
) dt
精彩评论