Insert Analyse in Oracle(related to Index)
2014-12-18 11:38
211 查看
Insert Analyse in Oracle(related to Index)
The number of indexes on a table is the most dominant factor for insertperformance. The more indexes a table has, the slower the execution becomes.
The insert statement is the only operation that cannot directly benefit from indexing because it has no where clause.
Adding a new row to a table involves several steps. First, the database must find a place to store the row. For a regular heap table—which has no particular row order—the database can take any table block that has enough free space. This is a very simple and
quick process, mostly executed in main memory. All the database has to do afterwards is to add the new entry to the respective data block.
If there are indexes on the table, the database must make sure the new entry is also found via these indexes. For this reason it has to add the new entry to each and every index on that table. The number of indexes is therefore a multiplier for the cost of
an insert statement.
Mastering SQL Performance
There is something for everyone:
training, tuning and literature on SQL performance
http://winand.at/
Moreover, adding an entry to an index is much more expensive than inserting one into a heap structure because the database has to keep the index order and tree balance. That means the new entry cannot be written to any block—it belongs to a specific leaf
node. Although the database uses the index tree itself
to find the correct leaf node, it still has to read a few index blocks for the tree traversal.
Once the correct leaf node has been identified, the database confirms that there is enough free space left in this node. If not, the database splits the leaf node and distributes the entries between the old and a new node. This process also affects the reference
in the corresponding branch node as that must be duplicated as well. Needless to say, the branch node can run out of space as well so it might have to be split too. In the worst case, the database has to split all nodes up to the root node. This is the only
case in which the tree gains an additional layer and grows in depth.
The index maintenance is, after all, the most expensive part of the insertoperation. That is also visible in Figure 8.1:
the execution time is hardly visible if the table does not have any indexes. Nevertheless, adding a single index is enough to increase the execute time by a factor of a hundred. Each additional index slows the execution down further.
The first index makes the greatest difference.
To optimize insert performance, it is very important to keep the number of indexes small.
Use indexes deliberately and sparingly, and avoid redundant indexes whenever possible. This is also beneficial for delete and updatestatements.
Considering insert statements only, it would be best to avoid indexes entirely—this yields by far the best insert performance.
However tables without indexes are rather unrealistic in real world applications. You usually want to retrieve the stored data again so that you need indexes to improve query speed. Even write-only log tables often have a primary key and a respective index.
Nevertheless, the performance without indexes is so good that it can make sense to temporarily drop all indexes while loading large amounts of data—provided the indexes are not needed by any other SQL statements in the meantime. This can unleash a dramatic
speed-up which is visible in the chart and is, in fact, a common practice in data warehouses.
If you like my way of explaining things, you’ll love my
book.
How would Figure 8.1 change
when using an index organized table orclustered
index?
Is there any indirect way an insert statement could possibly benefit from indexing? That is, could
an additional index make an insertstatement faster?
The number of indexes on a table is the most dominant factor for insertperformance. The more indexes a table has, the slower the execution becomes.
The insert statement is the only operation that cannot directly benefit from indexing because it has no where clause.
Adding a new row to a table involves several steps. First, the database must find a place to store the row. For a regular heap table—which has no particular row order—the database can take any table block that has enough free space. This is a very simple and
quick process, mostly executed in main memory. All the database has to do afterwards is to add the new entry to the respective data block.
If there are indexes on the table, the database must make sure the new entry is also found via these indexes. For this reason it has to add the new entry to each and every index on that table. The number of indexes is therefore a multiplier for the cost of
an insert statement.
Mastering SQL Performance
There is something for everyone:
training, tuning and literature on SQL performance
http://winand.at/
Moreover, adding an entry to an index is much more expensive than inserting one into a heap structure because the database has to keep the index order and tree balance. That means the new entry cannot be written to any block—it belongs to a specific leaf
node. Although the database uses the index tree itself
to find the correct leaf node, it still has to read a few index blocks for the tree traversal.
Once the correct leaf node has been identified, the database confirms that there is enough free space left in this node. If not, the database splits the leaf node and distributes the entries between the old and a new node. This process also affects the reference
in the corresponding branch node as that must be duplicated as well. Needless to say, the branch node can run out of space as well so it might have to be split too. In the worst case, the database has to split all nodes up to the root node. This is the only
case in which the tree gains an additional layer and grows in depth.
The index maintenance is, after all, the most expensive part of the insertoperation. That is also visible in Figure 8.1:
the execution time is hardly visible if the table does not have any indexes. Nevertheless, adding a single index is enough to increase the execute time by a factor of a hundred. Each additional index slows the execution down further.
Figure 8.1. Insert Performance by Number of Indexes
Note
The first index makes the greatest difference.To optimize insert performance, it is very important to keep the number of indexes small.
Tip
Use indexes deliberately and sparingly, and avoid redundant indexes whenever possible. This is also beneficial for delete and updatestatements.Considering insert statements only, it would be best to avoid indexes entirely—this yields by far the best insert performance.
However tables without indexes are rather unrealistic in real world applications. You usually want to retrieve the stored data again so that you need indexes to improve query speed. Even write-only log tables often have a primary key and a respective index.
Nevertheless, the performance without indexes is so good that it can make sense to temporarily drop all indexes while loading large amounts of data—provided the indexes are not needed by any other SQL statements in the meantime. This can unleash a dramatic
speed-up which is visible in the chart and is, in fact, a common practice in data warehouses.
If you like my way of explaining things, you’ll love my
book.
Think About It
How would Figure 8.1 changewhen using an index organized table orclustered
index?
Is there any indirect way an insert statement could possibly benefit from indexing? That is, could
an additional index make an insertstatement faster?
相关文章推荐
- Why And When To Use Pre-Update and Pre-Insert Triggers In Oracle Forms
- to create table if table do not exist -- to backup and restore table in oracle
- connect to Oracle in Ruby on Rails.
- System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Food' when IDENTITY_INSERT is set to OFF
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- Using MERGE in SQL Server to insert, update and delete at the same time
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- how to reset sys password in oracle
- How to Insert or Update records in SSIS DataFlow
- How to place all WPF commands and related events centrally in a static class
- Use Shell to Insert Oracle
- Oracle Letter Proposes to Buy BEA Systems for $17.00 per Share in Cash
- How to create Function/Procedure in Oracle?
- Whether to show a related link box in the post composer
- MS bug "The connection pool" in Oracle 10g and the data sort according to specified filed on DataGrid control.
- Some Samples Of Oracle Function In Order To Relax Java Program.
- How to enable Apache, OC4J and OPMN logging in Oracle Applications R12
- A issue related to "incorect SelectedIndex when remove a row from DataGrid"
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- How to get the rowid when insert the data to Oracle database