我的示例 oracle 查询结构是这样的:
SELECT <LIST_OF_COLUMNS> FROM <TABLE_NAME>
WHERE COLUMN_01 = <SOMETHING> AND COLUMN_02 = <SOMETHING> AND COLUMN_03 = <SOMETHING>
该表有超过 100 万条记录。我已经分别索引了 COLUMN_01、COLUMN_02 和 COLUMN_03。上述查询工作正常并按预期提供结果。
如果我将 COLUMN_01、COLUMN_02 和 COLUMN_03(
WHERE
子句中的所有列)作为复合索引而不更改现有索引,是否会提高性能?如果是这样,是否有复合索引列的顺序?如果我使用
OR
而不是 AND
像这个查询,它会提高性能吗?SELECT <LIST_OF_COLUMNS> FROM <TABLE_NAME>
WHERE COLUMN_01 = <SOMETHING> OR COLUMN_02 = <SOMETHING> OR COLUMN_03 = <SOMETHING>
最佳答案
您永远不会只使用
WHERE
中的一列来查询表子句,您将不需要单列索引。否则,它
取决于哪些列参与查询。而这个案例应该
得到很好的分析和测试。
还应该注意的是,Oracle 可以对不包含谓词中的所有索引列的查询使用复合索引。
例如:
create index idx1 on table_name (col1, col2, col3);
/*In this query Oracle can use index idx1 as a standard one-column index,
because col1 is the first column in the index*/
select * from table_name where col1 = 'some_value';
/*Here Oracle can still use the composite index,
but in this case it will use INDEX SKIP SCAN (assuming col1 equals ANY value),
which reduces query performance comparing to an ordinary index*/
select * from table_name where col2 = 'some_value1' and col3 = 'some_value2';
OR
或 AND
运营商在这里并不重要。更重要的是匹配给定谓词的行数 . 关于Oracle 综合指数性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255998/