SQL 中的索引是什么? [英] What is an index in SQL?

查看:22
本文介绍了SQL 中的索引是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另外,什么时候使用合适?

Also, when is it appropriate to use one?

推荐答案

索引用于加速数据库中的搜索.MySQL 有一些关于这个主题的很好的文档(这也适用于其他 SQL 服务器):http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

An index is used to speed up searching in the database. MySQL have some good documentation on the subject (which is relevant for other SQL servers as well): http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

索引可用于高效地查找与查询中某个列匹配的所有行,然后仅遍历表的该子集以查找完全匹配.如果 WHERE 子句中的任何列都没有索引,SQL 服务器必须遍历整个表并检查每一行看看是否匹配,这在大表上可能是一个缓慢的操作.

An index can be used to efficiently find all rows matching some column in your query and then walk through only that subset of the table to find exact matches. If you don't have indexes on any column in the WHERE clause, the SQL server has to walk through the whole table and check every row to see if it matches, which may be a slow operation on big tables.

索引也可以是 UNIQUE 索引,这意味着该列中不能有重复的值,或者是 PRIMARY KEY 在某些存储引擎中定义了在哪里存储值的数据库文件.

The index can also be a UNIQUE index, which means that you cannot have duplicate values in that column, or a PRIMARY KEY which in some storage engines defines where in the database file the value is stored.

在 MySQL 中,您可以在 SELECT 语句前使用 EXPLAIN 来查看您的查询是否会使用任何索引.这是解决性能问题的良好开端.在此处阅读更多信息:http://dev.mysql.com/doc/refman/5.0/en/explain.html

In MySQL you can use EXPLAIN in front of your SELECT statement to see if your query will make use of any index. This is a good start for troubleshooting performance problems. Read more here: http://dev.mysql.com/doc/refman/5.0/en/explain.html

这篇关于SQL 中的索引是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