views:

88

answers:

6

Hi

I don't know the correct words for what I'm trying to find out about and as such having a hard time googling.

I want to know whether its possible with databases (technology independent but would be interested to hear whether its possible with Oracle, MySQL and Postgres) to point to specific rows instead of executing my query again.

So I might initially execute a query find some rows of interest and then wish to avoid searching for them again by having a list of pointers or some other metadata which indicates the location on a database which I can go to straight away the next time I want those results.

I realise there is caching on databases, but I want to keep these "pointers" else where and as such caching doesn't ultimately solve this problem. Is this just an index and I store the index and look up by this? most of my current tables don't have indexes and I don't want the speed decrease that sometimes comes with indexes.

So whats the magic term I've been trying to put into google?

Cheers

+3  A: 

In Oracle it is called ROWID. It identifies the file, the block number, and the row number in that block. I can't say that what you are describing is a good idea, but this might at least get you started looking in the right direction.

Check here for more info: http://www.orafaq.com/wiki/ROWID.

By the way, the "speed decrease that comes with indexes" that you are afraid of is only relevant if you do more inserts and updates than reads. Indexes only speed up reads, so if the read ratio is high, you might not have an issue and an index might be your best solution.

MJB
I don't have our schema to hand so I don't know whether we use indexes, i just had some ideas over the weekend. Perhaps this is the way to go.
David
I still think that indexes are where you need to look, but good luck either way.
MJB
A: 

Do you mean a primary key?

Phil Sandler
+3  A: 

most of my current tables don't have indexes and I don't want the speed decrease that sometimes comes with indexes.

And you also don't want the speed increase which usually comes with indexes but you want to hand-roll a bespoke pseudo-cache instead?

I'm not being snarky here, this is a serious point. Database designers have expended a great deal of skill and energy into optimizing their products. Wouldn't it be more sensible to learn how to take advantage of their efforts rather re-implementing some core features?

APC
+2  A: 

In general, the best way to handle this sort of requirement is to use the primary key (or in fact any convenient, compact unique identifier) as the 'pointer', and rely on the indexed lookup to be swift - which it usually will be.

You can use ROWID in more DBMS than just Oracle, but it generally isn't recommended for a variety or reasons. If you succumb to the 'every table has an autoincrement column' school of database design, then you can record the autoincrement column values as the identifiers.

You should have at least one index on (almost) all of your tables - that index will be for the primary key. The exception might be for a table so small that it fits in memory easily and won't be updated and will be used enough not to be evicted from memory. Then an index might be a distraction; however, such tables are typically seldom updated so the index won't harm anything, and the optimizer will ignore it if the index doesn't help (and it may not).

You may also have auxilliary indexes. In a system where most of the activity is reading the data, you may want to erro on the side of having more indexes rather than fewer, because access time is most critical. If your system was update intensive, then you would go with fewer indexes because there is a cost associated with updating indexes when data is added, removed or updated. Clearly, you need to design the indexes to work well with the queries that your users actually perform (or your applications perform).

Jonathan Leffler
A: 

APC: Not seen as snarky at all, I'm here to learn - I'm no expert hence my questions. The point really was to learn whats out there so I can build something good. I don't want to reimplement anything if I can avoid it!

David
A: 

You may also be interested in cursors. (Note that the index debate is still valid with cursors.)

Wikipedia definition here.

aib