5

好的,所以我已经(重新)搜索了很多关于 MySQL 索引及其重要性的信息,我知道我必须使用它来使数据库查询运行得更快。,

而且我知道在任何文件上添加索引的语法。

但这是我不明白的,(我正在使用 Heidi SQL 在本地管理数据库)

我有一个包含以下字段的表

id
company_id
author_id
client_id
project_id
title
description
status
date

在这里,idprimary keyauto incremented并且已经是indexed

我想在company_id, author_id, client_id,上添加索引project_id,但我这里有几个不同的选项(Heidi SQL:选择字段,右键单击,创建新索引)Key, Unique, Full Text,Spatial

我知道(猜测)Key只会索引该字段,Unique将确保该字段必须是唯一的,并且Full Text如果我打算在该字段上执行搜索,索引将是最好的。

Question 1:我Spatial应该在何时何地使用此索引。

Question 2:在创建和索引时,我有 2 个选项,创建新索引或添加到现有索引(如主字段的名称)。

创建新索引或添加到现有索引有什么区别?为我索引的每个不同字段创建不同名称的索引是一个好主意,还是应该创建/添加同名的所有索引。

谢谢你的时间。

4

1 回答 1

4

You should start with a simple Key index. If you add Unique that can have unintended consequences, for example enforcing uniqueness on a column like company_id would definitely not work (lots of rows will share the same company_id).

A Spatial index is only for geo-coding data (latitude/longitude values), so this does not apply to any of these. Fulltext index is used when you want to search by words within the fields, it also will not apply to numeric values that you have. Further, a Fulltext index is only available in MyISAM, not the INNODB transactional engine, so if you ever want to migrate the table this would be a barrier to that.

If you add to an existing index, you are making a "compound" index on more than one column. Typically this is not a good idea, unless you are trying to enforce uniqueness in an index on a column that is not already unique. For example, you could index: company_id + id, and make it unique in that case. However, the index takes more space and will be slower on writes.

In summary, you should just use a normal KEY index on the columns you want to search by. MySQL will only use one of the indexes for each query, to see how it is actually accessing the data you can use the EXPLAIN utility. You want to make sure it is using an index in an effective way to narrow down the number of rows it needs to return or search for best performance.

于 2014-05-20T22:15:13.310 回答