0

We have a table called t_reading, with the following schema:

MEAS_ASS_ID     NUMBER(12,0)
READ_DATE       DATE
READ_TIME       VARCHAR2(5 BYTE)
NUMERIC_VAL     NUMBER
CHANGE_REASON   VARCHAR2(240 BYTE)
OLD_IND         NUMBER(1,0)

This table is indexed as follows:

CREATE INDEX RED_X4 ON T_READING
(
  "OLD_IND",
  "READ_DATE" DESC,
  "MEAS_ASS_ID",
  "READ_TIME"
)

This exact table (with the same data) exists on two servers, the only difference is the Oracle version installed on each one.

The query in question is:

SELECT * FROM t_reading WHERE OLD_IND = 0 AND MEAS_ASS_ID IN (5022, 5003) AND read_date BETWEEN to_date('30/10/2012', 'dd/mm/yyyy') AND to_date('31/10/2012', 'dd/mm/yyyy');

This query executes in less than a second on Oracle 10, and around a minute in Oracle 9.

Are we missing something?

EDIT:

Execution plan for Oracle 9: enter image description here

Execution plan for Oracle 10: enter image description here

5
  • 1
    please provide execution plans on both databases. Oct 31, 2012 at 7:50
  • What is the execution plan in 9i and 10g? (Btw: 9i is no longer supported)
    – user330315
    Oct 31, 2012 at 7:51
  • the simplest solution is to analyze table on 9i database. The best is to add a hint to use index. Oct 31, 2012 at 7:54
  • @a_horse_with_no_name - not true. 9.2 is covered by Oracle's lifetime support guarantee, although it is in the final phase of that policy, "sustaining support".
    – APC
    Oct 31, 2012 at 8:13
  • 1
    Apparently 9i is configured to use the rule based optimizer. That could be caused by missing statistics or the CBO has been disabled in your system.
    – user330315
    Oct 31, 2012 at 8:39

4 Answers 4

1

"Are we missing something?"

Almost certainly, but it's difficult for us to tell you what.

There were some performance improvements in the CBO from 9i to 10g but it's unlikely to make that much difference. So it must be some variation in your systems, which is obviously the hardest thing for us to diagnose, blind and remote as we are.

So, first things to rule out are general system diffences - disk speeds, i/o bottlenecks, memory sizing, etc. You say you have two servers, do they have different specs? Whilst it will require assistence from an sysadmin type to investigate these things, we can discount them with a single question: is it just this query, or can you reproduce this effect with many different queries?

If is just the query, there are at least three possible explanations.

One is data distribution. How was the data populated in the two databases? If the 10g was exported from the 9i database was it sorted in some fashion? Even if it wasn't it is possible that the ETL process has compacted and organised the data and built the fresh indexes in a way which improves the access times.

Another is statistics. Are the 10g statistics fresh and realistic, whilst the 9i statistics are stale and misleading?

A third possibility is a stored execution plan. (You have posted a query with literals, this only applies to queries with bind variables.) Searches on date ranges are notoriously hard to tune. A date range of to_date('30/10/2012', 'dd/mm/yyyy') AND to_date('31/10/2012', 'dd/mm/yyyy') suits one sort of plan, whereas date range of to_date('01/01/2010', 'dd/mm/yyyy') AND to_date('31/10/2012', 'dd/mm/yyyy') may well suit a different approach. If the extant plan on the 9i database suits a broader range then queries for a narrow range may take a long time.


While I've been typing this you have published the explain plans. The killer detail is at the bottom of the 9i plan:

Note: rule-based optimization

You haven't got any stats for the table or the index, so the optimizer is applying the dumb defaults of the RBO. You should really address this, but it's not a simple task. You may need to gather stats for all your tables. You may need to change the OPTIMIZER_MODE in the init.ora file. You may need to undertake a regression test of all the queries on your database. So, it's not something you shoudl do lightly.

In the meantime, if this query is bugging you, you'll need to wrnagle the Rule-Based Optimizer the old-fashioned way. Find out more.

1

A couple of potential explanations:

  • You're range scanning different indexes.
  • Assuming that you've got the same index on your 10g table but you've just called it a different thing the explain plans are different.

The main worry I would have is the lack of information in the rows, bytes and cost column of the explain plan on your 9i query. Oracle 9i does not collect statistics by default and this detail would indicate that you have not collected statistics on this table. Use dbms_stats to gather statistics on your table and the indexes. Specifically the procedure gather_table_stats:

BEGIN

    DBMS_STATS.GATHER_TABLE_STATS (
       ownname => user, 
       tabname => 'T_READING', 
       estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE
       method_opt => 'FOR ALL INDEXED COLUMNS',
       cascade => TRUE, -- gather index statistics
        );
END:

There are plenty of other options if you're interested. Assuming the indexes are different this may help the CBO (assuming it's "turned on") to pick the correct index.

The other options include what server they are on and what the database parameters are. If they're on different servers then the relative "power", disk-speed, I/O and a never-ending list of other options could easily cause a difference. If the database parameters are different then you have the same problem.

Database tuning is an art as much as a science. Oracle has a whole book on it and there are plenty of other resources out there.

1
  • If the 9i database has been running in RBO mode moreorless satisfactorily it may be destructive to switch on statistics. We don't have enough information about the OP's situation to offer this advice without caveats.
    – APC
    Oct 31, 2012 at 9:38
1

A few observations:

  • your index is a DESCENDING index. This is a function based index, as such, it won't work as expected under the RULE optimizer.
  • your 9i plan shows access only on OLD_IND, your 10g plan (you cut off the important predicate bits) shows a range scan + inlist iterator, so depending on that RED_PK, it may be accessing on MEAS_ASS_ID which is perhaps more selective.
  • in terms of indexing too, to answer your query WHERE OLD_IND = 0 AND MEAS_ASS_ID IN (5022, 5003) AND read_date BETWEEN ie OLD_IND equality, MEAS_ASS_ID equality and read_date range scanned, a better index is (OLD_IND , MEAS_ASS_ID , READ_DATE): do the range scan last to cut down on IO.
0

Have you tried running an explain on the queries on the two servers, the query optimiser for 9i is different from the one for 10g. The 10g query optimiser is much faster and parallelised. Check out the following link Upgrading Query Optimiser

explain SELECT * FROM t_reading WHERE OLD_IND = 0 AND MEAS_ASS_ID IN (5022, 5003) AND read_date BETWEEN to_date('30/10/2012', 'dd/mm/yyyy') AND to_date('31/10/2012', 'dd/mm/yyyy');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.