# Export and Import Statistics

+ You may need to export and import table statistics when
  + Using a standby database to collect statistics for very large table, when time for collecting statistics is insufficient
  + Verification changes, such as upgrading, adding or deleting columns, or index modifications
  + Diagnosing performance issue

+ Access the export URL to export the CBO statistics to a file
```
http://${tidb-server-ip}:${tidb-server-status-port}/stats/dump/${db_name}/${table_name}
```

+ Import the CBO statistics using the command **`LOAD STATS`** from the exported file
```
LOAD STATS 'file_name';
```
------------------------------------------------------------------------------
The process of importing and exporting statistics involves exporting statistics for a specific table or multiple tables and subsequently importing them into another database featuring the same table structure. This approach is useful in several scenarios:

- For very large tables (containing billions of rows or more), there may be insufficient time to collect statistics without adversely impacting performance, particularly when the current health status is already low, and performance is deteriorating. In such instances, a standby database can be employed. After collecting statistics in this standby database—which mirrors the data volume and distribution of the primary database—those statistics can be imported into the primary database to optimize execution plans.

- When changes such as upgrades, column additions or deletions, or index modifications are necessary, statistics can be exported from the production environment and imported into a testing environment. This allows for verification that the changes will function as expected.

- Occasionally, technical support from the original TiDB database engineers may be required. Providing table statistics and execution plans is essential for diagnosing optimizer issues. In these cases, exporting the statistics and submitting them to the engineers is necessary.

To export statistics, use the following method:
```
http://${tidb-server-ip}:${tidb-server-status-port}/stats/dump/${db_name}/${table_name}
```
- `${tidb-server-ip}` refers to the IP address of the TiDB Server host.
- `${tidb-server-status-port}` denotes the port number of the TiDB Server, which defaults to 4000.
- `${db_name}` represents the name of the database from which the statistics information is to be exported.
- `${table_name}` refers to the name of the table whose statistics are to be exported.

The exported statistics are typically stored in a JSON file. To import statistics, use the following command:
```
LOAD STATS 'file_name';
```

For detailed instructions on importing and exporting statistics, please consult the documentation: [Import and Export Statistics](https://docs.pingcap.com/tidb/stable/statistics#import-and-export-statistics).