Ruby on Rails的,ActiveRecord的,二进制搜索 [英] Ruby on Rails, ActiveRecord, Binary search

查看:564
本文介绍了Ruby on Rails的,ActiveRecord的,二进制搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我下面的表格了。

create_table :my_table, :id => false do |t|
   t.string :key_column
   t.string :value_column
end

我将如何确保行是optimaly的保存作为由领域二进制搜索:键

How would I ensure that the rows are optimaly stored for binary search by the field of :key?

和我将如何确保二进制搜索使用?

And how would I make sure binary search is used?

推荐答案

对于任何有趣的行数,最佳的方式(为最佳大多数定义),通过钥匙来访问单个随机记录是创建一个索引。

For any interesting number of rows, the optimal way (for most definitions of "optimal") to access a single random record by key is to create an index.

CREATE INDEX my_index ON my_table ( key_column );

或一个ActiveRecord迁移:

or in an ActiveRecord migration:

add_index(:my_table, :key_column)

数据库索引通常使用二进制搜索,使用 B树或类似的,它提供了存储成本和时间检索和更新之间的良好平衡。

Database indices typically use binary search, using B-trees or similar, which offers a good balance between storage costs and time for retrieval and update.

确保使用索引应该是相对简单的单表操作:

Ensuring the index is used should be relatively straightforward for single-table operations:

MyTable.find_by_key_column('ABC123')

例如,应该产生这样的事情(查看development.log):

for example, should generate something like this (check development.log):

SELECT * FROM my_table WHERE (key_column = 'ABC123')

甚至MySQL的比较如何依法履行自己pressive优化器应具有最佳状态运行没有问题。

which even MySQL's relatively unimpressive optimiser should have no problem running optimally.

行存储不应该是一个关注个人行检索,这是幸运的,因为没有什么可以做,反正来控制它。对于MySQL的性能,你或许应该选择的MyISAM比InnoDB的作为存储引擎,只要你的最佳的定义并不包括最可靠。

Row storage should not be a concern for individual row retrieval, which is fortunate as there isn't much you can do to control it anyway. For MySQL performance you should probably choose MyISAM over InnoDB as the storage engine, provided your definition of "optimal" doesn't include "most reliable".

这篇关于Ruby on Rails的,ActiveRecord的,二进制搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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