# Selectivity, Cardinality, and Cost
+ **Selectivity = (Number of rows satisfying the query condition) / (Total number of rows)**
+ Selectivity is expressed as a value between **0.0 (high)** and **1.0 (low)**
  + High selectivity: Small proportion of rows
  + Low selectivity: Big proportion of rows
+ **Cardinality = (Total number of rows) x (Selectivity)**
+ Cardinality is important to determine tasks (join, filter, and sort) costs
+ Task cost is important to determine network, CPU, memory, and disk I/O costs with particular factors applied internally
+ Cost-Based optimization favors the path with the lowest cost
------------------------------------------------------------------------------
Cost-based optimization aims to calculate the total cost of a specific execution plan. Selectivity and cardinality are crucial considerations in estimating execution costs.

Selectivity is the fraction of rows selected from a dataset, which can be a table, view, or the result set from an operator like a join or `GROUP BY`. It relates to query predicates, such as `spectral_type = 'G2 V'`, or combinations of predicates, like `spectral_type = 'G2 V' AND distance_from_earth < 1`. A predicate acts as a filter, determining how many rows are filtered out. Therefore, selectivity indicates the percentage of rows passing the predicate. The selectivity value ranges from `0.0` to `1.0`, where `0.0` means no rows are selected, and `1.0` means all rows are selected from the dataset.

Cardinality reflects the estimated number of rows an operation in a query's execution plan will retrieve. This is typically based on a table, view, or results from a join or `GROUP BY` operation. Understanding the cardinality of the input data is essential when evaluating the cost of a join operation.

Sorting costs depend on the size and number of rows to be sorted, making accurate cardinality estimates crucial for determining sort costs.

In addition to considering network, CPU, memory, and disk I/O—primary factors in cost calculation—the optimizer also considers concurrency and other factors.