October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Cost-Based Query Optimizer: What Its Cost Estimates Mean

A cost-based query optimizer compares estimated resource costs for possible execution plans. Its choice depends on cardinality estimates, statistics, and a database-specific model—not a guarantee of the shortest runtime.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A cost-based query optimizer estimates the resource use of alternative ways to execute a query, then chooses the plan with the lowest estimated cost among the plans it considers. It relies on estimates of how many rows operations will process and on a database-specific cost model. That internal cost is useful for comparing plans; it is not a promise of elapsed time.

What a cost-based query optimizer does

SQL describes the result a user wants, not a mandatory sequence of physical steps for retrieving it. The optimizer translates that request into an execution plan: a set and order of operations the database can carry out. It compares possible strategies and selects one based on estimated cost. Microsoft describes SQL Server’s optimizer as cost-based, meaning it selects plans with the lowest estimated processing cost (Microsoft Learn: Query processing architecture guide).

As an Amazon Associate I earn from qualifying purchases.

The choice can involve different ways to access data, join tables, order joins, or transform parts of a query. A plan might, for example, use an index rather than scan a table, or choose one join method and order instead of another. Which alternatives are available depends on the database and query.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How the optimizer estimates and compares plans

  1. Read the query and schema. The optimizer identifies the requested operations, relevant tables, columns, predicates, and relationships.
  2. Generate candidate plans. It considers available access paths, join methods, join orders, and applicable query transformations.
  3. Estimate data flow. For each operation, it estimates cardinality—the number of rows produced. Selectivity is the estimated fraction of rows a predicate retains. Statistics about tables and columns, including histograms, help inform these estimates.
  4. Estimate resource use. A database-specific cost model estimates the work associated with candidate operations. Oracle describes inputs that can include estimated I/O, CPU, memory, cardinality, input sizes, data distribution, and access structures (Oracle Database 26: Query optimizer concepts).
  5. Choose among evaluated candidates. The optimizer selects the plan with the lowest estimated cost among the alternatives it evaluated.

These stages are connected: a row-count estimate affects downstream work estimates. If the optimizer expects few rows, repeated index lookups in a nested-loops join may appear economical. If it expects many rows, a different join approach or access path may be estimated to require less work.

Why statistics and cardinality estimates matter

Statistics help the optimizer estimate how data is distributed and how many rows predicates will match. SQL Server documentation says its cardinality estimates are derived primarily from histograms created when indexes or statistics are created, and recommends having relevant automatic or manual statistics available (Microsoft Learn: Statistics; Microsoft Learn: Cardinality estimation).

An inaccurate estimate can change the optimizer’s assessment of later operations, such as the cost of repeated lookups or sorting. This is one reason a plan may perform poorly even when the optimizer is functioning as designed: it made a choice based on estimates that did not match the data encountered during execution.

What “lowest cost” does—and does not—mean

“Lowest cost” means lowest according to the optimizer’s model and among the plans it considered. It does not necessarily mean the globally optimal plan across every mathematically possible strategy. Searching every combination can be impractical; PostgreSQL documents using its genetic query optimizer above a join-count threshold to find a reasonable plan in a reasonable amount of time (PostgreSQL 17: Planner/Optimizer).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Nor is an optimizer cost a direct measure of elapsed seconds. Oracle describes cost as an internal estimate of resource use, specific to the query and optimizer environment. A plan with lower estimated cost can run faster, slower, or in about the same time as another plan. Costs are therefore not meaningful as a direct comparison across different queries or optimizer modes (Oracle Database 18: Query optimizer concepts).

Rank #3
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to use estimated cost when investigating a slow query

An explain plan shows the optimizer’s proposed operations and estimates; it is not, by itself, a runtime measurement. When a query is unexpectedly slow, compare estimated row counts with actual row counts in the database’s plan or runtime diagnostics, where available. Large differences can point toward a cardinality-estimation issue and help focus investigation on relevant statistics, data distribution, or predicates. The specific commands and diagnostic displays vary by database product and version.

Cost-based optimization is a way of ranking execution alternatives, not a guarantee that every selected plan will be fast. To understand a particular plan, examine its estimates, operations, and runtime evidence in the context of the database implementation being used.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.