Speed up a LIKE keyword search on chat messages with full-text search and a composite index
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A chat app searches messages with `message_text LIKE '%keyword%'`, and the query is slow on a large table.
Rewrite the slow `LIKE '%keyword%'` search using PostgreSQL full-text search, with a composite index on (`workspace_id`, `channel_id`) and a full-text index on `message_text`. Return `message_id`, `workspace_id`, `channel_id`, `sender_id`, `message_text`, `created_at` for messages in workspace 1, channel 101 matching `'deadline'`. Say which indexes you would create, and write the query so PostgreSQL can use them.
Requirements:
- Match with full-text search using the `'simple'` text search configuration, which lowercases words and does no stemming.
- Order by `created_at` descending, then `message_id` descending.
- No matches returns an empty result.
```hint Index types
A B-tree index cannot help with a pattern that starts with `%`. Think about which index type maps each word to the rows that contain it, and what the planner needs from your query to pick that index.
```
### Input Table(s)
`messages` (primary key `message_id`; every column is NOT NULL)
| column | type | meaning |
|---|---|---|
| message_id | INT | Message identifier |
| workspace_id | INT | Workspace the message belongs to |
| channel_id | INT | Channel within the workspace |
| sender_id | INT | User who sent the message |
| message_text | TEXT | Message body |
| created_at | TIMESTAMP | When the message was sent |
### Output Contract
- Columns, in this order: `message_id`, `workspace_id`, `channel_id`, `sender_id`, `message_text`, `created_at`.
- One row per message in workspace 1, channel 101 whose text matches `deadline`.
- Rows are ordered by `created_at` descending, then `message_id` descending. Since `message_id` is unique, the order is fully determined.
- An empty result when no message matches.
### Example
`messages`
| message_id | workspace_id | channel_id | sender_id | message_text | created_at |
|---|---|---|---|---|---|
| 1 | 1 | 101 | 7 | Reminder: the deadline is Friday. | 2024-03-01 09:00:00 |
| 2 | 1 | 101 | 8 | Can we move the DEADLINE? | 2024-03-02 10:30:00 |
| 3 | 1 | 101 | 9 | Two deadlines this week | 2024-03-02 11:00:00 |
| 4 | 1 | 102 | 7 | deadline moved | 2024-03-03 08:00:00 |
| 5 | 2 | 101 | 7 | deadline tomorrow | 2024-03-03 09:00:00 |
| 6 | 1 | 101 | 10 | Lunch at noon? | 2024-03-03 12:00:00 |
| 7 | 1 | 101 | 11 | New deadline: March 10 | 2024-03-02 10:30:00 |
Expected result:
| message_id | workspace_id | channel_id | sender_id | message_text | created_at |
|---|---|---|---|---|---|
| 7 | 1 | 101 | 11 | New deadline: March 10 | 2024-03-02 10:30:00 |
| 2 | 1 | 101 | 8 | Can we move the DEADLINE? | 2024-03-02 10:30:00 |
| 1 | 1 | 101 | 7 | Reminder: the deadline is Friday. | 2024-03-01 09:00:00 |
Messages 1, 2 and 7 contain the word `deadline` in some letter case. Message 3 only has `deadlines`, which is a different word without stemming, and messages 4 and 5 are in another channel or workspace. Messages 7 and 2 share a timestamp, so the higher `message_id` comes first.
### Constraints and Clarifications
- `messages` has up to `10^6` rows.
- `message_text` is non-null, up to 65,535 characters.
- A message matches when `deadline` appears as a word of its own in any letter case; punctuation next to it does not matter. Longer words that contain it, such as `deadlines`, do not match.
- No column is NULL, so the query needs no NULL handling.
- Several messages can share a `created_at`; `message_id` breaks the tie.
Overview: Rewrite a slow LIKE keyword search over chat messages as a PostgreSQL full-text query backed by a composite index and a GIN index. It tests why leading wildcards defeat B-tree indexes, how the planner matches expression indexes, and how word matching changes the results.