CHAPTER 1 ■ CORE SQL
14
When ordered output is requested, Oracle must take the final set of data after all other clauses have been
processed and sort them as specified. The size of the data that needs to be sorted is important. When I say size, I mean
total bytes of data in the result set. To estimate the size of the dataset, multiply the number of rows by the number of
bytes per row. The bytes per row are determined by summing the average column lengths of each of the columns in
the SELECT list.
The example query requests only the customer_id and orders_ct column values in the SELECT list. Let’s use ten
as our estimated bytes-per-row value. In Chapter 6, I show you where to find the optimizer’s estimate for this value.
So, given that we only have two rows in the result set, the sort size is actually quite small, approximately 20 bytes.
Remember, this is only an estimate, but the estimate is an important one.
Small sorts should be accomplished entirely in memory whereas large sorts may have to use temporary disk
space to complete the sort. As you may likely deduce, a sort that occurs in memory is faster than a sort that must use
disk. Therefore, when the optimizer estimates the effect of sorting data, it has to consider how big the sort is to adjust
how to accomplish getting the query result in the most efficient way. In general, consider sorts as a fairly expensive
overhead to your query processing time, particularly if the size of your result set is large.
The INSERT Statement
The INSERT statement is used to add rows to a table, partition, or view. Rows can be inserted in either a single-table
or multitable method. A single-table insert inserts values into one row of one table either by specifying the values
explicitly or by retrieving the values using a subquery. The multitable insert inserts rows into one or more tables and
computes the row values it inserts by retrieving the values using a subquery.
Single-Table Inserts
The first example in Listing 1-16 illustrates a single-table insert using the VALUES clause. Each column value is entered
explicitly. The column list is optional if you include values for each column defined in the table. However, if you only
want to provide values for a subset of the columns, you must specify the column names in the column list. A good
practice is to include the column list regardless of whether you specify values for all the columns. Doing so acts to
self-document the statement and also helps reduce possible errors that might occur in the future should someone add
a new column to the table.
Listing 1-16. Single-Table Insert
SQL> insert into hr.jobs (job_id, job_title, min_salary, max_salary)
2 values ('IT_PM', 'Project Manager', 5000, 11000) ;
1 row created.
SQL> insert into scott.bonus (ename, job, sal)
2 select ename, job, sal * .10
3 from scott.emp;
14 rows created.
The second example in Listing 1-16 illustrates an insert using a subquery, which is a very flexible option for
inserting rows. The subquery can be written to return one or more rows. Each row returned is used to supply column
values for the new rows to be inserted. The subquery can be as simple or as complex as needed to satisfy your needs.
In this example, we use the subquery to compute a 10 percent bonus for each employee based on his or her current
salary. The bonus table actually has four columns, but we only populate three of them with this insert. The comm
column isn’t populated with a value from the subquery and we do not include it in the column list. Because we don’t
include this column, the value for it is null. Note that if the comm column had a NOT NULL constraint, you would get a
constraint error and the statement would fail.