Interact with your tables and data inside CARTO, as if you were running SQL statements.

This component is still under support but it will not be further developed. We don’t recommend starting new projects with it as it will eventually become deprecated. Instead, learn more about our new APIs here

Query Optimizations

There are some tricks to consider when using the SQL API that might make your application a little faster.

  • Only request the fields you need. Selecting all columns will return a full version of your geometry in the_geom, as well as a reprojected version in the_geom_webmercator
  • Use PostGIS functions to simplify and filter out unneeded geometries when possible. One very handy function is, ST_Simplify
  • Remember to build indexes that will speed up some of your more common queries. For details, see Creating Indexes
  • Use cartodb_id to retrieve specific rows of your data, this is the unique key column added to every CARTO table. For a sample use case, view the _Faster data updates with CARTO blogpost
  • Check if your polygons contain an excessive number of vertices, and subdivide them if they do. Learn how in this Subdivide All the Things blogpost.

Creating Indexes

In order to better improve map performance, advanced users can use the SQL API to add custom indexes to their data. Creating indexes is useful if you have a large dataset with filtered data. By indexing select data, you are improving the performance of the map and generating the results faster. The index functionality is useful in the following scenarios:

  • If you are filtering a dataset by values in one or a more columns
  • If you are regularly querying data through the SQL API, and filtering by one or a more columns
  • If you are creating Torque maps on very large datasets. Since Torque maps are based on time-sensitive data (i.e. a date or numeric column), creating an index on the time data is optimal

Indexed data is typically a single column representing filtered data. To create a single column index, apply this SQL query to your dataset:

CREATE INDEX idx_{DATASET NAME}_{COLUMN_NAME} ON {DATASET_NAME} ({COLUMN_NAME})

Tip: You can also apply more advanced, multi-column indexes. Please review the full documentation about PostgreSQL Indexes before proceeding.

Note: Indexes are allocated towards the amount of data storage associated with your account. Be mindful when creating custom indexes. Note that indexes automatically generated by CARTO are not counted against your quota. For example, the_geom and cartodb_id columns. These columns are used to index geometries for your dataset and are not associated with storage.