mysql and indexes with more than one column

大憨熊 提交于 2019-12-24 00:59:13

问题


How to use indexes with more than one column

The original index has an index on block_id, but is it necesarry when it's already in the unique index with two column?

Indexes with more than one column

(a,b,c)

  • you can search for a, b and c
  • you can search for a and b
  • you can search for a
  • you can not search for a and c

Does this apply to unique indexes too?

table

id
block_id
account_id
name

indexes origin

PRIMARY KEY (`id`)
UNIQUE KEY `block_id` (`block_id`,`account_id`)
KEY `block_id` (`block_id`),
KEY `account_id` (`account_id`),

indexes alternative

PRIMARY KEY (`id`)
UNIQUE KEY `block_id` (`block_id`,`account_id`)
KEY `account_id` (`account_id`),

回答1:


The rules you describe above have to my knowledge always held whether an index is unique or not. You might run explain on the query you have in mind and observe when the index is used and when it is not used under various circumstances.



来源:https://stackoverflow.com/questions/13321332/mysql-and-indexes-with-more-than-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!