Full table scan

Full table scan (also known as sequential scan) is a scan made on a database where each row of the table under scan is read in a sequential (serial) order and the columns encountered are checked for the validity of a condition.[1] Full table scans [2] are usually slowest method of scanning a table due to the heavy amount of I/O reads required from the disk which consists of multiple seeks as well as costly disk to memory transfers.

Overview

In a database, a query that is not indexed results full table scan, where the database processes each record of the table to find all records meeting the given requirements. Even if the query selects just a few rows from the table, all rows in the entire table will be examined. This usually results in suboptimal performance but may be acceptable with very small tables or when the overhead of keeping indexes up to date is high.

When the Optimizer Considers a Full Table Scan[3]

The most important factor in choosing depends on speed. It means that full table scan should be used when it is the fastest and cannot use a different access path. Several full table scan examples are as followed.

Obviously, the optimizer must use full table scan without index.

The cost of full table scan is less than index range scan due to small table.

Query want to count the number of null column in typical index. However, SELECT COUNT(*) cannot count the number of null column.

The number of return rows is too large and takes nearly 100% in the whole table. These rows are unselective.

The number of rows in table was low before, but now the number grows higher. The table statistics does not update and regard it as small table. The optimizer does not know that the index is faster.

The high degree of parallelism table distorts the optimizer from a true way, because optimizer would use full table scan.

The hint lets optimizer to use full table scan.


How to Avoid Full table scan

If a table is large, full table scan should be avoided due to high cost.

Example

A full table scan example: The example shows the SQL statement of searching items with id is bigger than 10 from table1

   SELECT category_id1
   FROM table1
   WHERE category_id2 > 10;

In this situation, the database system needs to scan full table to find the content which fits the requirement.

The other example shows the SQL statement of searching employee information by their first name order

   SELECT * 
   FROM employees 
   ORDER BY first_name;

In this situation, the database system also needs to scan full table to compare the first name.

Pros and Cons

Pros:

Cons:


See also

References

  1. "Avoiding Table Scans". Oracle. 2011.
  2. "Which is Faster: Index Access or Table Scan?". Microsoft TechNet. 2002.
  3. "Optimizer Access Paths". Oracle. 2013.
  4. "How to Avoid Full Table Scans". MySQL. 2016.


This article is issued from Wikipedia - version of the 11/8/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.