2017-01-26 23 views
0

有什么办法,using the console来确定数据库查找是否使用索引?确定ActiveRecord在查找过程中是否使用了索引?

例如:User.where(name: 'Bob', state: 'Delaware').first

有没有一种方法,以确定是否被用于快速发现这是一个索引?

我已经添加了索引,但想要验证它们正在用于某些查找。

感谢

+3

你可以做'User.where(名称: '鲍勃',状态: '特拉华')。explain'得到分贝解释查询 –

回答

3

你可以用这个检查:

User.where(name: 'Bob', state: 'Delaware').explain 

请记住,一个指标并不能使查询更快所有的时间,在某些情况下,指数可能进行查询运行速度较慢,有些数据库试图识别这一点,甚至可能也不使用这个索引。

检查索引的基数:

https://www.lullabot.com/articles/slow-queries-check-the-cardinality-of-your-mysql-indexes

保重!

http://edgeguides.rubyonrails.org/active_record_querying.html#running-explain

相关问题