简体   繁体   English

MySQL-索引问题

[英]MySQL - Indexing questions

I'm for the first time making a bigger database using MySQL. 我是第一次使用MySQL创建更大的数据库。 Since it's going to be a bigger one I need better performance for the queries. 由于它将变得更大,因此我需要更好的查询性能。 So I looked into indexes. 所以我研究了索引。 Since this is what I was learned I always make a column named id which is auto-incremented and is used as a primary index. 因为这是我的经验,所以我总是创建一个名为id的列,该列会自动递增并用作主索引。

After some searching and reading I think I've understood some things about indexing but I want to be sure. 经过一些搜索和阅读,我认为我已经了解了一些有关索引编制的知识,但是我想确定一下。

Indexes is useful when you use a select query with, for example a WHERE clause because instead of searching the whole table, it just searching in a smaller part. 当您将选择查询与WHERE子句一起使用时,索引很有用,因为它不是在搜索整个表,而是在较小的部分中进行搜索。 You should index things that are used in WHERE and JOIN clauses. 您应该索引WHERE和JOIN子句中使用的内容。

Is it good enough to just add a index in phpMyAdmin on the columns that you want to index or do I have to do something when doing a select query? 仅在要索引的列上的phpMyAdmin中添加索引就足够了,还是在执行选择查询时必须做一些事情?

You don't need to make any changes to your SELECT statements. 您无需对SELECT语句进行任何更改。 MySQL will automatically use the indexes for lookups when it can. MySQL会在可能时自动使用索引进行查找。

So yes, it's good enough to add the indexes via phpMyAdmin. 所以是的,通过phpMyAdmin添加索引就足够了。

To see this in action, once you've created your indexes, try running a SELECT query prefixed with EXPLAIN. 若要查看实际效果,创建索引后,请尝试运行以EXPLAIN为前缀的SELECT查询。 MySQL will tell you which indexes are being used. MySQL会告诉您正在使用哪些索引。

Just add the index. 只需添加索引。 The DB engine will use it if possible. 数据库引擎将尽可能使用它。 To see if the DB engine uses it you can add an explain before your query and it will output what it does. 要查看数据库引擎是否使用它,您可以在查询之前添加一个explain然后输出结果。 Example: 例:

explain select * from your_table

It is true that you can use the index in your where clause, but often you don't know what indexes to search for and instead search for more meaningful data such as names, email-addresses, dates etc. 确实可以在where子句中使用索引,但是通常您不知道要搜索哪些索引,而是搜索更有意义的数据,例如名称,电子邮件地址,日期等。

The power of using indexes comes when you join tables, both for performance of the search and for the size of the database. 联接表时,使用索引的强大功能在于提高搜索性能和数据库大小。 Imagine if you have a DB with employees and you want to keep track of what office they belong to. 想象一下,如果您有一个包含员工的数据库,并且想跟踪他们所属的办公室。 With only one table each employee will have a char string containing the office name, ie "Albuquerque Headquarters". 每个员工只有一张桌子,将有一个包含办公室名称的字符字符串,即“阿尔伯克基总部”。 Char strings occupy a lot of space and are slow to search for. 字符字符串占用大量空间,搜索速度很慢。

Instead, designed with two tables and indexes the employee-table only need to store an int (the index of the other table) for each employee. 取而代之的是,设计有两个表和索引的employee表仅需要为每个员工存储一个int(另一个表的索引)。 And the second table keeps track of all the offices. 第二个表跟踪所有办公室。 The overall database gets much smaller when the number of data records increase. 当数据记录数量增加时,整个数据库将变得更小。 This approach is also much much easier to maintain and is better in each and every way. 这种方法也更容易维护,并且在各个方面都更好。

Either way - you can never go wrong with an index and you might as well add one to each table you'll ever create (I am sure that someone might disagree with this statement, but normally I'd say you should just go for it). 无论哪种方式-索引都不会出错,也可以在要创建的每个表中添加一个索引(我确信有人可能会不同意此语句,但是通常我会说你应该去做它) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM