我应该使用什么列数据类型来存储大量文本或 html

发布于 2024-10-27 04:33:34 字数 126 浏览 1 评论 0原文

我在表中有一个列,最初是 varchar(255),由于一些设计更改,现在它是 varchar(1536) = 1024 + 512。我不会搜索或索引该字段,这有意义吗如果您想优化性能,将此值存储在 varchar 以外的不同数据类型中?

I have a column in a table which used to be varchar(255) in the beginning and due to some design changes now it is varchar(1536) = 1024 + 512. I will not be searching or indexing this field, does it make sense to store this value in a different data type other than a varchar if you would like to optimize this for performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云胡 2024-11-03 04:33:34

您应该像其他人所说的那样使用 TEXT ,但是每次使用 TEXT 或 BLOB 时都有一些重要的建议:将它们与基表解耦,因为它们确实会减慢访问数据的速度。桌子。想象一下以下结构:

CREATE TABLE article (
    id INT(10) UNSIGNED,
    title VARCHAR(40),
    author_id INT(10) UNSIGNED,
    created DATETIME,
    modified DATETIME
);

CREATE TABLE article_body (
    id INT(10) UNSIGNED,
    body TEXT
);

每当您列出文章时,您都可以使用 article 表(作者 33 的最后 5 篇文章):

SELECT id, title FROM article WHERE author_id=33 ORDER BY created DESC LIMIT 5

当有人真正打开该文章时,您可以使用类似以下内容的内容:

SELECT a.title, ab.body
FROM article AS a
   LEFT JOIN article_body AS ab ON ab.id = a.id
WHERE a.id=82

You should use TEXT like the others said, but there is some important advice every time you use TEXT or BLOB: decouple them form your base table as they really slow down accessing the table. Imagine the following structure:

CREATE TABLE article (
    id INT(10) UNSIGNED,
    title VARCHAR(40),
    author_id INT(10) UNSIGNED,
    created DATETIME,
    modified DATETIME
);

CREATE TABLE article_body (
    id INT(10) UNSIGNED,
    body TEXT
);

Whenever you list articles you can use the article table (last 5 articles of author 33):

SELECT id, title FROM article WHERE author_id=33 ORDER BY created DESC LIMIT 5

And when someone really opens the article you can use something like:

SELECT a.title, ab.body
FROM article AS a
   LEFT JOIN article_body AS ab ON ab.id = a.id
WHERE a.id=82
肥爪爪 2024-11-03 04:33:34

是的,如果您可以将值存储在“TEXT”数据类型中,那就更好了。有关更多详细信息,请阅读本文

关于存储要求的知识,可以阅读这篇

希望有帮助。

Yes, it will be better if you can store the values in the "TEXT" data type. For more details, please read this article.

Regarding knowledge of storage requirements, you can read this one.

Hope it helps.

当梦初醒 2024-11-03 04:33:34

您应该使用文件而不是数据库来存储它。特别是MySQL。我曾经写过一篇文章,解释了如果您从数据库 BLOB 下载图像,会发生什么情况,请参阅 http://mysqldump.azundris.com/archives/36-Serving-Images-From-A-Database.html。使用文件,您可以使用 sendfile(2) 系统调用来使用 Web 服务器快速路径,并且使用此速度要快得多。

MySQL 也没有 BLOB API。这意味着,不可能上传或下载大于 max_allowed_pa​​cket 的对象,并且很难使用 SUBSTRING() 来解决这个问题,因为这会在服务器内存中生成不必要的字符串副本。

如果您绝对必须在服务器中存储 BLOB 或 TEXT 数据,则可以选择 TINYTEXT、TEXT、MEDIUMTEXT 和 LARGETEXT,它们在服务器中的数据限制为 255、65535、16 MB 和 4GB,另外还受到 max_allowed_pa​​cket 的限制。

大型 BLOB 或 TEXT 信息将完全破坏表中的数据密度。创建与 BLOB 表的人为 1:1 或 1:0 关系,然后将 blob 存储在这个额外的表中非常有用。

