# Rewriting the scope\_all benchmark in Sequel and Raw

**URL:** https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138
**Category:** Uncategorized
**Created:** [May 14, 2017, 6:59pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138 "2017-05-14T18:59:09Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 14, 2017, 6:59pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/1 "2017-05-14T18:59:09Z")

</div>

@sam I wrote `scope_all` benchmark in raw sql to compare with sequel and AR. If the [test](https://github.com/ruby-bench/ruby-bench-suite/pull/76/files#diff-8c4c2c66d266306b23906a180dfbb5baR28) is correctly written these are results I got:

activerecord/postgres\_scope\_all

* * *

- its/s : **116**
- its/s deviation : **7.71**
- total allocated objects per iteration : **14128**

sequel/postgres\_scope\_all

* * *

- its/s : **414**
- its/s deviation : **9.65**
- total allocated objects per iteration : **5032**

raw/postgres\_scope\_all

* * *

- its/s : **307**
- its/s deviation : **5.53**
- total allocated objects per iteration : **5018**

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 15, 2017, 12:29am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/2 "2017-05-15T00:29:03Z")

</div>

How can raw be slower than sequel, this is quite interesting, can you link to the sequel bench.

Also I think we should probably maybe do something with the results, perhaps if we just formatted a giant string with the results each time?

```auto
str = ""
User.all.each do |u|
   str << "name #{u.name} email #{u.emal}\n"
end 

```

This kind of makes the bench bit more honest cause it is actually trying to achieve something.

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 15, 2017, 9:31am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/3 "2017-05-15T09:31:11Z")

</div>

@sam Here is the same [sequel bench](https://github.com/ruby-bench/ruby-bench-suite/blob/master/sequel/benchmarks/bm_sequel_scope_all.rb#L30).

> How can raw be slower than sequel

I wasn’t expecting it neither. Maybe it’s slower because [raw query](https://github.com/ruby-bench/ruby-bench-suite/pull/76/files#diff-8c4c2c66d266306b23906a180dfbb5baR28) is made through `ActiveRecord::Base.connection`. Perhaps raw sql queries in sequel and AR perform differently. I’ll write bench for raw sql through sequel to compare.

> Also I think we should probably maybe do something with the results

Right know we convert to array after fetching. But doing something more specific is better idea, will fix it 👍

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 15, 2017, 11:47am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/4 "2017-05-15T11:47:18Z")

</div>

I added one more test to measure raw sql through sequel. Note that I added some operations on fetched records in scope\_all benches, which caused baseline to go little lower, but differences remained the same.

Again, these are results I got:

* * *

## [activerecord](https://github.com/bmarkons/ruby-bench-suite/blob/add-bm-raw-scope-all/rails/benchmarks/bm_activerecord_scope_all.rb#L28)

its/s : **77**  
deviation : **3.85**  
allocated objects : **20136**

## [raw sql through activerecord](https://github.com/bmarkons/ruby-bench-suite/blob/add-bm-raw-scope-all/rails/benchmarks/bm_activerecord_scope_all_raw_sql.rb#L28)

its/s : **285**  
deviation : **5**  
allocated objects : **6020**

## [sequel](https://github.com/bmarkons/ruby-bench-suite/blob/add-bm-raw-scope-all/sequel/benchmarks/bm_sequel_scope_all.rb#L30)

its/s : **345**  
deviation : **6**  
allocated objects : **6035**

## [raw sql through sequel](https://github.com/bmarkons/ruby-bench-suite/blob/add-bm-raw-scope-all/sequel/benchmarks/bm_sequel_scope_all_raw_sql.rb#L30)

its/s : **350**  
deviation : **8**  
allocated objects : **7040**

* * *

Interesting thing to note here is performance in both sequel tests is similar.

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 15, 2017, 12:26pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/5 "2017-05-15T12:26:16Z")

</div>

@sam now I figured out that you weren’t probably thinking of executing raw sql commands through orm’s like AR and sequal, but directly via pg and mysql gems. 😕

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 15, 2017, 1:37pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/6 "2017-05-15T13:37:39Z")

</div>

Oh yeah you got to use raw connections for raw tests:

So you would use:

