Noobit
Noobit

Reputation: 11

LOCATE function on TEXT column

Is it possible to use the Locate() function on TEXT column, or is there any alternative to it for TEXT fields.

the thing is we have LARGE varchars (65kb) that we use to track for subscriptions, so we add subscription_ids inside 1 long string in varchar.

this string can hold up to 5000 subscription_ids in 1 row. we use LOCATE to see if a user is subscribed. if a subscription_id is found inside the varchar string.

the problem is that we plan to have more than 500,000 rows like this, it seems this can have a big impact on performance.

so we decided to move to TEXT instead, but now there is a problem with indexation and how to LOCATE sub-text inside a TEXT column.

Upvotes: 0

Views: 179

Answers (1)

Rick James
Rick James

Reputation: 142228

Billions of subscriptions? Please show an abbreviated example of a TEXT value. Have you tried FIND_IN_SET()?

Is one TEXT field showing up to 5000 subscriptions for one user? Or is it the other way -- up to 5K users for one magazine?

In any case, it would be better to have a table with 2 columns:

CREATE TABLE user_sub (
    user_id INT UNSIGNED NOT NULL,
    sub_id INT UNSIGNED NOT NULL,
    PRIMARY KEY(user_id, sub_id),
    INDEX(sub_id, user_id)
) ENGINE=InnoDB;

The two composite indexes let you very efficiently find the 5K subscriptions for a user or the 500K users for a sub.

Shrink the less-500K id to MEDIUMINT UNSIGNED (16M limit instead of 4 billion; 3 bytes each instead of 4).

Shrink the less-5K id to SMALLINT UNSIGNED (64K limit instead of 4B; 2 bytes each instead of 4).

If you desire, you can use GROUP_CONCAT() to reconstruct the commalist. Be sure to change group_concat_max_len to a suitably large number (default is only 1024 bytes.)

Upvotes: 1

Related Questions