ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Full-Text Indexing for Product Search

SQL MariaDB

Goal -- WPM

Ready
Exercise Algorithm Area
1SELECT
2product_id,
3product_name,
4product_description,
5MATCH(product_description) AGAINST('database performance tuning' IN NATURAL LANGUAGE MODE) AS relevance_score
6FROM
7products
8WHERE
9MATCH(product_description) AGAINST('database performance tuning' IN NATURAL LANGUAGE MODE)
10ORDER BY
11relevance_score DESC;
12
13-- Helper function to create a full-text index (conceptual, requires ALTER TABLE)
14-- This is not executable SQL but illustrates the setup.
15-- PROCEDURE create_fulltext_index(table_name VARCHAR(255), column_list VARCHAR(255))
16-- BEGIN
17-- -- Example: ALTER TABLE products ADD FULLTEXT(product_description);
18-- -- Ensure the table engine supports FULLTEXT (e.g., MyISAM, InnoDB).
19-- END;
20
21-- Edge case: No matching rows found.
22-- If no rows contain the search terms, the WHERE clause will filter them out,
23-- and the query will return an empty result set.
24-- The relevance_score will be 0 for rows that don't match.
25
26-- Edge case: Empty search string.
27-- If the search string is empty, MATCH AGAINST might behave unexpectedly or return all rows with a low score.
28-- It's generally best to validate search input to prevent empty searches.
29
30-- Edge case: Stop words and stemming.
31-- Full-text search engines often have stop words (common words ignored) and stemming (reducing words to their root).
32-- The behavior depends on the engine's configuration and language settings.
33
34-- The `MATCH(column) AGAINST('search terms' IN MODE)` syntax is used to perform full-text searches.
35-- `NATURAL LANGUAGE MODE` treats the search string as a natural language phrase.
36-- Other modes like BOOLEAN MODE offer more advanced search capabilities.
37
38-- This query assumes a FULLTEXT index has been created on the `product_description` column.
39-- Without the index, performance will be significantly degraded as it would require a full table scan.
Algorithm description viewbox

Full-Text Indexing for Product Search

Algorithm description:

This SQL query demonstrates how to perform a full-text search in MariaDB using the `MATCH AGAINST` operator. It searches for products whose descriptions contain specific keywords ('database performance tuning'). Full-text indexing allows for efficient searching of text data, going beyond simple keyword matching to consider relevance and word proximity. This is crucial for e-commerce sites, documentation platforms, and any application requiring robust text search capabilities.

Algorithm explanation:

The query uses `MATCH(product_description) AGAINST('database performance tuning' IN NATURAL LANGUAGE MODE)` in both the `SELECT` list (to get a relevance score) and the `WHERE` clause (to filter results). `MATCH` operates on columns with a `FULLTEXT` index. `AGAINST` specifies the search terms and the mode. `NATURAL LANGUAGE MODE` interprets the search string as a natural language query. The `ORDER BY relevance_score DESC` sorts the results by how well they match the search terms. Time complexity is typically O(log N) or better with a `FULLTEXT` index, where N is the number of rows. Space complexity is O(1). Edge cases include no matching rows and empty search strings.

Pseudocode:

SELECT product_id, product_name, product_description, relevance_score FROM products WHERE MATCH(product_description) AGAINST('search terms' IN NATURAL LANGUAGE MODE) ORDER BY relevance_score DESC;

Define relevance_score = MATCH(product_description) AGAINST('search terms' IN NATURAL LANGUAGE MODE).