IMPORT INTO is the fastest method to ingest data into CockroachDB but it requires taking the target table offline for the duration of the import. IMPORT INTO is a good choice for initial data migrations and data migrations that can tolerate table downtime. If you cannot tolerate table unavailability, we recommend using COPY FROM instead.
Import performance primarily depends on the amount of data that you want to import. However, there are three actions you should take before importing that have a significant impact on the amount of time it takes to run an import:
If the import size is less than 100 GiB, then you do not need to do anything to optimize performance. For such small datasets, the import should run quickly, regardless of the settings.
Choose a performant import format
Different import file formats do not have the same performance due to the way they are processed by CockroachDB. The fastest import file formats are:CSVorDELIMITED DATA(both have about the same import performance).AVRO.
CSV or AVRO. These formats can be processed in parallel by all the nodes in the cluster, which increases performance. To import in these formats, use IMPORT INTO.
Import the schema separately from the data
When importing into a new table, split your dump data into two files:- A SQL file containing the table schema.
- A CSV, delimited, or AVRO file containing the table data.
cockroach sql, or use a third-party schema migration tool such as Alembic, Flyway, or Liquibase.
Then use the IMPORT INTO statement to import the CSV data into the newly created table:
Import into a schema with secondary indexes
When importing data into a table with secondary indexes, the import job will ingest the table data and required secondary index data concurrently. This may result in a longer import time compared to a table without secondary indexes. However, this typically adds less time to the initial import than following it with a separate pass to add the indexes. As a result, importing tables with their secondary indexes is the default workflow, an effective strategy for most migrations. However, in large imports (that is, datasets larger than 100 GiB in total size), it may be preferable to temporarily remove the secondary indexes from the schema, perform the import, and then re-create the indexes separately. Write operations on tables with many secondary indexes take longer to complete, and importing large datasets have a greater risk of timeouts. Removing the table’s secondary indexes allows you to separate the initial data import from the secondary index creation operation, and the total import time is lower. Separating these operations also provides increased visibility into each operation’s progress, and the ability to retry each operation independently if you encounter errors or timeouts. When recreating the secondary indexes, execute theCREATE INDEX statements one at a time for best performance. For example:
Temporarily remove foreign keys
When importing data into a table with foreign keys, temporarily remove the foreign keys, and add them after the initial migration with anALTER TABLE statement.
Data type sizes
Above a certain size, many data types such asSTRINGs, DECIMALs, ARRAY, BYTES, and JSONB may run into performance issues due to write amplification. See each data type’s documentation for its recommended size limits.
Split your data into multiple files
Splitting the import data into multiple files can have a significant impact on the import performance. The following formats support multi-file import usingIMPORT INTO:
CSVDELIMITED DATAAVRO
You can split the data into more files than you have nodes. CockroachDB will process the files in parallel across the cluster. When splitting the data Cockroach Labs recommends splitting it into a multiple of the number of nodes in your cluster, if possible. For example, if you have a 3 node cluster, split the dataset into 9, 27, or 300 files.Cockroach Labs recommends keeping the files to a maximum file size of 4 GB unless the files are streamed (for example, cloud storage locations will stream the data to CockroachDB), and to keep each file size similar across the dataset. For example, if you are importing a 9 GB dataset that was split into 3 files into a 3 node cluster, keep each file around 3 GB in size if possible. Don’t split the data into two 4 GB files, and one 1 GB file.
File storage during import
During migration, all of the features ofIMPORT INTO that interact with external file storage assume that every node has the exact same view of that storage. In other words, in order to import from a file, every node needs to have the same access to that file.
If a node runs out of disk space while processing an import job, CockroachDB will pause the import job. Make sure each node has enough free disk space to process the file and store the data.

