1

我有一个带有 2 个外键的表。我对 MySQL 有点陌生,有人能告诉我将 INDEX 应用于表的正确方法是什么吗?

# Sample 1
CREATE  TABLE IF NOT EXISTS `my_table` (
  `topic_id` INT UNSIGNED NOT NULL ,
  `course_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`topic_id`, `course_id`) ,
  INDEX `topic_id_idx` (`topic_id` ASC) ,
  INDEX `course_id_idx` (`course_id` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

# Sample 2
CREATE  TABLE IF NOT EXISTS `my_table` (
  `topic_id` INT UNSIGNED NOT NULL ,
  `course_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`topic_id`, `course_id`) ,
  INDEX `topic_id_idx` (`topic_id`, `course_id`) ,
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

我想我真正要问的是,将两者定义为单独的索引和将另一个定义为组合有什么区别?

4

2 回答 2

1

您可能想要其中一个而不是另一个的原因与您计划如何查询数据有关。做出正确的决定可能有点技巧。

例如,在文件柜中查找学生的文件夹时考虑组合键,首先按学生的姓氏,然后按他们的名字。

Now, in the case of the two single indexes in your example, you could imagine, in the student example, having two different sets of organized folders, one with every first name in order, and another with ever last name in order. In this case, you'll always have to work through the greatest amount of similar records, but that doesn't matter so much if you only have one name or the other anyway. In such a case, this arrangement gives you the greatest flexibility while still only maintaining indexes over two columns.

In contrast, if given both first and last name, it's a lot easier for us as humans to look up a student first by last name, then by first name (within a smaller set of potentials). However, when the last name is not known, it makes it very difficult to find the student by first name alone because the students with the same first-name are potential interleaved with every veration of last name (table scan). This is all true for the algorithms the computer uses to look up the information too.

So, as a rule of thumb, add the extra key to a single index if you are going to be filtering the data by both values at once. If at times you will have one and not the other, make sure which ever value that is, it's the leftmost key in the index. If the value could be either, you'll probably want both indexes (one of these could actually have both key for the best of both words, but even that comes at a cost in terms of writes). Getting this stuff right can be pretty important, as this often amounts to an all or nothing game. If all the data the dbms requires to preform the indexed lookup isn't present, it will probably resort to a table scan. Mysql's explain feature is one tool which can be helpful in checking your configuration and identifying optimizations.

于 2012-08-21T05:50:20.593 回答
0

如果您使用一个键创建索引,那么当搜索数据时,它将仅通过该键找到。

 INDEX `topic_id_idx` (`topic_id` ASC) ,

 INDEX `course_id_idx` (`course_id` ASC) 

在这种情况下,数据将分别搜索 topic_id 和 course_id。但是如果你将它们结合起来,就会搜索结合它们的数据。

例如,如果您有如下一些数据:

topic_id       course_id

----------

abc               1
pqr               2
abc               3

如果你想搜索 abc - 3 如果你放置单独的索引,那么它将分别搜索这两列并找到结果。但如果将它们组合起来,它将直接搜索 abc+3。

于 2012-08-21T04:57:20.960 回答