How to create function based index for TO_CHAR
i need to create a function based index for the function to_char.when i tried ,i got an error,"only pure function can be indexed".what the error really means, help me in creating the index to reduce my query cost.
It works fine on my database. version 9iR2
create index IDX_TO_CHAR on emp(to_char(hiredate,'YYYY-MM-DD HH24:MI:SS'));
explain plan for
select hiredate from emp where to_char(hiredate,'YYYY-MM-DD HH24:MI:SS')='1981-05-01 00:00:00';
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
| 0 | SELECT STATEMENT | | 1 | 8 | 2 (50)|
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 8 | 2 (50)|
|* 2 | INDEX RANGE SCAN | IDX_TO_CHAR | 1 | | 2 (50)|
Predicate Information (identified by operation id):
2 - access(TO_CHAR("EMP"."HIREDATE",'YYYY-MM-DD HH24:MI:SS')='1981-05-01 00
:00:00')Yours seem like a tuning issue, so why not give us your sql and execution plan and so on.
maybe there is other ways to tune your sql than creating a function based index?
Similar Messages
-
How to create function based index on REGEXP_LIKE funtion
Dear Gurus,
I have below table CDR
Name Null Type
STARTTIME NOT NULL DATE
SUBSCRIBERNUMBER NOT NULL NUMBER
CALLINGNUMBER NOT NULL VARCHAR2(20)
CALLEDNUMBER NOT NULL VARCHAR2(20)
I am regularly firing below query
SELECT count(*)
FROM CDR data
WHERE STARTTIME BETWEEN '01-Jul-2009 00:00:00' and '31-May-2012 23:59:59'
AND REGEXP_LIKE(data.SUBSCRIBERNUMBER, '^98721[0-9]*[5]+[0-9]*$');
since there is REGEXP_LIKE is being used, Can I use function based index to improve performance.
Thanking in advance
SanjeevHi,
you can do it that way :Scott@my11g SQL>create table test (name varchar2(30));
Table created.
Scott@my11g SQL>create index myfbi on test(case when regexp_like(name,'^98721[0-9]*[5]+[0-9]*$') then 1 else 0 end);
Index created.
Scott@my11g SQL>explain plan for
2 select * from test where case when regexp_like(name,'^98721[0-9]*[5]+[0-9]*$') then 1 else 0 end = 1;
Explained.
Scott@my11g SQL>/
PLAN_TABLE_OUTPUT
Plan hash value: 140237472
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 20 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 20 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | MYFBI | 1 | | 1 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access(CASE WHEN REGEXP_LIKE ("NAME",'^98721[0-9]*[5]+[0-9]*$')
THEN 1 ELSE 0 END =1)
Note
- dynamic sampling used for this statement (level=2)
19 rows selected. -
Function-Based Indexes for 8.1.6 SE and 9iAS
I have installed the 9iAS Portal into a 8.1.6 SE database, and I cannot get the Function-Based Index feature to turn on. I have set QUERY_REWRITE_INTEGRITY=trusted, QUERY_REWRITE_ENABLED=true and COMPATIBLE="8.1.0.0.0". The feature will still not enable.
I have 2 questions:
1. Is there anything else I can do to turn this feature on.
2. If not, do I have to upgrade to 8.1.7 or to 8.1.* Enterprise Edition to make use of this feature.Could you give the statement for the index you have used, the query you try to do and a description of columns and datatypes of the table? How do you know/check that is doesn't work? Execution plan, errors?...
-
Unable to create function based Index
Hi All,
I created a function as below:
create or replace function eqx_oklb_term_date_nvl(pin_term_date date)
return date
deterministic
is
l_sub_date date := sysdate+1;
l_return_value date := l_sub_date;
begin
l_return_value := nvl(pin_term_date, l_sub_date);
return l_return_value;
exception
when others
then
l_return_value := l_sub_date;
return l_return_value;
end eqx_oklb_term_date_nvl;
Now trying to create a function based index using below code:
create index EQX_OKC_K_LINES_B_N4 on OKC.OKC_K_LINES_B(EQX_OKLB_TERM_DATE_NVL(DATE_TERMINATED))
logging
tablespace EQIXDATA
noparallel;
Encountered Error:
SQL Error: ORA-00904: "EQX_OKLB_TERM_DATE_NVL": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I can successfully query dba_objects for the function.
Owner Object_name object_id object_type last_ddl_time status
APPS EQX_OKLB_TERM_DATE_NVL 11764623 FUNCTION 3/4/2013 9:44:57 PM VALID
I can also query the function using dual.
select eqx_oklb_term_date_nvl(null) nvl_date from dual;
nvl_date
3/5/2013 9:53:59 PM
I have given grants of the function to both schemas APPS/OKC
grant all on eqx_oklb_term_date_nvl to okc;
grant all on eqx_oklb_term_date_nvl to apps;
The column date_terminated in table okc_k_lines_b is a date. I've checked that.
Stuck here. Please help.
Thanks in advance,
RahulHi,
To follow up, the below code resolved the issue:
create index EQX_OKC_K_LINES_B_N4 on okc_k_lines_b(apps.eqx_oklb_term_date_nvl(date_terminated))
logging
tablespace eqixdata
noparallel;
-- qualifying the custom function name with schema name resolved the issue.
Thanks,
Rahul -
ORA-00439 while creating function based indexes.
Hi everybody
I'm working on Oracle 8.1.7 database, and try to create a function based index on a table.
Unfortunately I get the ORA-00439 error, that recommends me not to use this feature :-(
What should I do to activate this feature ?
ThanxTry.
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - 64bi
PL/SQL Release 10.1.0.2.0 - Production
CORE 10.1.0.2.0 Production
TNS for Solaris: Version 10.1.0.2.0 - Production
NLSRTL Version 10.1.0.2.0 - Production -
Creating function-based indexes
I'm trying to create a function-based index in one of the user's schemas and am getting an insufficient privilege error. Below are the examples:
connect scott/tiger ;
CREATE INDEX emp_ename_idx ON ename( emp ) ;
Index created.
CREATE INDEX upper_ename_idx ON ename( upper(ename)) ;
ERROR at line1:
ORA-01031: insufficient privileges
The DBA granted the CREATE ANY INDEX privilege to user scott already. Any ideas???
Thanks,
SYHi, all.
I didn't hear about function-based index before, so I did some testing. Instead of using Oracle predefined functions (upper, lower, substr etc.), I created my own function and tried to create an index on it. I received an error that function is not deterministic. The definition of deterministic from Oracle docs:
DETERMINISTIC
This hint helps the optimizer avoid redundant function calls. If a stored function was called previously with the same arguments, the optimizer can elect to use the previous result. The function result should not depend on the state of session variables or schema objects. Otherwise, results might vary across calls. Only DETERMINISTIC functions can be called from a function-based index or a materialized view that has query-rewrite enabled.
So, I put word deterministic in function declaration and everything works OK. Then I modified function to use some of the tables (I used tables across different schemas), recreated the index and it also worked fine.
Question - is this statement correct: You can use schema objects in function which is in turn used for indexes as long as you put word deterministic in function declaration and all objects (function, index) will be valid, but ORACLE doesn't guarantee that result produced using that index will be correct?
Thank you. -
Error creating function based index
i keep getting ORA-13203 errors when creating a function-based index. The function is owned by another schema but the the user creating the function has execute privileges
[email protected]> create index mpp_nc_sidx on MPP_NONCOMPLETE(GIS.DD832UTM(LON*-1,LAT))
2 indextype is mdsys.spatial_index
3 parameters('tablespace=sde4_idx sdo_indx_dims=2');
create index mpp_nc_sidx on MPP_NONCOMPLETE(GIS.DD832UTM(LON*-1,LAT))
ERROR at line 1:
ORA-29855: error occurred in the execution of ODCIINDEXCREATE routine
ORA-13203: failed to read USER_SDO_GEOM_METADATA table
ORA-13203: failed to read USER_SDO_GEOM_METADATA table
ORA-06512: at "MDSYS.SDO_INDEX_METHOD_9I", line 7
ORA-06512: at line 1
[email protected]> select table_name,column_name from user_sdo_geom_metadata;
TABLE_NAME COLUMN_NAME
MPP_NONCOMPLETE GIS.DD832UTM(LON*-1,LAT)
[email protected]> select GIS.DD832UTM(LON*-1,LAT) from mpp_noncomplete where rownum < 3;
GIS.DD832UTM(LON*-1,LAT)(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
SDO_GEOMETRY(2001, 82212, SDO_POINT_TYPE(864941.804, 3916953.95, NULL), NULL, NULL)
SDO_GEOMETRY(2001, 82212, SDO_POINT_TYPE(568560.541, 4181497.56, NULL), NULL, NULL)
[email protected]> select text from all_source where name = 'DD832UTM';
TEXT
FUNCTION dd832utm(x number, y number)
RETURN mdsys.sdo_geometry DETERMINISTIC
IS
geom mdsys.sdo_geometry;
BEGIN
geom := sdo_cs.transform
(mdsys.sdo_geometry (2001,8265,mdsys.sdo_point_type
(x,y, null),null,null),82212);
return geom;
END;
any help appreciated
--kassimHi, try to use a view:
create or replace view v_dd832utm as
select
mdsys.sdo_geometry(2001,8265,
mdsys.sdo_point_type((LON*-1),LAT, null),null,null),
82212)as GEOMETRY
from
mpp_noncomplete;
provide metadata for that view (column: GEOMETRY) and create a spatial index. your way is more sophisticated ;o)
regards, Andreas -
Re: how to create a temporary index for a table in program
Hi,
I execute a report to access table EKBE. The field that is essential is the CPUDT - entry date.
Now, the EKBE is not index with this field.
I do not want to create a permanent index which might occupied space. The current read on EKBE is sequential.
I thought of creating an index in the program and then releases it once the report is out.
Is there a way to achieve that?Hi,
You can dump the data into one internal table and create index of that internal table like this:
tables: ekbe.
data: begin of itab occurs 0,
cpudt like ekbe-cpudt,
indx type i,
end of itab.
select cpudt up to 10 rows
from ekbe into table itab.
loop at itab.
itab-indx = sy-tabix.
write:/ itab-cpudt, itab-indx.
endloop.
Regards,
Bhaskar -
10.1.3 - Create Table Wizard - function based indexes?
Is the ability to create function based indexes via the Create Table Wizard planned for the 10.1.3 production release?
It appears the Wizard under step 7 has the ability to enter "function" expressions. However an error dialog appears if you enter anything but a column name.
CM.Chris,
Yes, this has been implemented for Production.
Regards,
Lisa Sherriff
JDev QA -
Function based indexes on CLOB storage
On a 10gR2 database, with schema-less CLOB storage for an XMLType column:
(1) Can a function based index include a wildcard in the namespace ? Or do I need a new function based index for each specific namespace ?
(2) I must create a new function based index for each different element that I want an indexed search on ?
(3) What limit is there on the number of function based indexes per table ?
(4) I believe XQuery can include a wildcard for namespaces, but XPath 1.x can't. Can I create a function based index using XQuery, rather than XPath ?
Documents conforming to different versions of an XML schema will be present (schema versioning), but I want to search across all documents irrespective of a specific namespace - e.g. "Find any document with reference = 'some Value' , and amount = 1000".
CLOB storage is proposed, due to the need to handle documents from multiple versions of an XML schema. The knowledge of the XSD is not known at development time, but is user definable, and it must be possible to change the structure without system down time. Structured storage is not suitable, due to Oracle's requirement for downtime if the schema changes (CopyEvolve drops/recreates tables), and Oracle doesn't support schema collections, so you can't bind an XML column to multiple schemas.
Here is some sample code of what I'm trying to do:
create table BulkTest
ID NUMBER(10) not null primary key,
USERFIELDS XMLTYPE
create sequence S_BulkTest;
--Document conforming to version 1 of schema
INSERT INTO BulkTest(id, Userfields) VALUES
(S_BulkTest.Nextval,
'<?xml version="1.0" encoding="utf-8" ?>
<mt395 xmlns="urn:incident:mt395-1">
<reference>FH12345678</reference>
<relatedReference>FH23456789</relatedReference>
<queries>Here is some query text.</queries>
<narrative>Here is some narrative text.</narrative>
<relatedMessageType>300</relatedMessageType>
<relatedMessageDate>2005-03-29</relatedMessageDate>
<direction>R</direction>
<sessionNumber>1234</sessionNumber>
<isn>123456</isn>
<relatedMessageDescription>This is the deal where I bought USD 1 million for GBP at 1.76.</relatedMessageDescription>
<otherParty>232332</otherParty>
</mt395>'
--Document conforming to version 2 of schema
INSERT INTO BulkTest(id, Userfields) VALUES
(S_BulkTest.Nextval,
'<?xml version="1.0" encoding="utf-8" ?>
<mt395 xmlns="urn:incident:mt395-2">
<guid>0f9a08f6-b052-4693-baba-8f7dc881e7e8</guid>
<reference>333333</reference>
<queries>Another query</queries>
<narrative>Some narrative</narrative>
<direction>R</direction>
<sessionNumber>1234</sessionNumber>
<isn>223456</isn>
</mt395>'
--It seems I need to create a new index for each field I want to search on
create index iBulkTest_REFERENCE
on BulkTest
(extractValue(UserFields,'/mt395/reference', 'xmlns="urn:incident:mt395-1"'));
--And that a new index is required for each specifc namespace that is present
--Can't we include a wildcard in the namespace ?
create index iBulkTest_REFERENCE_2
on BulkTest
(extractValue(UserFields,'/mt395/reference', 'xmlns="urn:incident:mt395-2"'));
--If I want to query, I have to explicitly specify each namespace.
--Can't I specify a wildcard ?
--This will make it "fun" querying across namespaces!
select
id,
extractValue(UserFields,'/mt395/reference', 'xmlns="urn:incident:mt395-2"') As Reference,
t.userfields.getclobval() userfields
from bulktest t
WHERE extractValue(UserFields,'/mt395/reference', 'xmlns="urn:incident:mt395-2"') = '333333'Andy
#1. You do not have scehma versioning here. Your model is totally incorrect. You shoud not change the namespace when versioning the XML Schema. You have 2 different and totally disjoint XML Schemas. The correct was to version, as distinct from evolve an XML Schema is to change the Schema Location Hint associated with your XML...
Eg
INSERT INTO BulkTest(id, Userfields) VALUES
(S_BulkTest.Nextval,
'<?xml version="1.0" encoding="utf-8" ?>
<mt395 xmlns="urn:incident:mt395" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:incident:mt395 mt395-1.xsd">
<reference>FH12345678</reference>
<relatedReference>FH23456789</relatedReference>
<queries>Here is some query text.</queries>
<narrative>Here is some narrative text.</narrative>
<relatedMessageType>300</relatedMessageType>
<relatedMessageDate>2005-03-29</relatedMessageDate>
<direction>R</direction>
<sessionNumber>1234</sessionNumber>
<isn>123456</isn>
<relatedMessageDescription>This is the deal where I bought USD 1 million for GBP at 1.76.</relatedMessageDescription>
<otherParty>232332</otherParty>
</mt395>'
--Document conforming to version 2 of schema
INSERT INTO BulkTest(id, Userfields) VALUES
(S_BulkTest.Nextval,
'<?xml version="1.0" encoding="utf-8" ?>
<mt395 xmlns="urn:incident:mt395 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:incident:mt395 mt395-2.xsd">
<guid>0f9a08f6-b052-4693-baba-8f7dc881e7e8</guid>
<reference>333333</reference>
<queries>Another query</queries>
<narrative>Some narrative</narrative>
<direction>R</direction>
<sessionNumber>1234</sessionNumber>
<isn>223456</isn>
</mt395>'
This is the correct way of versioning an XML Schema. THe namespace stays the same, the SchemaLocationHint in the SchemaLocation tag changes.
Bear in mind that if you use the technique you are currently using you will make any path expressions you need to write absolutely unmaintaining and the processing of them very inefficient.
Some questions to consider
Node 'X' in namespace 'X' is never the same as Node 'X' in namespace 'Y'.
How would you write an Xpath or XQuery that targetted multiple versions, but not all versions ?
What happens if you have other documents that are really in a different namespace ? Using wildcards can you differentiate them..
From the problem you are describing and the terminolgy you are using it looks like you've been an early customer of Yukon. MSFT clearly didn't understand schema versioning in the early beta releases and used the 'change the namespace' schema for modelling schema versioning.
We do have some technology coming down the pipe which can address the issue, regardless of whether or not it is too late for you to correct the versioning scheme you have selected. However I cannot discuss that in a public forum. If you want to learn about these features and are prepared to enter an NDA with Oracle in order to do so please contact me directly. You can do this a number of ways...
Guess my email address @oracle.com
Post your email address here and I'll delete the post as soon as I have it..
Update your OTN Forum profile to include your email address
Open a TAR and post the tar number here. You can then softclose the tar as this is simply a method for me to get your contact info. -
Function based indexes on object tables
Hi,
I am trying to create a function based index on an object table. I am getting the following error:
SQL> create index cell1_indx on cell1(create_cell1(id)) indextype is mdsys.spatial_index;
create index cell1_indx on cell1(create_cell1(id)) indextype is mdsys.spatial_index
ERROR at line 1:
ORA-29855: error occurred in the execution of ODCIINDEXCREATE routine
ORA-13249: internal error in Spatial index: [mdidxrbd]
ORA-13249: Error in Spatial index: index build failed
ORA-13249: Stmt-Execute Failure: SELECT num_rows from all_tables where owner='ASHE' and table_name=
'CELL1'
ORA-06512: at "MDSYS.SDO_INDEX_METHOD_9I", line 7
ORA-06512: at line 1
Here cell1 is an object table.
Is the procedure for creating function based indexes on object tables different from relational tables?
ChinniOne of the many new features in Oracle 8i is the Function-Based Index (we will refrain from using FBI, but only just). This allows the DBA to create indexes on functions or expressions; these functions can be user generated pl/sql functions, standard SQL functions (non-aggregate only) or even a C callout.
A classic problem the DBA faces in SQL Tuning is how to tune those queries that use function calls in the where clause, and result in indexes created on these columns not to be used.
Example
Standard B-Tree index on SURNAME with cost based optimizer
create index non_fbi on sale_contacts (surname);
analyze index non_fbi compute statistics;
analyze table sale_contacts compute statistics;
SELECT count(*) FROM sale_contacts
WHERE UPPER(surname) = 'ELLISON';
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=1 Bytes=17)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'SALES_CONTACTS' (Cost=3 Card=16 Bytes=272)
Now we use a function based index
create index fbi on sale_contacts (UPPER(surname));
analyze index fbi compute statistics;
analyze table sale_contacts compute statistics;
SELECT count(*) FROM sale_contacts WHERE UPPER(surname) = 'ELLISON';
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=17)
1 0 SORT (AGGREGATE)
2 1 INDEX (RANGE SCAN) OF 'FBI' (NON-UNIQUE) (Cost=2 Card=381 Bytes=6477)
The function-based index has forced the optimizer to use index range scans (retuning zero or more rowids) on the surname column rather than doing a full table scan (non-index lookup). Optimal performance does vary depending on table size, uniqueness and selectivity of columns, use of fast full table scans etc. Therefore try both methods to gain optimal performance in your database.
It is important to remember that the function-based B*Tree index does not store the expression results in the index but uses an "expression tree". The optimizer performs expression matching by parsing the expression used in the SQL statement and comparing the results against the expression-tree values in the function-based index. This comparison IS case sensitive (ignores spaces) and therefore your function-based index expressions should match expressions used in the SQL statement where clauses.
Init.ora Parameters
The following parameter must be set in your parameter file: QUERY_REWRITE_INTEGRITY = TRUSTED
QUERY_REWRITE_ENABLED = TRUE
COMPATIBLE = 8.1.0.0.0 (or higher)
Grants
Grants To create function-based indexes the user must be granted CREATE INDEX and QUERY REWRITE, or alternatively be granted CREATE ANY INDEX and GLOBAL QUERY REWRITE. The index owner must have EXECUTE access on the function used for the index. If execute access is revoked then the function-based index will be "disabled" (see dba_indexes).
Disabled Indexes
If your function-based index has a status of "disabled" the DBA can do one of the following:
a) drop and create the index (take note of its current settings)
b) alter index enable, function-based indexes only, also use disable keyword as required
c) alter index unusable.
Queries on a DISABLED index fail if the optimizer chooses to use the index.Here is an example ORA error:
ERROR at line 1: ORA-30554: function-based index MYUSER.FBI is disabled.
All DML operations on a DISABLED index also fail unless the index is also marked UNUSABLE and the initialization parameter SKIP_UNUSABLE_INDEXES is set to true.
Some more Examples
CREATE INDEX expression_ndx
ON mytable ((mycola + mycolc) * mycolb);
SELECT mycolc FROM mytable
WHERE (mycola + mycolc) * mycolb <= 256;
..or a composite index..
CREATE INDEX example_ndx
ON myexample (mycola, UPPER(mycolb), mycolc);
SELECT mycolc FROM myexample
WHERE mycola = 55 AND UPPER(mycolb) = 'JONES';
Restriction & Rule Summary
The following restrictions apply to function based indexes. You may not index:
a) LOB columns
b) REF
c) Nested table column
d) Objects types with any of the above data types.
Function-based indexes must always follow these rules:
a) Cost Based optimizer only, must generate statistics after the index is created
b) Can not store NULL values (function can not return NULL under any circumstance)
c) If a user defined pl/sql routine is used for the function-based index, and is invalidated, the index will become "disabled"
d) Functions must be deterministic (always return the same value for a known input)
e) The index owner must have "execute" access on function used in the function-based index. Revocation of the privilege will render the index "disabled"
f) May have a B-Tree and Bitmap index type only
g) Can not use expressions that are based on aggregate functions, ie. SUM, AVG etc.
h) To alter a function-based index as enabled, the function used must be valid, deterministic and the signature of the function matches the signature of the function when it was created.
Joel P�rez -
Function based indexes doing full table scan
Guys,
I am testing function based indexes and whatever I do
it is doing a full table scan.
1)I have set the following init parameters as
QUERY_REWRITE_ENABLED=TRUE
QUERY_REWRITE_INTEGRITY=TRUSTED
2)CREATE INDEX i3 ON emp(UPPER(ename));
3) ANALYZE TABLE emp COMPUTE STATISTICS
ANALYZE INDEX I3 COMPUTE STATISTICS
4) DELETE plan_table;
5) EXPLAIN PLAN SET statement_id='Test1' FOR
SELECT ename FROM emp WHERE UPPER(ename) = 'KING';
6) SELECT LPAD(' ',2*level-2)||operation||' '||options||' '||object_name
query_plan
FROM plan_table
WHERE statement_id='Test1'
CONNECT BY prior id = parent_id
START WITH id = 0 order by id
7) And the query plan shows as
SELECT STATEMENT
TABLE ACCESS FULL EMP
I am using 9.0.1.4 !!!
Any help is appreciated !!!
Regards,
A.KishoreOne of the many new features in Oracle 8i is the Function-Based Index (we will refrain from using FBI, but only just). This allows the DBA to create indexes on functions or expressions; these functions can be user generated pl/sql functions, standard SQL functions (non-aggregate only) or even a C callout.
A classic problem the DBA faces in SQL Tuning is how to tune those queries that use function calls in the where clause, and result in indexes created on these columns not to be used.
Example
Standard B-Tree index on SURNAME with cost based optimizer
create index non_fbi on sale_contacts (surname);
analyze index non_fbi compute statistics;
analyze table sale_contacts compute statistics;
SELECT count(*) FROM sale_contacts
WHERE UPPER(surname) = 'ELLISON';
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=1 Bytes=17)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'SALES_CONTACTS' (Cost=3 Card=16 Bytes=272)
Now we use a function based index
create index fbi on sale_contacts (UPPER(surname));
analyze index fbi compute statistics;
analyze table sale_contacts compute statistics;
SELECT count(*) FROM sale_contacts WHERE UPPER(surname) = 'ELLISON';
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=17)
1 0 SORT (AGGREGATE)
2 1 INDEX (RANGE SCAN) OF 'FBI' (NON-UNIQUE) (Cost=2 Card=381 Bytes=6477)
The function-based index has forced the optimizer to use index range scans (retuning zero or more rowids) on the surname column rather than doing a full table scan (non-index lookup). Optimal performance does vary depending on table size, uniqueness and selectivity of columns, use of fast full table scans etc. Therefore try both methods to gain optimal performance in your database.
It is important to remember that the function-based B*Tree index does not store the expression results in the index but uses an "expression tree". The optimizer performs expression matching by parsing the expression used in the SQL statement and comparing the results against the expression-tree values in the function-based index. This comparison IS case sensitive (ignores spaces) and therefore your function-based index expressions should match expressions used in the SQL statement where clauses.
Init.ora Parameters
The following parameter must be set in your parameter file: QUERY_REWRITE_INTEGRITY = TRUSTED
QUERY_REWRITE_ENABLED = TRUE
COMPATIBLE = 8.1.0.0.0 (or higher)
Grants
Grants To create function-based indexes the user must be granted CREATE INDEX and QUERY REWRITE, or alternatively be granted CREATE ANY INDEX and GLOBAL QUERY REWRITE. The index owner must have EXECUTE access on the function used for the index. If execute access is revoked then the function-based index will be "disabled" (see dba_indexes).
Disabled Indexes
If your function-based index has a status of "disabled" the DBA can do one of the following:
a) drop and create the index (take note of its current settings)
b) alter index enable, function-based indexes only, also use disable keyword as required
c) alter index unusable.
Queries on a DISABLED index fail if the optimizer chooses to use the index.Here is an example ORA error:
ERROR at line 1: ORA-30554: function-based index MYUSER.FBI is disabled.
All DML operations on a DISABLED index also fail unless the index is also marked UNUSABLE and the initialization parameter SKIP_UNUSABLE_INDEXES is set to true.
Some more Examples
CREATE INDEX expression_ndx
ON mytable ((mycola + mycolc) * mycolb);
SELECT mycolc FROM mytable
WHERE (mycola + mycolc) * mycolb <= 256;
..or a composite index..
CREATE INDEX example_ndx
ON myexample (mycola, UPPER(mycolb), mycolc);
SELECT mycolc FROM myexample
WHERE mycola = 55 AND UPPER(mycolb) = 'JONES';
Restriction & Rule Summary
The following restrictions apply to function based indexes. You may not index:
a) LOB columns
b) REF
c) Nested table column
d) Objects types with any of the above data types.
Function-based indexes must always follow these rules:
a) Cost Based optimizer only, must generate statistics after the index is created
b) Can not store NULL values (function can not return NULL under any circumstance)
c) If a user defined pl/sql routine is used for the function-based index, and is invalidated, the index will become "disabled"
d) Functions must be deterministic (always return the same value for a known input)
e) The index owner must have "execute" access on function used in the function-based index. Revocation of the privilege will render the index "disabled"
f) May have a B-Tree and Bitmap index type only
g) Can not use expressions that are based on aggregate functions, ie. SUM, AVG etc.
h) To alter a function-based index as enabled, the function used must be valid, deterministic and the signature of the function matches the signature of the function when it was created.
Joel P�rez -
How can I enable function-based indexes on a already created
database. Is an Installation/Db Creation setting?
On another database I am able to create function-based indexes.
Any help appreciated.
Ashish
null<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Rashmi Rungta ([email protected]):
Hi,
I have a table with huge amount of data and hence I have created
a function-based (Upper) index on one of the character NOT
NULL field which is very likely to be used for query purposes.
First of all though I can create ordinary normal field based
indexes in my user I cannot create a function-based index, why
That had to be created thru SYS user. Secondly I my query
invloving this function is not using the index and instead doing
a full-table scan. Problem outlined below :
Table : PLZPOST, USER / OWNER : PARTNER, FIELD : ORT
INDEX created on UPPER(ORT) in SYS user -
Create index upper_plz_ort on partner.plzpost (upper(ort))
When I give any of the following queries instead of using the
resp. index as mentioned in Oracle DOC it just does a full table
scan (checked thru Explain Plan) :
select * from PLZPOST where upper(ort) is not null;
select * from PLZPOST where upper(ort) like upper('saar%')
select * from PLZPOST where upper(ort) = 'SAARBRUECKEN'
etc etc
If anyone has used Function-based indexes in Oracle 8i could you
please tell me where am I going wrong. Is it because my Table
belongs to PARTNER and Index to SYS (tried running the above
queries under SYS user also but still did not work) ?? If so how
can I grant access on an Index to PARTNER from SYS ??
Your help would be greatly appreciated.
Thanks in advance,
Cheers
Rashmi<HR></BLOCKQUOTE>
null -
Function Based Index on Date Column
Hi All,
I need to execute a query like this :
SELECT * FROM ORDERS WHERE APPROVE_DATE IS NULL
I read anywhere that this will cause unnecessary FTS so that I should create function based index.
I have tried one below , but not sure that this is correct approach :
CREATE INDEX idx_1
ON ORDERS (NVL(APPROVE_DATE, '01-JAN-1900'));
SELECT * FROM ORDERS WHERE NVL(APPROVE_DATE, '01-JAN-1900') = '01-JAN-1900'
Is this a correct approach ?
Thank you,
xtantoA SQL_TRACE output will explain clearly what Justin has stated.
I have created a table T based on all_objects.
SQL> desc t
Name Null? Type
OWNER NOT NULL VARCHAR2(30)
OBJECT_NAME NOT NULL VARCHAR2(30)
SUBOBJECT_NAME VARCHAR2(30)
OBJECT_ID NOT NULL NUMBER
DATA_OBJECT_ID NUMBER
OBJECT_TYPE VARCHAR2(19)
CREATED DATE
LAST_DDL_TIME NOT NULL DATE
TIMESTAMP VARCHAR2(19)
STATUS VARCHAR2(7)
TEMPORARY VARCHAR2(1)
GENERATED VARCHAR2(1)
SECONDARY VARCHAR2(1)
CASE I_
SQL> select count(1) from t
2 /
COUNT(1)
934320
SQL> select count(1) from t where created is null
2 /
COUNT(1)
2376The number of null values in CREATED column is proportionately very small.
Now i execute the query without function based index.
select *
from t
where created is null
call count cpu elapsed disk query current rows
Parse 1 0.00 0.09 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 160 0.04 0.10 0 12662 0 2376
total 162 0.04 0.19 0 12662 0 2376
Rows Execution Plan
0 SELECT STATEMENT GOAL: ALL_ROWS
2376 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'T' (TABLE)And here is the query that uses the function based index
select *
from t
where nvl(created,to_date('01-01-1900','DD-MM-YYYY')) = to_date('01-01-1900','DD-MM-YYYY')
call count cpu elapsed disk query current rows
Parse 1 0.01 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 160 0.01 0.01 0 698 0 2376
total 162 0.03 0.01 0 698 0 2376
Rows Execution Plan
0 SELECT STATEMENT GOAL: ALL_ROWS
2376 TABLE ACCESS GOAL: ANALYZED (BY INDEX ROWID) OF 'T' (TABLE)
2376 INDEX GOAL: ANALYZED (RANGE SCAN) OF 'T_FN_IDX' (INDEX)Its very obvious from the above output that the Function Based Index as increased the performance.
CASE II_
SQL> select count(1) from t
2 /
COUNT(1)
934320
SQL> select count(1) from t where created is null
2 /
COUNT(1)
202168Now the null values in the CREATED column is proportionately large than the first test case.
Now lets see without using the function based index
select *
from t
where created is null
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 13479 0.46 0.71 2 25832 0 202168
total 13481 0.46 0.71 2 25832 0 202168
Rows Execution Plan
0 SELECT STATEMENT GOAL: ALL_ROWS
202168 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'T' (TABLE)Now iam trying to use the function based index
select *
from t
where nvl(created,to_date('01-01-1900','DD-MM-YYYY')) = to_date('01-01-1900','DD-MM-YYYY')
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 13479 0.54 0.84 0 33826 0 202168
total 13481 0.54 0.84 0 33826 0 202168
Rows Execution Plan
0 SELECT STATEMENT GOAL: ALL_ROWS
202168 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'T' (TABLE)Its obvious from the result that oracle has decided to go for a FULL TABLE SCAN even when an index was available.
So just having a function based index is not going to increase the query performance. There are lot of other factors to be considered as stated above.
Thanks,
Karthick. -
Function-based indexes don't seem to work in Oracle 8.1.5?
Hi,
What gives? What am I doing wrong? I have a table AIRPORT with a column (varchar2(64)) which I have specified a function based index for, but I can't get SQL wueries to use it!!!! the following SQL executes a FULL TABLE SCAN:
select /*+ index (a idx_upper_cityname) */ *
from airport a
where nls_upper(cityName) = 'dfdf'
...as does...
select *
from airport a
where nls_upper(cityName) = 'dfdf'
Table and index code is as follows:
CREATE TABLE airport
id NUMBER NOT NULL,
citycode VARCHAR2(3) NOT NULL,
cityname VARCHAR2(64) NOT NULL,
state VARCHAR2(2),
country VARCHAR2(2) NOT NULL,
region CHAR(1),
airportcode VARCHAR2(3) NOT NULL,
airportname VARCHAR2(64),
code VARCHAR2(4)
drop index idx_upper_cityname
CREATE INDEX idx_upper_cityname ON airport nls_upper(substr(cityName, 0, 64) )
Environment is as follows:
Oracle8i v8.1.5 running on WinNT v4.0 (SP 5)
Client is running on the same machine
thanks in advance,
AlexanderNew data point: when I set the handler in my logging.properties file thusly,
org.apache.catalina.core.ContainerBase.[Catalina].[info-dev].[/infoisland].level = ALL
org.apache.catalina.core.ContainerBase.[Catalina].[info-dev].[/infoisland].handlers = java.util.logging.ConsoleHandlerI get 0 bytes in the info-dev log (which used to have the aforementioned expception in it). Where is my console going?
Maybe you are looking for
-
Unit of measure EA allows transactions in decimals ? Is EA indivisible?
Unit of measure EA allows transactions in decimals ? Is EA indivisible? There are two scenarios. While creating a Bill of material - based on the configuration (see below) if you enter a quantity with decimals for a part number for which the Basic UO
-
Server Crashes while compiling JSP
Hi, I have a jsp (in a web app which i have deployed on WLS 7.0)and when i invoke it, the server crashes and i get the below mentioned error, somewhere i read this is a bug in jdk. I am using the jdk shipped wi
-
Configuration issue : Oracle Essbase Studio Server
Hi, What going wrong here? Please advice... C:\Hyperion\products\Essbase\EssbaseStudio\Server\server_prod.log 20:44:06 05/13/10 INFO Starting up 20:44:06 05/13/10 INFO Oracle Essbase Studio Server. Version 11.1.1.3.00, Build 090, June 25 2009 20:44:1
-
Reversal Indicator Field Not reflecting Correctly In BW
Hi All, The deletion indicator coming from the DataSource 2LIS_02_ITM is not working properly for few entries and in BW its mapped to Reversal Indicator (Storno) When we checked, we found that the deletion indicator is not working for the items whic
-
Hello, I am trying to get a dropdown column in an ALV table. While searching in the forum I now have the dropdown in thr ALV column 'VALUE' but I am not able to to get values in the column. I have one node called 'RES' type WDR_CONTEXT_ATTR_VALUE wit