当MySQL显示“使用临时”的查询计划时,这意味着服务器需要在交付结果之前在服务器中具体化结果集表。如果可能的话,这是使用 MEMORY 表来完成的。任何 TEXT 或 BLOB 类型都无法在 MEMORY 表中表示,因此临时表将作为 MyISAM 表写入磁盘。

您需要扫描此类查询计划,并将它们转换为加载 BLOB/TEXT 值的 ID 值的内容。在第二个查询中,您可以 SELECT id, thetext FROM texttable WHERE id in ( ... ) 来获取 TEXT/BLOB 值。这将使带有“using tempor”的查询不使用 TEXT 或 BLOB 类型,然后您可以通过在不使用“usingtemporary”的情况下运行的简单查询来获取 TEXT 字段。

您可以通过阅读 http://www.mysqlperformanceblog.com/2010/02/09/blob-storage-in-innodb/

You should be using a file, not a database to store this. Especially not MySQL. I made a writeup once explaining what happens if you for example download images out of a database BLOB, see http://mysqldump.azundris.com/archives/36-Serving-Images-From-A-Database.html. Using files, you can use the web server fast path using the sendfile(2) system call, and it is much faster to use this.

MySQL also has no BLOB API. That means, it is impossible to upload or download objects larger than max_allowed_packet, and it is hard to work your way around that using SUBSTRING(), because that will make needless copies of strings in server memory.

If you absolutely MUST store BLOB or TEXT data in the server, you have the choice of TINYTEXT, TEXT, MEDIUMTEXT and LARGETEXT which are limited to 255, 65535, 16 MB and 4GB of data in the server, additionally constrained by max_allowed_packet.

Large BLOB or TEXT information will completely wreck data density in your table. It is useful to create an artificial 1:1 or 1:0 relationship to a BLOB table, and then store the blobs in this extra table.

When MySQL shows a query plan that is 'using tempoary', it means that the server needs to materialize the result set table in the server before delivering the result. This is being done using MEMORY tables, if possible. Any TEXT or BLOB type cannot be represented in MEMORY tables, hence the temporary table then hits the disk as a MyISAM table instead.

You need to scan for such query plans, and convert them into something that loads the ID values of the BLOB/TEXT values instead. In a second query, you'd then SELECT id, thetext FROM texttable WHERE id in ( ... ) to get the TEXT/BLOB values. That will make the query with 'using temporary' not use TEXT or BLOB types, and you can get the TEXT fields then with a trivial query that runs without 'using temporary'.

You can learn more about the internals of MySQL TEXT and BLOB storage by reading http://www.mysqlperformanceblog.com/2010/02/09/blob-storage-in-innodb/

山人契 2024-11-03 04:33:34

我在发展陷入困境后来到这里。
在我的网站中:
我在 MYSQL 中使用 TEXT 数据类型来存储用户输入给出的 textarea 的内容。今天我在textarea中给出了一个大文本(112KB)来存储在DB中。
它就坏了。
然后我从网上读到——

TINYTEXT:255 个字符 - 255 B
文本:65,535 个字符 - 64 KB
MEDIUMTEXT:16,777,215 - 16 MB
长文本:4,294,967,295 个字符 - 4 GB

对我来说,我必须切换到MEDIUMTEXT

I am here after get stuck in my development.
In my website:
I used TEXT datatype in MYSQL to store the content of the textarea given by user input. Today I give a large text (112KB) in the textarea to store in DB.
and it breaks.
Then I read from internet -

TINYTEXT: 255 characters - 255 B
TEXT: 65,535 characters - 64 KB
MEDIUMTEXT: 16,777,215 - 16 MB
LONGTEXT: 4,294,967,295 characters - 4 GB

For me I have to switch to the MEDIUMTEXT.

眼眸里的那抹悲凉 2024-11-03 04:33:34

我会使用 text 来表示长度可变的列。

I would use text for columns with variable length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文