拿VS Ruby的首场演出on Rails的 [英] take vs first performance in Ruby on Rails

查看:111
本文介绍了拿VS Ruby的首场演出on Rails的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于ActiveRecord的查询方法的问题:

This is a question regarding ActiveRecord query methods:

  • 第一查找第一个记录(或第N个记录,如果一个参数提供)。如果没有订单被定义,将通过主键顺序。
  • 给出了一个记录(或N个记录,如果提供参数),没有任何隐含的顺序。该命令将取决于数据库的实现。如果为了提供将被尊重。
  • first Find the first record (or first N records if a parameter is supplied). If no order is defined it will order by primary key.
  • take Gives a record (or N records if a parameter is supplied) without any implied order. The order will depend on the database implementation. If an order is supplied it will be respected.

用例: 基于独特的属性,从数据库中检索记录,例如。

usecase: retrieve record from database based on unique attribute, example.

User.where(email: 'f@example.com')

在这里, 第一生成

here, first generates

SELECT "users".* FROM "users" WHERE "users"."email" = 'f@example.com' ORDER BY "users"."id"` ASC LIMIT 1

生成

SELECT "users".* FROM "users" WHERE "users"."email" = 'f@example.com' LIMIT 1

所以上面看到第一个增加了额外的订购条款。我想知道是否有与 A的性能差异采取 VS 第一

so as seen above first adds additional ordering clause. I am wondering if there a performance difference between take vs first.

的速度比第一或反之亦然?

Is take faster than first or vice-versa?

推荐答案

在一般的取会更快,因为该数据库没有找出所有符合条件的行,然后进行排序,找到最低-sorting一行。 采取允许数据库尽快停止,因为它已经找到了一个一行。

In general "take" will be faster, because the database does not have to identify all of the rows that meet the criteria and then sort them and find the lowest-sorting row. "take" allows the database to stop as soon as it has found a single row.

的程度它更快会根据来改变:

The degree to which it is faster is going to vary according to:

  1. 多少时间保存在没有​​寻找多行。这里最糟糕的情况是需要一个大表进行完全扫描,但一个匹配行被发现在扫描得很早。 吃可以让扫描停止。

  1. How much time is saved in not having to look for more than one row. The worst case here is where a full scan of a large table is required, but one matching row is found very early in the scan. "take" would allow the scan to be stopped.

如何将需要许多行进行排序,找到具有最低ID。这里最坏的情况是,其中表中的每一行符合条件并需要包括在排序

How many rows would need to be sorted to find the one with the lowest id. The worst case here is where every row in the table matches the criteria and needs to be included in the sort.

有一些其他的因素要考虑 - 例如用于一个第一查询优化器可能能够经由主键索引的扫描访问表,并检查每一行,看它是否与该条件匹配。如果在数据和排序的那则两个完全扫描的可能性非常高,可避免如果查询优化器是足够复杂的

There are some other factors to consider -- for example for a "first" query the optimiser might be able to access the table via a scan of the primary key index and check each row to see if it matches the condition. If there is a very high likelihood of that then both a complete scan of the data and a sort can be avoided if the query optimiser is sophisticated enough.

在很多情况下,在那里很少有匹配的记录,并基于索引的访问,以找到他们,你会发现,不同的是微不足道的(那里是在你们的榜样电子邮件唯一索引)。不过,我仍然会用吃在preference先即使这样。

In many cases, where there are very few matching records and index-based access to find them, you'll find that the difference is trivial (where there is a unique index on "email" in your example). However, I would still use "take" in preference to first even then.

这篇关于拿VS Ruby的首场演出on Rails的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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