Indexing on timestamp
738272Dec 1 2009 — edited Dec 2 2009I have a table with more than 20 million records. I need to be able to query the table for data of a particular period say Nov-01 to Nov-30 of 2009. This is how the table looks:
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(13)
STARTDATETIME NOT NULL TIMESTAMP(9) WITH TIME ZONE
TOOL NOT NULL VARCHAR2(20)
TOOLARGS VARCHAR2(4000)
VERSION VARCHAR2(80)
TESTBED VARCHAR2(64)
LAB VARCHAR2(48)
SITE VARCHAR2(30)
HOST VARCHAR2(25)
HOSTDOMAIN VARCHAR2(75)
HOSTIP VARCHAR2(25)
HOSTUSERID VARCHAR2(64)
Now as the table size increased my query has gotten very slow almost 5-6 mins right now without any indexing. It does a complete table scan even though I need data from the last month.
And an index on the 'startdatetime' column is not helping at all. And I cant think of any other column that should be indexed for this scenario.
My query would look like:
SELECT tool,Site,count(*),sum(usecount) FROM (SELECT tool,Site,HOSTUSERID,count(*) usecount FROM TOOLUSAGE WHERE TOOL IS NOT NULL AND TOOL='ntest' AND VERSION LIKE '3.%.%' AND StartDateTime>TO_DATE('20091101','YYYYMMDD') AND StartDateTime<TO_DATE('20091130','YYYYMMDD') GROUP BY tool,Site,HOSTUSERID) GROUP BY tool,Site ORDER BY tool,Site;
1. Should I do a functional based indexing i.e. Would it be possible to create one with the function being TO_DATE(StartDateTime,YYYYMMDD)? Am not sure about this..
2. Alter the table and add a field which stores just the YYYYMM part of StartDateTime and indexing on that field?
Indexing on a timestamp is not helping much for my purpose.
Thanks