select * from table1 where id1 in
(
select id1 from table2 where ...
)在table1表的id1字段上已经建了索引(为方便描述取名"idx1"),
括号里面的查询结果不多只有几百条,table1有几百万记录。
我看了sql的执行计划,如果不加hint的话就会对table1做全表扫描。
我想如果能使用上索引idx1,肯定会快得多。
但是怎么才可以用上索引idx1呢?我试过这样修改
select /*+ index (table1 idx1)*/ * from table1 where id1 in
(
select id1 from table2 where ...
)
这个更糟糕,是用idx1访问了整个表..
熟悉SQL调优的高手,可以给我一些建议吗?
(
select id1 from table2 where ...
)在table1表的id1字段上已经建了索引(为方便描述取名"idx1"),
括号里面的查询结果不多只有几百条,table1有几百万记录。
我看了sql的执行计划,如果不加hint的话就会对table1做全表扫描。
我想如果能使用上索引idx1,肯定会快得多。
但是怎么才可以用上索引idx1呢?我试过这样修改
select /*+ index (table1 idx1)*/ * from table1 where id1 in
(
select id1 from table2 where ...
)
这个更糟糕,是用idx1访问了整个表..
熟悉SQL调优的高手,可以给我一些建议吗?
解决方案 »
- PLJSON中文乱码有人遇到没?
- oracle中创建一个生成两位数的流水号
- 求查询当前数据库运行SQL语句的SQL
- 请问为什么在赛扬1.7G,硬盘80G的机器上不能按装oracle816
- 我想用存储过程插入一条记录,ID(number)为表中目前最大记录加一,怎么解决啊
- 请教在linux上oracle9i双机热备方案?
- 看看吧!!!数据库已打开了,但运行一会就报错
- 我的OracleOraHome81ManagementServer启动不起来,有人说是java问题
- 毕业设计最终发行版!请各界朋友参与最终的测试!
- 在表中 插入用户名的语句
- oracle10g Data guard 损失一个归档日志应如何处理呢?
- 一句sql语句,很有挑战性哦!!
(
select 1000 from dual
)很明显这个SQL用索引idx1能够在瞬间内完成查询,但是事实上Oracle会对table1做全表扫描,不会用索引,不信大家试试select * from table1 where id1 in (1000,1001)
这个sql就才会用上idx1
(
select id1 from table2 where ...
)
若是cbo优化模式,要使用上索引必须要对表进行分析。Select last_analyzed,table_name from dba_tables where table_name='PSCP_SMS_SENT';查看表最后一次的分析时间;exec dbms_stats.gather_table_stats (ownname=>'cch',tabname=>'cch_interactions_motive',degree=>2,cascade=>TRUE);对表进行分析,这个过程依据你的表的数据量。
select * from table1 where exists
(
select 1 from table2 where id1=table1.id1
)
在性能优化时首先考虑使用exists或者not exists替换in或者not in。
这是第一步。