简体   繁体   English

Ruby on Rails,ActiveRecord,二进制搜索

[英]Ruby on Rails, ActiveRecord, Binary search

If I had the following table. 如果我有下表。

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

How would I ensure that the rows are optimaly stored for binary search by the field of :key? 我如何确保通过: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 );

or in an ActiveRecord migration: 或在ActiveRecord迁移中:

add_index(:my_table, :key_column)

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. 数据库索引通常使用B树或类似方法进行二进制搜索,这在存储成本与检索和更新时间之间取得了良好的平衡。

Ensuring the index is used should be relatively straightforward for single-table operations: 对于单表操作,确保使用索引应该相对简单:

MyTable.find_by_key_column('ABC123')

for example, should generate something like this (check development.log): 例如,应生成如下内容(请检查development.log):

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

which even MySQL's relatively unimpressive optimiser should have no problem running optimally. 哪怕是MySQL相对不那么令人印象深刻的优化器,它也不会以最佳方式运行。

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". 为了获得MySQL性能,您应该选择MyISAM而不是InnoDB作为存储引擎,前提是您对“最佳”的定义不包括“最可靠”。

It's the job of the database to accurately store and retrieve the data. 准确地存储和检索数据是数据库的工作。 You describe what you want, it delivers it. 您描述您想要的东西,它交付了它。 If you want to control specifically how it goes about doing so then a database is not the answer. 如果你想控制它具体怎么去了解这样做,然后数据库就不是答案。

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

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