05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 13 ■ PARTITIONING 611<br />

So, as with everything, before applying partitioning to a system to “increase performance,”<br />

make sure you underst<strong>and</strong> what that system needs. If your system is currently CPU<br />

bound, but that CPU usage is not due to contention <strong>and</strong> latch waits, introducing partitioning<br />

could make the problem worse, not better!<br />

USING ORDER BY<br />

This example brought to mind an unrelated but very important fact. When looking at hash partitioned indexes,<br />

we are faced with another case where the use of a index to retrieve data would not automatically retrieve the<br />

data sorted. Many people assume that if the query plan shows an index is used to retrieve the data, the data<br />

will be retrieved sorted. This has never been true. The only way we can retrieve data in any sort of sorted<br />

order is to use ORDER BY on the query. If your query does not contain an ORDER BY statement, you cannot<br />

make any assumptions about the sorted order of the data.<br />

A quick example demonstrates this. We create a small table as a copy of ALL_USERS <strong>and</strong> create a hash<br />

partitioned index with four partitions on the USER_ID column:<br />

ops$tkyte@ORA10G> create table t<br />

2 as<br />

3 select *<br />

4 from all_users<br />

5 /<br />

Table created.<br />

ops$tkyte@ORA10G> create index t_idx<br />

2 on t(user_id)<br />

3 global<br />

4 partition by hash(user_id)<br />

5 partitions 4<br />

6 /<br />

Index created.<br />

Now, we will query that table <strong>and</strong> use a hint to have <strong>Oracle</strong> use the index. Notice the ordering (actually,<br />

the lack of ordering) of the data:<br />

ops$tkyte@ORA10G> set autotrace on explain<br />

ops$tkyte@ORA10G> select /*+ index( t t_idx ) */ user_id<br />

2 from t<br />

3 where user_id > 0<br />

4 /<br />

USER_ID<br />

----------<br />

11<br />

34<br />

...<br />

81<br />

157

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!