---
title: "Query optimization: statistics"
description: "How the CoraDB optimizer uses statistics: UPDATE STATISTICS, GET STATISTICS, index cardinality and the corasql ;info commands."
url: "https://www.coradb.ai/manual/sql/tuning.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/tuning.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb update statistics, get statistics, optimizer statistics, index cardinality, query optimization, graph statistics, info stats"
---

# Query optimization: statistics

The CoraDB query optimizer chooses how to run a query (which indexes to use, the join order and join methods) from estimates of how many rows each step produces. These estimates come from the *statistics* of tables and indexes: number of rows and pages, and the number of distinct keys of each index.

| Topic | Page |
|---|---|
| Keeping statistics up to date, checking them | This page |
| Reading query plans, profiling execution with SET TRACE | [Query plans and profiling](https://www.coradb.ai/manual/sql/tuning_plan.html) |
| Steering the optimizer with SQL hints and index hints | [SQL and index hints](https://www.coradb.ai/manual/sql/tuning_hints.html) |
| Filtered, function-based and shared indexes | [Special indexes](https://www.coradb.ai/manual/sql/tuning_special_index.html) |
| Covering indexes, ORDER BY / GROUP BY without sorting, key ranges, skip and loose scans | [Index optimizations](https://www.coradb.ai/manual/sql/tuning_index_opt.html) |


## Updating statistics

Statistics are updated automatically by DDL such as **CREATE TABLE**, **CREATE INDEX** and **DROP INDEX**, but not by **INSERT**, **UPDATE** or **DELETE**. After loading or deleting a large share of a table's rows, update its statistics so that the optimizer works with realistic numbers:

```sql
UPDATE STATISTICS ON table_name [, table_name ...] [WITH FULLSCAN];
UPDATE STATISTICS ON ALL CLASSES [WITH FULLSCAN];
UPDATE STATISTICS ON CATALOG CLASSES [WITH FULLSCAN];
```

- Without **WITH FULLSCAN**, the statistics are estimated from a sample of the data, which is enough in most cases. **WITH FULLSCAN** reads all data and costs correspondingly more; use it sparingly on large tables.
- **ALL CLASSES** updates every table of the database; **CATALOG CLASSES** updates the system catalog tables.

```sql
UPDATE STATISTICS ON product, orders;
UPDATE STATISTICS ON product WITH FULLSCAN;
UPDATE STATISTICS ON ALL CLASSES;
```

Each update writes a pair of `NOTIFICATION` messages to the server error log, which shows when it started and finished:

```text
... NOTIFICATION ... CODE = -1114 ... Started to update statistics (class "product", oid : 0|204|64).
... NOTIFICATION ... CODE = -1115 ... Finished to update statistics (class "product", oid : 0|204|64, error code : 0).
```


## Checking statistics information

### GET STATISTICS

**GET STATISTICS** returns one statistics value of a table:

```sql
GET STATISTICS 'item' OF table_name [ { INTO | TO } :variable ];
```

| Item | Value |
|---|---|
| `'#objects'` | Number of rows |
| `'#pages'` | Number of heap pages |
| `'#attributes'` | Number of columns |
| `'#keys(column:n)'` | Number of distinct keys of the *n*-th index on *column* (estimated unless the statistics were updated **WITH FULLSCAN**) |

```sql
GET STATISTICS '#objects' OF product;        -- 2115
GET STATISTICS '#attributes' OF product;     -- 5
GET STATISTICS '#keys(id:1)' OF product;     -- about 2,000 (sampled)
```

The values reflect the last statistics update, not necessarily the current contents of the table.

### SHOW INDEX

[SHOW INDEX](https://www.coradb.ai/manual/sql/query/show.html#show-index) lists the indexes of a table with their estimated cardinality (number of distinct keys):

```sql
SHOW INDEX FROM product;
```

| Table | Non_unique | Key_name | Column_name | Cardinality |
|---|---|---|---|---|
| product | 1 | idx_product_name | name | 262 |
| product | 0 | pk_product_id | id | 2115 |

(Selected columns.) Product names repeat in the sample data, so the name index has far fewer distinct keys than the table has rows.

### ;info stats in corasql

The corasql session command `;info stats` prints all statistics of a table: rows, pages, and for each column its type, minimum and maximum and its B+tree statistics.

```text
corasql> ;info stats product
CLASS STATISTICS
****************
 Class name: product  Timestamp: ...
 Total pages in class heap: 13
 Total objects: 2115
 Number of attributes: 5
 Attribute: id
    id: 0
    Type: DB_TYPE_INTEGER
    ...
    B+tree statistics:
        BTID: { ... }
        Cardinality: ... , Total pages: ... , Leaf pages: ... , Height: ...
 ...
```


### Checking graph statistics

For graph queries, the optimizer also uses statistics on vertex and edge tables and on how they are connected. The corasql session command `;info graph_stats` shows them. Vertex and edge table names are case-sensitive.

```text
;info graph_stats <subcommand> [arguments]
```

| Subcommand | Shows |
|---|---|
| `vertex_label_elements <vertex_table>` | Number of vertices with this label |
| `edge_label_elements <edge_table>` | Number of edges with this label |
| `relationships_between_specified_label <vertex1> <vertex2>` | Edges of all types from *vertex1* to *vertex2* |
| `relationships_between_specified_label <vertex1> <edge> <vertex2>` | Edges of one type from *vertex1* to *vertex2* |
| `relationships_from_specified_label <vertex> <edge>` | Outgoing edges of one type from a vertex label |
| `relationships_to_specified_label <edge> <vertex>` | Incoming edges of one type to a vertex label |

Examples on the e-commerce graph:

```text
;info graph_stats vertex_label_elements product
;info graph_stats edge_label_elements likes
;info graph_stats relationships_between_specified_label customer product
;info graph_stats relationships_between_specified_label customer likes product
;info graph_stats relationships_from_specified_label customer follows
;info graph_stats relationships_to_specified_label manufactures product
```

Use them to see the size and shape of the graph: which labels are the largest, how many edges a typical vertex has, and therefore which direction of a traversal is cheaper. Graph statistics reflect the current state of the database and may be cached and refreshed periodically, depending on the configuration.
