我定义索引(A)和索引(B),或索引(A,B),或两者? [英] Shall I define index (A) and index (B), or index (A, B), or both?

查看:1070
本文介绍了我定义索引(A)和索引(B),或索引(A,B),或两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表中,我有两个紧密相关的列A和B.我可以考虑哪些因素来决定是否创建:




  • 上述两个都是索引(A)和索引(B),

  • 索引(A,B),

  • $ b


如果我:


  1. 其中A = 5且B = 10 (且从不喜欢其中A = 5 ),

  2. 也使用的查询,其中A> 3和A < 10和B> 12和B < 20

  3. 经常使用订单(A,B)

  4. 经常使用 group by(A,B)



<注意:我故意没有提供更多关于我的具体情况的详细信息,因为我想要一个一般的答案,也将为他人服务。我使用mysql,但如果你给更多的一般答案,一般涵盖SQL,这将是巨大的。

解决方案

有索引(A,B),MySQL也可以使用它作为索引(A)。声明AB和A没有意义。然而,



  1. 索引(A)和索引(B)



In my table I have two closely related columns A and B. What possible considerations shall I made to decide whether to create:

  • index(A) and index(B),
  • index(A, B),
  • both of the above?

How does this change if I:

  1. use only queries like where A = 5 and B = 10 (and never like where A = 5),
  2. also use queries like where A > 3 and A < 10 and B > 12 and B < 20,
  3. often use order by (A, B),
  4. often use group by (A, B)?

Note: I intentionally haven't provided more details about my particular case as I want a general answer that will also serve to others. I use mysql, but if you give more general answer that covers SQL in general that would be great.

解决方案

Ok, when you have an index(A,B), MySQL can also use it as an index(A). Declaring AB and A has no sense. Declaring AB and B does, however

So you have these choices:

  1. Index(A,B)
  2. Index(A,B) and Index(B)
  3. Index(A) and Index(B)

这篇关于我定义索引(A)和索引(B),或索引(A,B),或两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