Slide 1

Slide 1 text

Today I want to talk about algorithms.

Slide 2

Slide 2 text

Today I want to talk about algorithms. an algorithm.

Slide 3

Slide 3 text

My name is Gordon Diggs.

Slide 4

Slide 4 text

I work here: www.paperlesspost.com @paperlesspost / @paperlessdev

Slide 5

Slide 5 text

But this talk isn't about that.

Slide 6

Slide 6 text

I collect these:

Slide 7

Slide 7 text

But I started getting a lot of them

Slide 8

Slide 8 text

So I made this: vinyl.gordondiggs.com

Slide 9

Slide 9 text

It makes these: ooooo...pretty!

Slide 10

Slide 10 text

At first, I did this settings.fields.each do |field| occurrences[field] = {} Item.all.each do |item| value = item.send(field.robotize) if occurrences[field][value] occurrences[field][value] += 1 else occurrences[field][value] = 1 end end if (others = occurrences[field].select{|k,v| v==1}.size) > 1 occurrences[field].delete_if{|k,v| v==1} occurrences[field]["Other"] = others end end

Slide 11

Slide 11 text

That sucks! Super slow

Slide 12

Slide 12 text

That sucks! With 9000 items, it takes ~23.4457 seconds

Slide 13

Slide 13 text

OK, fine. What do? Reduce the need to do it: cache the result, only recalculate when something changes

Slide 14

Slide 14 text

But it's still slow! Find another way to do it. Step 1: ditch mongodb for postgres

Slide 15

Slide 15 text

A new dawn awaits SQL has cool stuff, like counting and grouping (we want that, right?)

Slide 16

Slide 16 text

The new solution settings.fields.each do |field| occurrences[field] = {} items = DataMapper.repository.adapter.select( "SELECT #{field.robotize} as \"col\", COUNT(*) as \"times\" FROM items GROUP BY #{field.robotize} ORDER BY \"times\" desc") items.each do |item| occurrences[field][item[:col]] = item[:times] end if (others = occurrences[field].select{|k,v| v==1}.size) > 1 occurrences[field].delete_if{|k,v| v==1} occurrences[field]["Other"] = others end end

Slide 17

Slide 17 text

How fast is that? With the same 9000 items, it takes ~0.4663 seconds

Slide 18

Slide 18 text

Why is that? Because SQL is way better at this than we are.

Slide 19

Slide 19 text

The Benefits ● Can run it whenever I want without repercussions ● Scales way better ● Can stare at graphs all day

Slide 20

Slide 20 text

The Takeaway ● There are things that ruby does really well, and things that SQL does better.

Slide 21

Slide 21 text

Thank you. @gordondiggs