简体   繁体   English

Oracle 中使用子查询和文字表达式的 SQL 性能

[英]SQl performance with subquery and literal expression in Oracle

I am encounter some performance issue for following scenario and want to ask what could be the problem from performace perspective because I think those to queries should be run cca.我在以下场景中遇到了一些性能问题,想从性能角度询问可能是什么问题,因为我认为查询应该运行 cca。 same time.同时。

imagine following 2 queries, the queries are much more longer in reality but for simplicity I reduced them想象以下 2 个查询,查询实际上要长得多,但为了简单起见,我减少了它们

select * from table where x = 2010; select * from table where x = 2010;

select * from table where x = (select year from yearTable where ID=1234); select * from table where x = (select year from yearTable where ID=1234);

there is an index on column X and the subquery gives exact one year 2010 but this query runs forever compared to the first one with literal (few seconds ) X 列上有一个索引,子查询给出了 2010 年的确切年份,但与第一个带有文字(几秒)的查询相比,该查询永远运行

from technical perspective it should not be much difference between them.从技术角度来看,它们之间应该没有太大区别。 Of course the subquery has to be evaluated but this is in milliseconds cause there is an index on ID as well.当然必须评估子查询,但这是以毫秒为单位的,因为 ID 上也有一个索引。

I found out that with this issue , literal vs subquery , fighting severeal SQL statments in the database.我发现这个问题,文字与子查询,在数据库中与严重的 SQL 语句作斗争。

what could be the problem?可能是什么问题呢?

thanks谢谢

For the second query:对于第二个查询:

SELECT *
FROM table
WHERE x = (SELECT year FROM yearTable WHERE ID = 1234);

you actually want to index the table involved in the subquery for fast lookup:您实际上想要索引子查询中涉及的表以进行快速查找:

CREATE INDEX idx1 ON yearTable (ID, year);

Adding an index on x to the table in the outer query likely won't do much, assuming that Oracle would choose to scan this table.假设 Oracle 会选择扫描此表,那么在外部查询中将x上的索引添加到表中可能不会有太大作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM