[英]mysql unique index with compound index
I have one table in mysql as follows : 我在mysql中有一张表,如下所示:
CREATE TABLE `demo_tbl` (
`id` MEDIUMINT(8) NOT NULL,
`uniquefld` VARCHAR(20) NULL,
`field1` VARCHAR(20) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ux_uniquefld` (`uniquefld` ASC),
INDEX `ix_uniquefld_field1` (`uniquefld` ASC, `field1` ASC)
);
Here I have created 2 indexes, one unique index for uniquefld
field and another is for 2 fields uniquefld
& field1
. 在这里,我创建了2个索引,一个索引用于
uniquefld
字段,另一个索引用于2个字段uniquefld
和field1
。
Is there any way to create single index instead of two with uniqueness for uniquefld
? 有什么方法可以创建唯一索引,而不是为
uniquefld
创建两个唯一uniquefld
?
why do you need to index ix_uniquefld_field1? 为什么需要索引ix_uniquefld_field1? since uniquefld is unique, any query on uniquefld+field1 can do just fine by accessing the index on ux_uniquefld.
由于uniquefld是唯一的,因此对uniquefld + field1的任何查询都可以通过访问ux_uniquefld上的索引来完成。
CREATE TABLE demo_tbl (
id MEDIUMINT NOT NULL PRIMARY KEY,
uniquefld VARCHAR(20) CONSTRAINT uid_uniquefld UNIQUE,
field1 VARCHAR(20),
);
CREATE INDEX idx_field1 ON demo_tbl (field1);
This will Work much effectively. 这将非常有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.