Skip to Main Content
  • Questions
  • Index column order and Partitioned table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sam.

Asked: February 18, 2017 - 1:32 am UTC

Last updated: February 25, 2017 - 12:46 am UTC

Version: 12c

Viewed 1000+ times

You Asked

We have a table with few hundred millions of rows. We perform INSERT, DELETE, SEARCH operation on this table.
Column ID is unique and column CODE has very few distinct values (lets say 100 distinct values). So we partition table by LIST CODE column value and sub partition by HASH ID column value and we have Primary key (ID, CODE).
Most of the queries we perform on this table includes ID and CODE column in WHERE clause. There are very few queries which have only CODE column in WHERE clause.
We defined Primary Key as (ID, CODE) where ID has lowest cardinality.


Question -
1. Primary key and its index (ID, CODE) is a better choice or (CODE, ID)?
2. Primary key (ID) and a separate index on (CODE) would be a better choice?

Thanks a lot.
-Sam


and Connor said...

Well, if you are partitioned by CODE, then already queries just on CODE are taken care of.

So that leaves queries by ID and queries by ID and CODE.

(a) Global index on ID,CODE
=> works, but has a partition maintenance impact

(b) Local index on ID
=> works, but cannot be used for primary key

(c) Local index on CODE,ID
=> satisfies all requirements but not queries just on ID

(d) Local index in ID,CODE
=> satisfies all requirements

So with the information you've provided, I'd go with (d)


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Suggest your own partitioning

Gh.., February 19, 2017 - 12:08 pm UTC

Good analysis for giving partitioned table. But what if have to do the best partitioning / indexing? How do you proceed giving the same definitions for cols Id and Code?

Eg what about partitioning by hash (Id, code ) what would be the indexes?

Or what about partitioning by rang interval id and subpartition by hash code? What indexes?
And in which case of partitioning choice you would use a bitmap index on code? ..

So what do you choose as partitioning + indexing?
Please suppose it's you tabke and job. So what is you technical choice?

What if table is not partitioned?

Sam, February 21, 2017 - 7:53 pm UTC

Thanks Connor. I did not think about "queries just on CODE are taken care of because of Partitioned by CODE". Sometimes answer is simple but we chase the wrong end. :)

Sometimes we do not partition table (less than 100 million rows). In this scenario what would be better choice -

We defined Primary Key as (ID, CODE) where ID has lowest cardinality and ID itself is sufficient for PrimaryKey but CODE is part of PrimaryKey to support possible partitioning scheme.

Question -
1. Primary key and its index (ID, CODE) is a better choice or (CODE, ID)?
2. Primary key (ID) and a separate index on (CODE) would be a better choice?

Thanks a lot.
-Sam
Connor McDonald
February 22, 2017 - 1:27 am UTC

Running under the same assumption:

"Most of the queries we perform on this table includes ID and CODE column in WHERE clause. There are very few queries which have only CODE column in WHERE clause. "

I would index by (CODE,ID) because it satisfies both operations with a single index, and I can also take advantage of leading column compression, ie

create index IX on T ( code, id ) compress 1;

to keep the index compact.

i didnt get a feedback on my post signed Gh..

A reader, February 23, 2017 - 9:51 am UTC

tkx for any suggestion on my above question
Connor McDonald
February 25, 2017 - 12:46 am UTC

How do you proceed giving the same definitions for cols Id and Code?

Partitioning is often driven by a combination of business requirements, not just what is "technically best", eg, do I want to archive by code ? compress by code ? What is the speed of my storage ? (which might determine granularity)


More to Explore

VLDB

If you are new to partitioning, check out Connor McDonald's introduction series here.

VLDB

Documentation set on VLDB and Partitioning.