MySQL Varchar 上的索引是否会对性能产生影响?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1898453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:46:36  来源:igfitidea点击:

Does index on Varchar make performance difference?

mysqlsqlperformanceindexingvarchar

提问by Murvinlai

Does index on a varchar column make the query run slower? I can make that be int. and I don't need to do the LIKE % comparison.

varchar 列上的索引是否会使查询运行速度变慢?我可以把它变成int。我不需要做 LIKE % 比较。

回答by OMG Ponies

Does index on a varchar column make the query run slower?

varchar 列上的索引是否会使查询运行速度变慢?

No, it does not.
If the optimizer decides to use of the index, the query will run faster. INSERTs/UPDATEs/DELETEs on that table will be slower, but not likely enough to notice.

不,不是的。
如果优化器决定使用索引,查询将运行得更快。 该表上的INSERTs/ UPDATEs/ DELETEs 会更慢,但不太可能引起注意。

I don't need to do the LIKE % comparison

我不需要做 LIKE % 比较

Be aware that using:

请注意,使用:

LIKE '%whatever%'

...will notuse an index, but the following will:

...不会使用索引,但以下将:

LIKE 'whatever%'

The key is wildcarding the lefthand side of the string means that an index on the column can't be used.

关键是字符串左侧的通配符意味着不能使用列上的索引。

Also know that MySQL limits the amount of space set aside for indexes- they can be up to 1000 bytes long for MyISAM (767 bytes for InnoDB) tables.

还要知道MySQL 限制了为索引预留的空间量- MyISAM 表的长度可达 1000 字节(InnoDB 为 767 字节)。

回答by Leigh S

If you can replace your varchar column with an integer (which i think is what your hinting at) Then an index including the integer column will perform better than the varchar column for a few reasons.

如果您可以用整数替换 varchar 列(我认为这就是您的暗示),那么包含整数列的索引的性能将优于 varchar 列,原因有几个。

  1. The index size will be smaller and involve less paging.
  2. The comparison of integers is far better than varchar.
  1. 索引大小将更小并且涉及更少的分页。
  2. 整数的比较远比varchar好。

回答by YOU

Indexing don't make query slower, database size just get bigger, So go ahead and give it a try.

索引不会使查询变慢,数据库大小只会变大,所以请继续尝试。

You should able to get better performance on fetching rows but its depends on your data, your queries.

您应该能够在获取行时获得更好的性能,但这取决于您的数据和查询。

回答by Brad

You are not going to be making your queries any slower by indexing. If you can make the column a type int, I would recommend doing so as most RDBMS's can search int's faster than varchars

您不会通过索引使查询变慢。如果您可以将列设为 int 类型,我建议您这样做,因为大多数 RDBMS 可以比 varchars 更快地搜索 int

回答by Erhard Dinhobl

There are performance issues when it comes to inserts. I can for sure tell you that inserting into a large table which has an index over a varchar column can be very slow. (up to 50x)

插入时存在性能问题。我可以肯定地告诉你,插入一个在 varchar 列上有索引的大表可能会非常慢。(高达 50 倍)