From SQL to Redisearch: Mapping SQL commands to Redisearch

From SQL to Redisearch: Mapping SQL commands to Redisearch

Daily short news for you
  • Competition in the model race is becoming increasingly fierce, with tech companies not wanting to be left behind. Llama 4 Scout and Llama 4 Maverick are the latest open-source models from Meta, touted for their superior performance, even surpassing the most advanced models like GPT-4.5, Claude Sonnet 3.7, and Gemini 2.0 Pro... However, alongside this, Scout and Maverick are facing criticism over cheating allegations. Should we trust Meta once again? 🤔

    Llama 4 Scandal: Meta’s release of Llama 4 overshadowed by cheating allegations on AI benchmark

    » Read more
  • Today, I accidentally came across the website notes.andymatuschak.org which has a very interesting note-taking method. Clicking on a link opens a new tab next to it. Each time you click, it continues to open. Just like filing cabinets.

    This presentation style is not only easy to follow but also aligns with the flow of thought. It's a pity that I can't find the author of this open-source project. I wonder if there's anything similar out there 🤔

    » Read more
  • Instagram has just introduced a video editing app for content creators called Edits. This is a direct competition with popular apps on the market. However, it might take some time because looking at the features, they are still quite basic, just enough to use.

    By the way, I've been "playing" on IG for over 10 years now. Anyone with IG, please leave your account for me to check out 🥳.

    My IG is hoaitx_

    » Read more

The Problem

Let me tell you a story. In the past, my blog used MySQL with a Node.js API. To maintain it, we had to use a 2GB RAM server. The monthly maintenance cost was $10 - $12. It might not be a huge amount, but it would accumulate if we had to continuously maintain it. Not to mention the other bills we had to pay every month.

This became a big issue that forced me to evaluate whether there was a way to run the blog on the lowest server configuration, reducing the cost by half. I had heard of Redis before, but using Redis to replace SQL was inconvenient due to some limitations in storage and query compared to SQL, which was already very good and familiar.

During my research, I stumbled upon Redisearch, which turned out to be a suitable tool for this situation. In addition to its storage capacity and query speed, Redisearch also provided advanced search capabilities. Search was a feature I prioritized because I wanted to provide a good search experience for users, allowing them to quickly find what they need without wasting too much time.

After spending some time with Redisearch, I have summarized some experiences for those who are considering "using Redisearch as SQL" or in other words, mapping SQL queries to Redisearch.

Relationship Mapping

A Table is an essential component in SQL, where Columns define the data types for storage. In Redis, there is a data type similar to a Table called HASH. When data is stored in HASH, with the magic touch of Redisearch, we can have the ability to query data equivalent to SQL.

A HASH structure consists of a hash_name, containing key-value pairs.

For example, data stored in HASH would look like this:

> HSET articles:1 id 1 title "What is Node.js" url "what-is-nodejs" createdAt 1671032684

You can learn more about the HASH data type at Redis hashes.

Since HASHes in Redis are simply discrete data and not contained in any set like a table in SQL, Redisearch creates Index Schemas to gather HASHes into a structure similar to a table.

Common Queries

In this example, I created a table named articles consisting of id, title, url, and createdAt, corresponding to the information of the article ID, title, URL, and creation time.

Data Types

In SQL, columns can have various data types such as char, varchar, int, float... depending on the storage purpose of the column. In Redisearch, a "column" only includes the following types: Text, Numeric, Tag, Geo.

Creating a Table

SQL:

CREATE TABLE articles (
    id int PRIMARY KEY,  
    title varchar,  
    url varchar,  
    createdAt date
);

Redisearch:

> FT.CREATE articles ON HASH PREFIX 1 articles: SCHEMA id NUMERIC title TEXT url TEXT createdAt NUMERIC

The HASHes created follow the articles: rule and are automatically added to the articles Index Schema.

Altering a Table

SQL:

ALTER TABLE articles
ADD deletedAt date;

Redisearch:

> FT.ALTER articles SCHEMA ADD deletedAt NUMERIC

Note: In SQL, the ALTER command can be used to add/modify/delete columns, but in Redisearch, you can only add a Schema. If you want to modify or delete, you have to DROP the index schema and recreate it.

INSERT

SQL:

INSERT INTO article (id, title, url, createdAt)
VALUES (1, 'What is Node.js', 'what-is-nodejs', 1671032684);

Redisearch:

> HSET articles:1 id 1 title "What is Node.js" url "what-is-nodejs" createdAt 1671032684

SELECT

SQL:

SELECT * FROM articles LIMIT 10 OFFSET 0;

Redisearch:

> FT.SEARCH articles * LIMIT 0 10

For more details on how basic SQL query syntax is mapped to Redisearch, you can refer to Mapping common SQL predicates to RediSearch.

UPDATE

SQL:

UPDATE articles SET title = 'What is Node.js?' WHERE id = 1;

Redisearch:

> HSET articles:1 title "What is Node.js?"

GROUP BY

Suppose we have another table named react with three columns client_id, react, and url to store user reviews by url of an article. Now, you want to retrieve the total number of each react by URL:

SQL:

SELECT react, COUNT(*) AS count FROM react WHERE url = 'what-is-nodejs' GROUP BY react;

Redisearch:

> FT.AGGREGATE articles "@url:what-is-nodejs" GROUPBY 1 @react REDUCE count 0 AS count

The syntax is quite different from SQL, but Redisearch does support the AGGREGATE query. You can learn more about the syntax at FT.AGGREGATE.

Conclusion

In this article, I have summarized some common SQL commands that can be mapped to Redisearch. Through this article, I would like to provide readers with a different perspective on whether Redisearch can solve problems similar to SQL. If you are interested, you can read more in my article What is Redisearch? Estacks is using Redisearch as a database!.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...