I have two tables with the following columns:
Table1
{ ID NUMBER(15),
ROLL_NUM VARCHAR2(9),
BATCH_NUM VARCHAR2(6),
ACCT_BALANCE NUMBER(15,2)
}
Table2
{ Table1_ID NUMBER(15) REFERENCES TABLE1.ID,
SEQ_NUM NUMBER(2),
TRANS_NUM VARCHAR2(10),
TRANS_AMT NUMBER(8,2),
TRANS_DT DATE
}
Table1 has 200,000 records and Table2 has 500,000 records
I have simple joins as follows:
SQL #1:
SELECT A.ROLL_NUM, A.ACCT_BALANCE, B.TRANS_NUM, TRANS_AMT, TRANS_DT
FROM TABLE1 A, TABLE2 B
WHERE B.Table1_ID = A.ID
AND A.BATCH_NUM = 400012
SQL #2:
SELECT A.ROLL_NUM, A.ACCT_BALANCE, B.TRANS_NUM, TRANS_AMT, TRANS_DT
FROM TABLE1 A, TABLE2 B
WHERE B.Table1_ID = A.ID
AND A.BATCH_NUM = '400012'
SQL #3:
SELECT A.ROLL_NUM, A.ACCT_BALANCE, B.TRANS_NUM, TRANS_AMT, TRANS_DT
FROM TABLE1 A, TABLE2 B
WHERE B.Table1_ID = A.ID
AND A.BATCH_NUM = TO_NUMBER('400012')
The expected result from the count should be 500,000 if every BATCH_NUM from Table1 is '400012' and all the ID's match up in Table2.
When I run these queries in Oracle (v11 or v10), SQL #2 seems to take forever and I had to stop the query from running after 10 to 15 minutes. SQL #1 and #3 seems to come back with results instantaneously with the full 500,000 records in less than a second. At first, I thought it was an indexing issue, but adding the index doesn't do anything to resolve the issue. I tried this query in TOAD and in SQL Developer with the same results.
I'm at a loss here because the BATCH_NUM column in Table1 is a VARCHAR2 and you'd think that an implicit conversion of the data types would cause the query to be slower, not faster than a non-converted comparison. Can someone explain this?
A.BATCH_NUM = 400012the actually execution will be likeTO_NUMBER(A.BATCH_NUM) = 400012. this will cause index on this field not be used