使用INDEX的原因和位置 - 优点和缺点 [英] Why and where to use INDEXes - pros and cons

查看:147
本文介绍了使用INDEX的原因和位置 - 优点和缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对数据库编程很陌生,我想知道索引的负面影响是什么?据我所知,索引加速了必须在数据库中搜索特定值的操作(例如SELECT)。

I'm quite new to database programming and I am wondering what the negative effects of indexes are? As far as I understood, indexes speed up operations which have to search the database for a specific value (for example a SELECT).

考虑这个例子:

对于表示例,列 user_name 的索引,操作:

For the table Example, with an index on column user_name, the operation:

SELECT TestField FROM Example WHERE user_name=XXXX

由于指数会更快。

我的问题是:使用索引的缺点是什么?如果一个索引只给我们专业人士(性能提升),他们为什么不设置为默认值?

My question is: what are cons of using indexes? If an index just give us pros (performance gaining), why aren't they set as default?

推荐答案

那么你可以填写关于索引的书籍,但简而言之,在创建索引时需要考虑一些事项:

Well you can probably fill books about indices but in short here a few things to think about, when creating an index:

虽然它(大多数)加速了选择,但它会降低插入速度,更新和删除,因为数据库引擎不必仅写入数据,而是索引。
RAM中的索引需要硬盘空间(更重要的是)。无法保存在RAM中的索引是没用的。
只有几个不同值的列上的索引不会加快选择速度,因为它不能排序很多行(例如列性别,通常只有两个不同的值 - 男性,女性)。

While it (mostly) speeds up a select, it slows down inserts, updates and deletes because the database engine does not have to write the data only, but the index, too. An index need space on hard disk (and much more important) in RAM. An index that can not be held in RAM is pretty useless. An index on a column with only a few different values doesn't speed up selects, because it can not sort out much rows (for example a column "gender", which usually has only two different values - male, female).

如果您使用MySQL,您可以检查,如果enginge通过在选择之前添加explain来使用索引 - 对于您的上述示例 EXPLAIN SELECT TestField FROM示例WHERE username = XXXX

If you use MySQL for example you can check, if the enginge uses an index by adding "explain" before the select - for your above example EXPLAIN SELECT TestField FROM Example WHERE username=XXXX

这篇关于使用INDEX的原因和位置 - 优点和缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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