简体   繁体   English

Mysql索引键有什么用?

[英]What is the use of Mysql Index key?

Hi I am a newbie to mysql 嗨,我是mysql的新手

Here are my questions: 这是我的问题:

  1. What is the use of Mysql Index key? Mysql索引键有什么用?

  2. Does it make a difference in mysql queries with defining an Index key and without it? 在没有定义索引键的情况下,它在mysql查询中是否有所不同?

  3. Are all primary keys default Index key? 所有主键都是默认的索引键吗?

Thanks a million 太感谢了

1- Defining an index on a column (or set of columns) makes searching on that column (or set) much faster, at the expense of additional disk space. 1-在一个列(或一组列)上定义索引可以使在该列(或一组集)上的搜索变得更快,但会增加磁盘空间。

2- Yes, the difference is that queries using that column will be much faster. 2-是的,不同之处在于使用该列的查询会快得多。

3- Yes, as it's usual to search by the primary key, it makes sense for that column to always be indexed. 3-是的,因为通常使用主键进行搜索,所以始终对该列进行索引是有意义的。

Read more on MySQL indexing here . 此处阅读有关MySQL索引的更多信息

An index is indeed an additional set of records. 索引确实是另一组记录。 Nothing more. 而已。

Things that make indexes access faster are: 使索引访问更快的是:

  • Internally there's more chance that the engine put in buffer the index than the whole table rows 在内部,引擎比整个表行更有可能缓冲索引
  • The index is smaller so to parse it means reading less blocks of the hard drive 索引较小,因此要进行解析意味着要读取更少的硬盘块
  • The index is sorted already, so finding a given value is easy 该索引已经排序,因此查找给定值很容易
  • In case of being not null, it's even faster (for various reasons, but the most important thing to know is that the engine doesn't store null values in indexes ) 在不为null的情况下,它甚至会更快(出于各种原因,但是最重要的要知道的是,引擎不会在索引中存储null值

To know whether or not an index is useful is not so easy to guess (obviously I'm not talking about the primary key) and should be investigated. 要知道索引是否有用并不是一件容易的事(显然我不是在谈论主键),应该进行调查。 Here are some counterparts when it might slow down your operations: 以下是一些可能会减慢您的操作速度的方法:

  • It will slow down inserts and updates on indexed fields 它将减慢索引字段的插入和更新
  • It requires more maintenance: statistics have to be built for each index so the computing could take a significantly longer time if you add many indexes 它需要更多维护:必须为每个索引建立统计信息,因此如果您添加许多索引,则计算可能会花费更长的时间
  • It might slow down the queries when the statistics are not up to date. 当统计信息不是最新时,它可能会使查询变慢。 This effect could be catastrophic because the engine would actually go "the wrong way" 这种影响可能是灾难性的,因为引擎实际上会“走错路”
  • It might slow down when the query are inadequate (anyway indexes should not be a rule but an exception: no index, except if there's an urge on certain queries. I know usually every table has at least one index, but it came after investigations) We could comment this last point a lot, but I think every case is special and many examples of this already exist in internet. 当查询不足时,它可能会变慢(无论如何索引都不是一个规则,而是一个例外:没有索引,除非有某些查询的冲动。我知道通常每个表至少有一个索引,但是它是在调查之后得出的)我们可以对最后一点进行很多评论,但是我认为每种情况都是特殊的,并且互联网上已经存在许多这样的例子。

Now about your question 'Are all primary keys default Index key?', I must say that it is not like this that the optimizer works. 现在关于您的问题“所有主键都是默认的索引键吗?”,我必须说优化器不是这样工作的。 When there are various indexes defined in a table, the more efficient index combination will be compiled with on the fly datas and some other static datas (indexes statistics), in order to reach the best performances. 当表中定义了各种索引时,将使用动态数据和一些其他静态数据(索引统计信息)来编译更有效的索引组合,以达到最佳性能。 There's no default index per se, every situation leeds to a different result. 本身没有默认索引,每种情况都会导致不同的结果。

Regards. 问候。

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

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