varchar 与文本 - MySQL
在我的项目中,用户可以编写评论[纯文本],并查看其他人的评论,可以删除自己的评论,但不能更新评论!
在这种情况下我应该使用哪个?
文本还是 Varchar(4048) ?
Text 和 Varchar(大如 4000)有何优缺点?
如果我只替换 '<' 是否足够安全与 '& LT;'和“>”与 '& >'确保一切正常?
[我不想转换所有像 ' " & ...,为了节省空间,我只是想确保用户不能编写 javascript]
前端会有限制
in my project an user can write comment [plain text], and view others comment, can delete own comment, but can not update comment !
In this case which would should i use ?
Text or Varchar(4048) ?
What is the advantage and disadvantage of Text and Varchar(large like 4000) ?
Is it secure enough if i replace only '<' with '& lt;' and '>' with '& gt;' to make sure everything is fine ?
[i dont want to convert all those like ' " & ..., to save space, i just want to make sure user can not write javascript]
There will be a limit on the front end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当大小合理时,Varchar 的检索速度通常更快,因为它存储在表中,而 TEXT 则通过指向位置的指针存储在表外。
谢谢
Varchar is usually faster in retrieval when the size is reasonable, as it is stored within the table, where as TEXT is stored off the table with a pointer to location.
Thanks
(您有多个问题;我将解决标题中的问题。)
VARCHAR(4000)
和TEXT
之间的唯一区别是INSERT
将分别截断为 4000 个字符或 65536 个字节。对于小于 4000 的值,在某些情况下,复杂
SELECT
中的临时表在使用VARCHAR(255)
时会比使用TINYTEXT
运行得更快代码>.出于这个原因,我认为永远不应该使用TINYTEXT
。(You have multiple questions; I will address the one that is in the title.)
The only difference between
VARCHAR(4000)
andTEXT
is that anINSERT
will truncate to either 4000 characters or 65536 bytes, respectively.For smaller values than 4000, there are cases where the temp table in a complex
SELECT
will run faster with, for example,VARCHAR(255)
thanTINYTEXT
. For that reason, I feel that one should never useTINYTEXT
.为了保护自己免受 XSS 攻击,请使用 htmlentities 函数对其进行编码。
除此之外,数据类型的选择与内容的大小密切相关。如果可能超过 4048 个字符,则使用文本数据类型。如果许多帖子很大,使用文本数据类型可能会减少浪费的数据空间,并且性能可能比巨型 varchar 稍好,但这取决于您的情况,您最好测试替代方案。
我通常更喜欢 varchar,因为从编码的角度来看它更容易处理(如果没有其他情况的话),并且如果内容可能超过 varchar 的大小,则回退到文本。
To protect yourself from XSS attack, encode it using the htmlentities function.
Other than that, the choice of datatype has most to do with how big the content will be. If it may exceed 4048 characters, then use a text datatype. If many posts will be large, using a text datatype may reduce wasted data space and may perform slightly better than a giant varchar, but it depends upon your situation, you would be best to test the alternatives.
I generally prefer varchar because it's easier to deal with from a coding perspective, if nothing else, and fall back to text if the contents may exceed the size of a varchar.
这取决于应用程序的行为。块表内分配的空间会减少其他列的空间并减少其中的数据密度。如果mysql使用全表扫描,会扫描很多块,效率低下。
所以这取决于你的sql请求。
it depends on the application behavior. space allocated inside block's table decrease space for other columns and decrease density data inside it. if full table scan is used by mysql, many blocks are scanned, it's inefficient.
so it depends on your sql requests.