When Oracle is estimating the ‘Cost’ for certain queries, does it actually look at the amount of data (rows) in a table?
For example:
If I’m doing a full table scan of employees for name=’Bob’, does it estimate the cost by counting the amount of existing rows, or is it always a set cost?
In the default configuration, Oracle will check the table statistics (which you can look at by querying the
ALL_TABLESview – see the columnNUM_ROWS). Normally an Oracle job is run periodically to re-gather these statistics by querying part or all of the table.If the statistics haven’t been gathered (yet), the optimizer will (depending on the
optimizer_dynamic_samplingparameter) run a quick sample query on the table in order to calculate an estimate for the number of rows in that table.(To be more accurate, the cost of scanning a table is calculated not from the number of rows, but the number of blocks in the table (which you can see in the
BLOCKScolumn inALL_TABLES). It takes this number and divides it by a factor related to the multi-block read count to calculate the cost of that part of the plan.)