简体   繁体   English

TEXT 列上的 LOCATE 函数

[英]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.是否可以在 TEXT 列上使用 Locate() 函数,或者对于 TEXT 字段是否有任何替代方法。

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.问题是我们有用于跟踪订阅的大型 varchars (65kb),因此我们在 varchar 中的 1 个长字符串中添加了 subscription_ids。

this string can hold up to 5000 subscription_ids in 1 row.此字符串最多可在 1 行中容纳 5000 个 subscription_id。 we use LOCATE to see if a user is subscribed.我们使用 LOCATE 来查看用户是否订阅。 if a subscription_id is found inside the varchar string.如果在 varchar 字符串中找到了 subscription_id。

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.问题是我们计划像这样有超过 500,000 行,这似乎会对性能产生很大影响。

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.所以我们决定改为使用 TEXT,但现在索引和如何在 TEXT 列中定位子文本存在问题。

Billions of subscriptions?数十亿订阅? Please show an abbreviated example of a TEXT value.请显示一个TEXT值的缩写示例。 Have you tried FIND_IN_SET() ?你试过FIND_IN_SET()吗?

Is one TEXT field showing up to 5000 subscriptions for one user?一个TEXT字段是否为一个用户显示最多 5000 个订阅? Or is it the other way -- up to 5K users for one magazine?或者是另一种方式——一本杂志最多有 5K 用户?

In any case, it would be better to have a table with 2 columns:无论如何,最好有一个包含 2 列的表:

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.这两个复合索引让您可以非常高效地查找用户的 5K 订阅或订阅的 500K 用户。

Shrink the less-500K id to MEDIUMINT UNSIGNED (16M limit instead of 4 billion; 3 bytes each instead of 4).将小于 500K 的 id 缩小到MEDIUMINT UNSIGNED (16M 限制而不是 40 亿;每个 3 个字节而不是 4 个)。

Shrink the less-5K id to SMALLINT UNSIGNED (64K limit instead of 4B; 2 bytes each instead of 4).将小于 5K 的 id 缩小为SMALLINT UNSIGNED (64K 限制而不是 4B;每个 2 个字节而不是 4 个)。

If you desire, you can use GROUP_CONCAT() to reconstruct the commalist.如果您愿意,可以使用GROUP_CONCAT()来重建逗号列表。 Be sure to change group_concat_max_len to a suitably large number (default is only 1024 bytes .)确保将group_concat_max_len更改为适当的大数(默认仅为 1024字节。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM