Solved

INDEX BY clause in collection

Posted on 2010-11-19
16
645 Views
Last Modified: 2013-12-18
Can anyone tell me what exactly is meant by INDEX by clause?

How exactly INDEX BY clause will help in below collections?

Please explain with example

TYPE row_num_type  IS TABLE OF NUMBER       INDEX BY PLS_INTEGER;

TYPE row_text_type IS TABLE OF VARCHAR2(11) INDEX BY PLS_INTEGER;
0
Comment
Question by:ajexpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
16 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 34175562
it's not an "index" like a normal table

by PLS_INTEGER  means the elements of your collection are referenced by integers

x(1)
x(2)
x(3)

as opposed to INDEX BY VARCHAR2(10)

x('Hi')
x('Hello')
x('otherstring')
0
 
LVL 14

Author Comment

by:ajexpert
ID: 34175586
Ok Can you give me example of the following type?

TYPE row_text_type IS TABLE OF VARCHAR2(11) INDEX BY PLS_INTEGER;

how the PLS_INTEGER will enhance the performance?

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34175595
it doesn't enhance performance
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:sdstuber
ID: 34175603
INDEX BY simply declares how you find stuff in your collection.

Do you find it by a number,  or do you find it by a string?



0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 34175612
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34175688
can I ask why you think the INDEX BY declarative might be a performance tool?
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 34175734
ok, now I can think of one.

sparse vs dense collections.


string based indexing is better for performance than numeric if your collection is sparse
numeric based indexing is better for performance than string if your collection is dense

by dense I mean  1,2,3,4,5,6,7...N  -  all index positions are populated

by sparse I mean 'Abacus',  'Gorilla', 'Mountain',  'Xylophone'  -  index positions could have many gaps between them

to compare directly try..

generating a thousand elements with random numbers as the indexes
then generate the same elements with those numbers converted to strings as the indexes.

the string version should perform better (slightly, but measurably)

conversely  if you have 1-1000,  and strings '1'-'1000'  the number version should perform better (slightly, but measurably)
0
 
LVL 14

Author Comment

by:ajexpert
ID: 34175737
Sean,


In the example

TYPE row_num_type  IS TABLE OF NUMBER      

x(1)
x(2)
x(3)
.
.
x(30)
.
.
x(50)

if I have to delete x(30) element, I have to traverse first then delete 30th element

Conversely, if I declare

TYPE row_num_type  IS TABLE OF NUMBER   INDEX BY BINARY INTEGER

I can delete x(30) without traversing, correct?

So I thought INDEX BY is useful in some way as we are gaining performance by not looping

Please correct me if I am wrong
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34175768
the extact threshold of how dense vs how sparse will depend on usage, memory, cpu, etc.  There is no exact number for when you should use one versus the other.  However I wouldn't worry about it usually.

Use whichever indexing is the most logical programmatically.
Only try to tune with this if you've exhausted all other performance options.
The gains are small and if it's not intuitively obvious why one method was chosen over the other then you're just adding complexity
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 34175787
you are wrong

the difference between the two declarations is you are creating two different types.
if you read the link above you'll get more complete descriptions, but in summary...

TYPE row_num_type  IS TABLE OF NUMBER    --- this declares a nested table structure

TYPE row_num_type  IS TABLE OF NUMBER   INDEX BY BINARY INTEGER  --- this declares an associative array
0
 
LVL 14

Author Comment

by:ajexpert
ID: 34175800
Ok, let me read it first then I will ask you more questions :)

By now, you might have got an idea, I am weak in collections, so more questions to follow
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 34175802
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 34175818
actually that is the same chapter, but a different section, just a little bit before the previous link
0
 
LVL 14

Author Comment

by:ajexpert
ID: 34176725
Thanks Sean,

I am still to complete the whole chapter but got some idea.

I believe there is no use of the following

TYPE row_text_type IS TABLE OF VARCHAR2(11) INDEX BY PLS_INTEGER;

it should be INDEX BY VARCHAR2 right?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34177427
no, you need to specify a size

0
 
LVL 14

Author Closing Comment

by:ajexpert
ID: 34207304
Thanks Sean
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to take different types of Oracle backups using RMAN.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question