```auto
 ActiveRecord::Base.connection.raw_connection
=> #<PG::Connection:0x00563053b11f78>

```

Don’t use the AR abstraction on top of connection as it is wasteful.

Also, keep in mind Sequel has tricks:

> **[GitHub - jeremyevans/sequel\_pg: Faster SELECTs when using Sequel with pg](https://github.com/jeremyevans/sequel_pg)**
>
> Faster SELECTs when using Sequel with pg. Contribute to jeremyevans/sequel\_pg development by creating an account on GitHub.

We got to test with this as well.

> Note for Jeremy

Our long term plan is to make this graph/page here:

[https://rubybench.org/rails/rails/commits?result\_type=activerecord/postgres\_scope\_all\_with\_default\_scope&display\_count=2000](https://rubybench.org/rails/rails/commits?result_type=activerecord/postgres_scope_all_with_default_scope&display_count=2000)

Show Sequel numbers and raw numbers as well.

But we got to get all the benches into good shape first.

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 15, 2017, 2:10pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/7 "2017-05-15T14:10:27Z")

</div>

Here is one [raw test](https://github.com/ruby-bench/ruby-bench-suite/pull/76/files#diff-47f8144a829821b7f7521d566e86144e) for `postgres_scope_all`, I did it this time directly via pg gem.

Results I got locally are following:

its/s : **942**  
allocated objects: **4004**

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 15, 2017, 3:16pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/8 "2017-05-15T15:16:31Z")

</div>

Its interesting to see 77 vs 942, it really drives home how much overhead AR brings to the table.

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 16, 2017, 1:57pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/9 "2017-05-16T13:57:37Z")

</div>

Can you update that post with all your results to date on this specific test?

Also, can you run it on Rails 2/3/4 for a quick and dirty comparison.

(I know it will be eventually backfilled, but I am curious) 😉

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 16, 2017, 6:55pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/10 "2017-05-16T18:55:30Z")

</div>

sure @sam, I’m on it 👍

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 16, 2017, 8:33pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/11 "2017-05-16T20:33:03Z")

</div>

```
// Activerecord

{
  "label":"activerecord/postgres_scope_all",
  "version":"3.2.22.5",
  "iterations_per_second":76.49321589608218,
  "iterations_per_second_standard_deviation":2.614610951534639,
  "total_allocated_objects_per_iteration":43179
}

{
  "label":"activerecord/postgres_scope_all",
  "version":"4.2.8",
  "iterations_per_second":80.02798516365223,
  "iterations_per_second_standard_deviation":3.748688654181643,
  "total_allocated_objects_per_iteration":21153
}

{
  "label":"activerecord/postgres_scope_all",
  "version":"5.1.1",
  "iterations_per_second":77.37787266441069,
  "iterations_per_second_standard_deviation":3.8770773823300333,
  "total_allocated_objects_per_iteration":20131
}

// Sequel

{
  "label":"sequel/postgres_scope_all",
  "version":"4.46.0",
  "iterations_per_second":342.1849354356875,
  "iterations_per_second_standard_deviation":5.2603133966378355,
  "total_allocated_objects_per_iteration":6033
}

// Raw pg

{
  "label":"raw_pg/postgres_scope_all",
  "version":"0.18.1",
  "iterations_per_second":810.0042539142364,
  "iterations_per_second_standard_deviation":7.530824647552864,
  "total_allocated_objects_per_iteration":4004
}

```

^ Here it is @sam 🙂

Btw, I didn’t managed to run benchmark for rails 2, I couldn’t require rails gem in benchmark script. Not sure where’s the problem 😕

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 16, 2017, 9:01pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/12 "2017-05-16T21:01:48Z")

</div>

Awesome, now that you have this data. I want the graph amended so it can display a comparison of all these 5 results! Start hacking away at the web UI and the backfill.

**Only focus on this single test** , this is the only test you care about till we can render the 5 lines here and get it backfilled 😉

