Oracle training, tutorials and consulting
Search for:                            

 

For Better, Faster, Smarter, Oracle Training and Consultancy

 

Oracle Performance Tuning Part 1: Using Full Table Scans

It is a common misconception that all SQL queries on all tables in Oracle databases should be index driven. In fact, using full-table scans can improve performance in two scenarios: when querying very small tables and when querying very large tables.

1.1 The Effect of Full Table Scans When Querying Small Tables

Let's suppose your using your Oracle database to run in-house designed and built HR application. Consider a reference table such as a list of department ids and the associated department names. Even a large company is likely to have only a few departments - HR, Sales, Marketing, Finance, IT, so the table is going to be quite small.

 

 

 

Now let's suppose the table has just 2 columns - department id and department name - with an index on the department id. To find the department name for a given id, we would have to read the index and then read the table, but because the table is so small and because Oracle reads multiple database blocks in one read operation the whole table is scanned in just one read, so however efficient the index, by using it we will be performing unnecessary i/o.

In this case, therefore, a full table scan is faster than an index scan and table lookup. The exception to this of course is when the table has been created as an index-only table (available since Oracle 8) which means that the whole table is stored in a B-tree structure (although you may have pointers to overflow areas).

1.2 The Effect of Full Table Scans When Querying Very Large Tables

Let's look at using this technique for querying very large tables in your Oracle database. Surely they should use an index? Otherwise you might have to read thousands of blocks. It is correct to say that a full table scan of a very large table could read many thousands of data blocks, but as we shall see it may be better to do this than to perform an index scan and table lookup.

The situation when the a full table scan is very likely to perform better than an index scan and table lookup is when you are retrieving 10% or more of the data in the table and it may perform better even when you are retrieving as little as 1% of the table data. Of course if you only want to retrieve one row in the table, then you would want to use an index.

1.2.1 Index Scan And Table Lookup Vs. Full Table Scan For Very Large Tables in Oracle Databases

Let's look at the 2 scenarios then - retrieving 10% of the table data by index scan and table lookup vs. full table scan.

To make the maths easy, assume our table has 10,000,000 rows with 10 data rows per block and 100 index entries per block. Therefore to read 10% of the table via an index scan and table lookup, we would have to read 10,000 (1,000,000/100) index blocks plus 100,000 (1,000,000/10) data blocks. That's 110,000 blocks in total.

However this assumes that the data is stored in order which means that we only retrieve the blocks of data that we want. If the data is not sorted then the worst case is that we would have to read 1 block for each row of data i.e. 1,000,000 blocks, which would would give us a worst case total of 1,010,000 blocks.

For a full table scan the maths is easy: (10,000,000 rows)/(10 rows per block) = 1,000,000 blocks. This is less than the worst case scenario for an indexed read, but more than the best case scenario for an indexed read. This would seem to suggest that if you sort your data before loading, an indexed read would be faster than a full-table scan.

Whilst it is true that pre-sorting the data of very large table will improve performance , it is not necessarily correct that the read via the index will be better overall. We also need to take into account what happens to the blocks stored in the buffer cache of the Oracle SGA and the impact this will have on other users of the database.

Let's look at the effect on the buffer cache of reading many data blocks via an index. As we know, data and index blocks are stored in the buffer cache by Oracle for reuse by other queries by being marked as least recently used when we do an indexed read. However, those data blocks read by a full table scan are quickly aged out of the buffer cache, because they are not marked as least recently used.

What this means is that the large number of blocks (1,010,000) of index and table data read via the indexed read of our table will be saved in the SGA flushing practically all other data from it - which will obviously have an effect on other users.

Conversely, when a full table scan is performed only the last blocks read are held in the SGA (the actual number is determined by the multi-block read count) so the impact on other users of the database would be minimal.

1.3 Summary

To decide whether or not a full-table scan would be better than an indexed read, for a large table you need to consider what proportion of the table the query will retrieve from your Oracle database and consider the likely effect of that on other users. The denser the data, the more efficient a full table scan is for very large tables, but generally if you're reading more than 1-10% of a very large table, a full table scan would be more efficient than an index scan and table lookup.

For small tables you will get much better performance from your Oracle database by caching the table in its entirety (so that it is always in memory), or by using an index-organised table then you will by relying on indexes. Having said that, every table should have a primary key index to guarantee uniqueness, but you don't have to use it.

More Oracle Tips

Read the next article on Oracle performance tuning in the series and learn how and when to use indexes.

Also see the Oracle resources section for other web sites and recommended books etc.

--------------------------------------

Are your Oracle developments over running, over budget or just failing to deliver? Discover how to develop your Oracle systems better, faster and smarter with help from our top consultants . Contact us for more info.

Looking to sky-rocket productivity, save time and reduce costs ? Learn the short cuts with high quality Oracle training from our top consultants . See the Oracle training page for more info. Details of our Oracle training courses including locations and dates are available here. For booking or further details, just click here and complete the form to have somebody contact you, or call 0845 0031320 .

Looking for more Oracle tips and tricks? Subscribe to our ezine. Published monthly, it's jam-packed full of tips and tricks to help you make more of your Oracle systems and save you hours searching for information. Subscribe today and your first issue will soon be winging its way to your mailbox.

Smartsoft Computing Ltd
Bristol, England

Tel: 0845 0031320
Contact Us

Click here to view our privacy policy .

This site uses woopra.com to gather statistical information about our visitors. View woopra privacy policy .

Oracle is a registered trademark of Oracle Corporation and/or its affiliates.

© Copyright Smartsoft Computing Ltd 2001-2009. All rights reserved.