SingleStore Cheat Sheet
Notebook
Note
This notebook can be run on a Free Starter Workspace. To create a Free Starter Workspace navigate to Start using the left nav. You can also use your existing Standard or Premium workspace with this Notebook.
SingleStore Database Cheat Sheet
List of useful commands for SingleStore SQL and Kai (MongoDB API) operations
Important Notes
SingleStore Core Concepts
Every distributed and columnstore table MUST have a SHARD KEY defined
Reference tables don't need a SHARD KEY as they are replicated to all nodes
SingleStore supports both rowstore (default) and columnstore table types
Hash indexes are recommended for equality comparisons
JSON operations are optimized for performance in SingleStore
Use Reference tables for lookup data that needs to be available on all nodes
Vector Operations Tips
Vector dimensions must be specified at table creation
Normalize vectors before cosine similarity calculations
Choose appropriate index metric based on your use case
Vector operations support AI/ML workloads
Combine with full-text search for hybrid search capabilities
Available both in SQL and through SingleStore Kai (MongoDB API)
For the most up-to-date information, refer to the official SingleStore documentation at https://singlestore.com/docs.
Database Operations
In [1]:
%%sql# Show DatabasesSHOW DATABASES;
Out [1]:
In [2]:
%%sql# Create DatabaseCREATE DATABASE database_name; --Note this will not work on free tier due to one DB constraint
In [3]:
%%sql# Use DatabaseUSE database_name;
In [4]:
%%sql# Drop DatabaseDROP DATABASE database_name; -- Use with extreme caution
Table Operations
In [5]:
%%sql# Create Distributed TableCREATE TABLE posts (id BIGINT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255),body TEXT,category VARCHAR(50),created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,SHARD KEY (id));
Out [5]:
In [6]:
%%sql# Create Reference TableCREATE REFERENCE TABLE categories (id INT PRIMARY KEY,name VARCHAR(50)-- No SHARD KEY needed for reference tables);
Out [6]:
In [7]:
%%sql# Create Columnstore TableCREATE TABLE analytics (id BIGINT,event_type VARCHAR(50),timestamp TIMESTAMP,data JSON,SORT KEY (timestamp),SHARD KEY (id) -- Required for columnstore tables) ENGINE=columnstore;
Out [7]:
Table Management
In [8]:
%%sql# Show tablesSHOW TABLES;# Describe tableDESCRIBE posts;# Drop tableDROP TABLE posts;
Out [8]:
Data Manipulation
In [9]:
%%sql# Insert single rowINSERT INTO posts (title, body, category)VALUES ('Post One', 'Body of post one', 'News');# Insert multiple rowsINSERT INTO posts (title, body, category) VALUES('Post Two', 'Body of post two', 'Technology'),('Post Three', 'Body of post three', 'News');
In [10]:
%%sql# Select Data# Select all rowsSELECT * FROM posts;# Select specific columnsSELECT title, category FROM posts;# Select with conditionSELECT * FROM posts WHERE category = 'News';
In [11]:
%%sql# Update DataUPDATE postsSET body = 'Updated body'WHERE title = 'Post One';
In [12]:
%%sql# Delete DataDELETE FROM posts WHERE title = 'Post One';
SingleStore Pipelines
Pipelines are used to bring data into SingleStore tables from different sources, for example an S3 bucket
Create Pipeline
In [13]:
%%sql#Create PipelineCREATE PIPELINE SalesData_Pipeline ASLOAD DATA S3 's3://singlestoreloaddata/SalesData/*.csv'CONFIG '{ "region": "ap-south-1" }'INTO TABLE SalesDataFIELDS TERMINATED BY ','LINES TERMINATED BY '\n'IGNORE 1 lines;
Start Pipeline
In [14]:
%%sqlSTART PIPELINE SalesData_Pipeline;
Check pipeline status
In [15]:
%%sqlSELECT * FROM information_schema.pipelines_filesWHERE pipeline_name = "SalesData_Pipeline";
Out [15]:
DATABASE_NAME | PIPELINE_NAME | SOURCE_TYPE | FILE_NAME | FILE_SIZE | FILE_STATE |
---|
Stop pipeline
In [16]:
%%sqlSTOP PIPELINE [IF RUNNING] pipeline_name [DETACH];
Drop Pipeline
In [17]:
%%sqlDROP PIPELINE [IF EXISTS] pipeline_name;
SingleStore Specific Features
JSON Operations
In [18]:
%%sql# Create table with JSON columnCREATE TABLE json_posts (id BIGINT AUTO_INCREMENT PRIMARY KEY,data JSON,SHARD KEY (id));# Insert JSONINSERT INTO json_posts (data)VALUES ('{"title": "Post One", "tags": ["news", "events"]}');# Query JSONSELECT JSON_EXTRACT_JSON(data, '$.title') as titleFROM json_posts;
Out [18]:
Vector Operations
In [19]:
%%sql# Create table with vector columnCREATE TABLE embeddings (id BIGINT AUTO_INCREMENT PRIMARY KEY,description TEXT,embedding VECTOR(1536), -- Specify vector dimensionSHARD KEY (id));
Out [19]:
In [20]:
%%sql# Create vector index using dot productALTER TABLE embeddings ADD VECTOR INDEX idx_embedding (embedding)INDEX_OPTIONS '{"metric_type": "DOT_PRODUCT"}';
Out [20]:
In [21]:
%%sql# Vector search examples# Find similar vectors using dot productSELECT id, description, DOT_PRODUCT(embedding, '[0.1, 0.2, ...]') as similarityFROM embeddingsORDER BY similarity DESCLIMIT 10;# Hybrid search combining full-text and vector searchSELECT id, description,DOT_PRODUCT(embedding, '[0.1, 0.2, ...]') as vector_score,MATCH(description) 'search terms' as text_scoreFROM embeddingsWHERE MATCH(description) 'search terms'ORDER BY (vector_score * 0.7 + text_score * 0.3) DESC;
SingleStore Kai (MongoDB API)
MongoDB Client Connection
mongodb://username:password@hostname:27017/database
In [22]:
%%sql# MongoDB-style commands# Show databasesshow dbs# Use databaseuse mydb# Show collectionsshow collections# Create collectiondb.createCollection('users')
Details
About this Template
Get started with SingleStore quickly with common commands
This Notebook can be run in Shared Tier, Standard and Enterprise deployments.
Tags
License
This Notebook has been released under the Apache 2.0 open source license.