[https://rubybench.org/rails/rails/commits?result\_type=activerecord/postgres\_scope\_all&display\_count=2000](https://rubybench.org/rails/rails/commits?result_type=activerecord/postgres_scope_all&display_count=2000)

If you have to drop the whole “prepared” vs “non prepared” stuff and always do “prepared” for now so be it.

cc @system

> [@bmarkons](#):
>
> didn’t managed to run benchmark for rails 2

Don’t worry about Rails 2 for now.

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 17, 2017, 11:01am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/13 "2017-05-17T11:01:56Z")

</div>

@sem Just to make myself sure. Since this is graph with commit data, you want me to include here commits for sequel and pg. Therefore it would have 3 lines (AR, sequel, pg) and graph would look something like this:

 ![](https://us1.discourse-cdn.com/flex016/uploads/rubybench/original/1X/b7737f24a916cabd2814816f9124a52c1882dbcd.png)

Maybe better place to display results for older Rails releases would be here?  
[https://rubybench.org/rails/rails/releases?result\_type=activerecord/postgres\_scope\_all](https://rubybench.org/rails/rails/releases?result_type=activerecord/postgres_scope_all)

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 17, 2017, 2:21pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/14 "2017-05-17T14:21:07Z")

</div>

> [@bmarkons](#):
>
> you want me to include here commits for sequel and pg. Therefore it would have 3 lines (AR, sequel, pg) and graph would look something like thi

Yes, absolutely, this is what I want you to work towards!

> [@bmarkons](#):
>
> Maybe better place to display results for older Rails releases would be here?

Possibly, let’s not worry about that for now, just for now worry about getting the graph with the 3 lines going and backfilling all the info. Work with @system on gaining access to prd.

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 24, 2017, 11:39am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/15 "2017-05-24T11:39:43Z")

</div>

How are you going with this? Are you blocked on anything from our side?

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 24, 2017, 11:59am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/16 "2017-05-24T11:59:26Z")

</div>

@sam, I am not blocked on anything. Right now I am working on web to display graphs to compare. My first goal is to display sequel along with AR because sequel has been already added. Here’s wip [PR](https://github.com/ruby-bench/ruby-bench-web/pull/199) on web.

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [May 24, 2017, 1:14pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/17 "2017-05-24T13:14:42Z")

</div>

Fantastic, focus on getting stuff merged and on the site as soon as you can. I prefer to have a slightly janky UI than wait weeks till we have the UI in perfect state.

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [May 25, 2017, 11:53am UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/18 "2017-05-25T11:53:41Z")

</div>

@sam  
Little peek. This is with data from production imported in local. UI is not perfect and has some issues but before I get into fixing it I’ll make sure sequel benchmark graphs are being displayed.

 ![](https://us1.discourse-cdn.com/flex016/uploads/rubybench/original/1X/8602a76ff60a1686037dd1ac20c5fc2ff8dc11fb.png)
* * *

Though I am not sure why sequel is not being displayed since it looks like been added 😕

> <https://github.com/ruby-bench/ruby-bench-web/pull/171>

* * *

@system pls take a look at this one ⬇ 🙏

> <https://github.com/ruby-bench/ruby-bench-web/pull/199>

---

<div class="post-metadata">

### Author: ![sam](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/sam/32/5_2.png) [@sam](https://community.rubybench.org/u/sam)
#### Post date: [June 5, 2017, 4:08pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/19 "2017-06-05T16:08:26Z")

</div>

Cool!!! how far do you feel we are from having something on [rubybench.org](http://rubybench.org)?

---

<div class="post-metadata">

### Author: ![bmarkons](https://sea2.discourse-cdn.com/flex016/user_avatar/community.rubybench.org/bmarkons/32/87_2.png) [@bmarkons](https://community.rubybench.org/u/bmarkons)
#### Post date: [June 5, 2017, 8:34pm UTC](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138/20 "2017-06-05T20:34:23Z")

</div>

Hey @sam,

[PR](https://github.com/ruby-bench/ruby-bench-web/pull/199) is some time in reviewing process. According to me it’s ready to be merged. When it’s merged we could compare benchmarks on rubybench.

Jeremy configured sequel repo with webhook to send push events on rubybench, so next time when he pushes something we should have sequel repo with it’s benchmarks on rubybench 🎉

[Next page](https://community.rubybench.org/t/rewriting-the-scope-all-benchmark-in-sequel-and-raw/138.md?page=2)
