0

I have a table with 226 million rows that has a varchar2(2000) column. The first 10 characters are indexed using a functional index SUBSTR("txtField",1,10).

I am running a query such as this:

select count(1) 
from myTable 
where SUBSTR("txtField",1,10) = 'ABCDEFGHIJ';

The value does not exist in the database so the return in "0".

The explain plan shows that the operation performed is "INDEX (RANGE SCAN)" which I would assume and the cost is 4. When I run this query it takes on average 114 seconds.

If I change the query and force it to not use the index:

select count(1) 
from myTable   
where SUBSTR("txtField",1,9) = 'ABCDEFGHI';

The explain plan shows the operation will be a "TABLE ACCESS (FULL)" which makes sense. The cost is 629,000. When I run this query it takes on average 103 seconds.

I am trying to understand how scanning an index can take longer than reading every record in the table and performing the substr function on a field.

Followup: There are 230M+ rows in the table and the query returns 17 rows; I selected a new value that is in the database. Initially I was executing with a value that was not in the database and returned zero rows. It seems to make no difference.

Querying for information on the index yields: CLUSTERING_FACTOR=201808147 LEAF_BLOCKS=1131660

I am running the query with AUTOTRACE ON and the gather_plan_statistics and will add those results when they are available.

Thanks for all the suggestions.

4

1 回答 1

3

有很多可能性。

不过,您需要查看实际的执行计划。

您可以使用提示运行查询/*+ gather_plan_statistics */,然后执行:

select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));

您还应该查看运行跟踪/tkprof 以查看实际发生的情况 - 您的 DBA 应该能够帮助您解决这个问题。

于 2013-09-20T15:29:37.647 回答