News for hackers. Stuff that matters.
Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @03:41PM (#13045730)
At work, we have an internally generated report that was done purely in ruby. It worked, barely, munging numbers for about 1000 records. When we hit 4000 records, the report took about 6 hours to run in ruby.

We looked for some obvious optimizations like eager loading and some map reduce magic and implemented them. That brought us to a range of 45 minutes to 2 hours.

We decided that it was still not good enough. We moved ALL of the report into SQL functions (Postgres) and we have the whole function running in 27 seconds.

I agree with some of the top comments that maintaining this is a little trickier than ruby code, but some ideas we came up with in maintenance: 1) Write the SQL in a ruby file that has a class method of sorts that returns the generated function. This allows for version control to track the changes of the sql function. 2) Focus on tests at the interface level. We had great test coverage on the end result prior to moving to SQL. We were able to re utilize those tests to verify accuracy with calculations that are now in SQL.

This really isn't for everyone, but for those that want to try something new, it is worth it.

Re:Let Postgres Do the Work (2013) by wsmith on Saturday November 26, 2016 @04:17PM (#13045909)
I once reduced the running time of a report from 45 minutes to 3 seconds (900x improvement) by moving the code inside the database.

If a programming language wants to stay fast it must eventually become a database. I realize this is an unpopular opinion but popularity is the wrong metric to judge by.

Re:Let Postgres Do the Work (2013) by Normal_gaussian on Saturday November 26, 2016 @04:57PM (#13046107)
576 hours -> 12 minutes (2880)

Admittedly the code I was moving away from was hilariously bad. Threaded custom build of PHP bad. Then again I haven't tried to optimise the code I wrote at all

You are absolutely right about fast code becoming a database, this is simply down to the query planner - it can try and do the least possible work for the data you actually have.

Re:Let Postgres Do the Work (2013) by inopinatus on Saturday November 26, 2016 @06:19PM (#13046604)
I recently used temporary per-transaction tables (CREATE TEMPORARY TABLE .. ON COMMIT DROP, basically CTEs that persist across statements, and that can be indexed) with json_to_recordset and turned a three-minute ruby ETL background process into a sub-1-second inline call.
Re:Let Postgres Do the Work (2013) by majewsky on Saturday November 26, 2016 @06:25PM (#13046632)
CREATE TEMP TABLE is really awesome. Not really related, but I used it at my previous gig to optimize the unit tests. They would previously all use the same database that devs use during development, so scans over some large tables were particularly inefficient, and tests could break when someone modified table contents for some development task.

I implemented a small function where you could list the tables that you were working with, and it would create empty temporary tables with the same name in a namespace that takes precedence over the normal namespace in postgres' search_path, therefore giving you a blank slate to work with for the unit test, while the other tables were still populated with useful data. (Some of these data come from expensive infrastructure scanning jobs, so starting from an entirely empty database was not an option.)

Re:Let Postgres Do the Work (2013) by debaserab2 on Sunday November 27, 2016 @12:54AM (#13047953)
What language were you using to do that?

Just curious - not sure if you were using Ruby like the parent above. I literally just built a library to back ActiveRecord models with temporary tables. It's very useful since you can use AR scopes off of your temporary table then (I had to do this instead of using CTE's because my DB currently doesn't support CTE's)

https://github.com/ajbdev/acts_as_temptable

Just thought I'd share it in case it helps, or to hear if anyone else has a better approach to this. (warning: it's very experimental and not production tested yet). I couldn't find any other gem that would do this.

Re:Let Postgres Do the Work (2013) by inopinatus on Sunday November 27, 2016 @02:19AM (#13048149)
Cool idea.

How did you resist the temptation to call it temptation?

Although I think temptable is almost the opposite of contemptible, therefore also good going in the pun department.

My code is all Ruby, and I ended up pushing all the work into SQL and just eventually selecting with find_by_sql into existing models for the results. There is possibly concurrent invocation vs updates and a race against itself, so it's also all wrapped with a serializable transaction and starts with a mutex lock on the controlling model.

Re:Let Postgres Do the Work (2013) by debaserab2 on Sunday November 27, 2016 @02:55AM (#13048237)
Hah! I was sitting around trying to think of a clever name, but then I got tired of sitting around and just went with temptable. Had I thought of temptation I would have gone with that.

The approach I went with works really well for cases when you want to persist the temporary table through the end of the request (it works good for master/index type views that may have a lot of sums/counts and filter options available on it).

Re:Let Postgres Do the Work (2013) by harperlee on Sunday November 27, 2016 @07:22AM (#13048804)
One shower idea that I had is the concept of a clojure version that instead of compiling to JVM bytecode, javascript or CLR, compiled to postgreSQL. I think that would be awesome: you could just run the same functions, seamlessly, from the database, through the web server, to the browser. And, whilst of course you need to know about the limitations of the database, it could be great for pushing code to the database seamlessly.

Unfortunately I don't think I have the skills for that, so posting here in the hopes that someone that can likes the idea :)

Re:Let Postgres Do the Work (2013) by brassic on Sunday November 27, 2016 @08:28AM (#13049015)
This is kind of what LINQ does. You write a query in C#. The structure of the query (like an abstract syntax tree) can be exposed to the query provider which can interpret it or compile it to the CLR or compile it to SQL or whatever.
Re:Let Postgres Do the Work (2013) by Someone on Sunday November 27, 2016 @11:03AM (#13049796)
A sufficiently advanced LINQ (http://wiki.c2.com/?SufficientlySmartCompiler) would do wonders in some cases, but I haven't encountered it. AFAIK, LINQ to SQL only knows about joins and some aggregate functions.

One thing that makes improving it cumbersome is that the semantics of many operations are slightly different in the database than in C#. For example, SQL Server doesn't do NaNs and infinities, supports a zillion string string collations, and its date types may not 100% map to C# ones.

Also, databases may run stored procedures faster than the SQL that LINQ genrerates on the fly because they can cache their query plans (most databases will detect that an on-the-fly query is equal to one run recently, though, so this may be somewhat of a moot argument)

Re:Let Postgres Do the Work (2013) by tracker1 on Sunday November 27, 2016 @10:43AM (#13049680)
Which can lead to great, or less ideal results.. Seeing some of the resulting queries via monitoring are wild though.
Re:Let Postgres Do the Work (2013) by __s on Sunday November 27, 2016 @10:56AM (#13049748)
At work we've got a LINQ query that gets passed around a few functions, eventually getting into a core of 60 lines of LINQ logic. Colleague verified that chaining selects produces different output, but gets ran at the same speed (Chain selects in order to somewhat declare variables, Select(x => new { x, y = <query-x> }) then you can Select(xy => new { use xy.y multplie times }))

Sometimes I think I should just be using sql.. (which we do on other projects)

Re:Let Postgres Do the Work (2013) by tracker1 on Monday November 28, 2016 @09:29AM (#13055557)
Yeah, I've been pretty happy without ORM in node.js, I even wrote a semi-nice wrapper so I could turn template strings into parameterized queries. Made writing a bunch of migration scripts a cakewalk.

Sometimes it's really just easier to write SQL directly.

Re:Let Postgres Do the Work (2013) by vincentdm on Saturday November 26, 2016 @09:00PM (#13047291)
Do you mean something like Datomic? It has been intriguing me for a while...
Re:Let Postgres Do the Work (2013) by dom0 on Saturday November 26, 2016 @04:23PM (#13045944)
Hence Kx, ZODB.
Re:Let Postgres Do the Work (2013) by takeda on Sunday November 27, 2016 @03:03AM (#13048253)
I'm not too familiar with ZODB, but it looks like it tries to impose OO on a database, while in reality relational model works best with data, so making programing language able to interact with data that way would be better. I think something like JOOQ[1] is close to that.

In order to get good performance you want to minimize number of back and forth requests over network. So instead of making a request to obtain list of items, and then fetching each of the item one by one (so called N+1 select issue) you will get a better performance if you make the database send only the data you want, nothing more, nothing less.

[1] http://www.jooq.org/

Re:Let Postgres Do the Work (2013) by dom0 on Sunday November 27, 2016 @05:25AM (#13048572)
> I'm not too familiar with ZODB, but it looks like it tries to impose OO on a database, while in reality relational model works best with data, so making programing language able to interact with data that way would be better.

ZODB itself is essentially a transactional single-object store, where that "single object" is usually the root node of an arbitrary (possibly cyclic) object graph. Storage depends on the backend, nowadays you'd usually use file storage for development and relstorage for any kind of deployment.

It doesn't have any query language, Python is. There are no indices beyond those that you explicitly create (eg. a dictionary would be a simple index, but other packages provide more complex indices, like B-Trees - the nice thing here is that anything that goes into the DB is fully transacted, which removes a lot of headache that you'd have with other solutions (eg. flat XML files)).

ZODB thrives when your data model is too complex to easily or efficiently map into SQL, when you don't do or need efficient complex ad-hoc queries and when 180 % performance and concurrency (although this was recently improved through the MVCC reimplementation) isn't the highest priority. Since it's pretty good at caching (much better than most ORMs) performance usually doesn't suck.

Re:Let Postgres Do the Work (2013) by the-dude on Saturday November 26, 2016 @04:29PM (#13045974)
ZODB is not a database, nor is it fast.
Re:Let Postgres Do the Work (2013) by dom0 on Saturday November 26, 2016 @04:32PM (#13045986)
More like a database toolkit. Performance depends on application but was clearly never the top priority. (Can still be pretty impressive in comparison due to the rather effective instance caching)
Re:Let Postgres Do the Work (2013) by PeCaN on Saturday November 26, 2016 @08:19PM (#13047145)
Yep, the K interpreter is pretty mediocre (it is very small and the language is inherently pretty fast, but the interpreter is otherwise not very advanced¹) but being directly integrated with a very optimized in-memory column-store database means it smokes anything else. Even the massive engineering effort in a JVM+RDBMS simply can't compete with running K directly on memory-mapped files.

1. I believe they added SIMD support a year or two ago and got a massive (2-10× IIRC) speedup.

Re:Let Postgres Do the Work (2013) by dllthomas on Saturday November 26, 2016 @03:59PM (#13045820)
> This allows for version control to track the changes of the sql function.

You... realize you could just put the SQL itself in git, right? Not saying there aren't potential benefits, but "it can go in version control" isn't one of them.

Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @04:02PM (#13045838)
Absolutely! We use rails. I just mentioned doing it in that method as opposed to throwing it in a migration. That migration shouldn't change in version control. We wanted a history of the changes and an easy way to access it in migrations.

Sorry for not explaining that fully.

Re:Let Postgres Do the Work (2013) by rpeden on Saturday November 26, 2016 @04:58PM (#13046123)
Makes sense.

I suppose you could also execute "REPLACE FUNCTION" and redefine the inside a new migration each time you want to change it. The downside is that you end up with the whole function in new a migration every time you want to change if. But if you're not updating your stored procedures very frequently (which is usually the case, IME), it probably wouldn't be too bad.

Re:Let Postgres Do the Work (2013) by jeffasinger on Saturday November 26, 2016 @05:12PM (#13046200)
At work we use a rather idiosyncratic tool called RoundhousE to deploy our SQL. It treats sql labelled as a stored procedure differently, and will run the file on every deploy, as opposed to scripts that alter the schema, which are only ran once, and may not be modified.

The system works pretty well.

Re:Let Postgres Do the Work (2013) by slagfart on Saturday November 26, 2016 @05:31PM (#13046311)
You don't need a tool for this. If you store base data in one schema, and functions/views in another, you can update this second schema willy nilly without worrying about damaging your data.

Just start the second file with DROP SCHEMA IF EXISTS schemaname CASCADE; CREATE SCHEMA schemaname;. Then, make sure any functions/views/permissions built on top of this schema are also contained within this file.

Re:Let Postgres Do the Work (2013) by inopinatus on Saturday November 26, 2016 @06:21PM (#13046612)
Or you can just pull the SQL for the stored procedure from a table column and execute it dynamically.

Now it's an ops problem :-)

Re:Let Postgres Do the Work (2013) by jessaustin on Sunday November 27, 2016 @07:54AM (#13048903)
Haha, usually we hear this from smart guys who have been working for about 8 months...
Re:Let Postgres Do the Work (2013) by angersock on Saturday November 26, 2016 @10:45PM (#13047573)
Oh, that's slick. Nice!
Re:Let Postgres Do the Work (2013) by inopinatus on Saturday November 26, 2016 @06:09PM (#13046542)
Or you can just define the function every time you use it. https://www.postgresql.org/docs/current/static/sql-do.html
Re:Let Postgres Do the Work (2013) by majewsky on Saturday November 26, 2016 @06:26PM (#13046640)
That won't help if you want to use it for indexing, as the article demonstrated.
Re:Let Postgres Do the Work (2013) by pmontra on Sunday November 27, 2016 @02:55AM (#13048239)
You are right but a Rails migration would provide a way to deploy to the database. Far from ideal though, because you could have many migrations with that SQL code, one for each version of the code. Not sure what's the best solution in this case.
Re:Let Postgres Do the Work (2013) by semi-extrinsic on Saturday November 26, 2016 @04:50PM (#13046082)
I'm not a database guy, but I have to ask: you're talking about 4000 records, which is essentially nothing.

Why not just load all the data from SQL into a DataFrame in Python or R and do the processing there? I've written some pretty big R DataFrame crunching scripts, including very wide-window moving averages operating on tens of billions of records, that run in less than five minutes on my laptop.

Hell, I have a web app built with Flask+Pandas, running on a single t2.small instance, that does complex queries into a 6000 records big chemistry data set based on user input of selectors. It can serve thousands of simultaneous users without noticeable slowdown.

What am I missing here?

Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @04:57PM (#13046117)
4000 records with a one-to-many join (the many part capped at 240 records). The aggregations are done mostly on that join table.

In our original ruby process, it would return just that, 240 records per join and aggregate on that. Our SQL process aggregates that in a stored procedure and returns the flat 4000 records.

We really didn't want to introduce another piece of technology to this stack, hence we didn't look at R or Python or Elixir. We did consider storing all of this inside elasticsearch for a hot second but keeping things in sync at the correct times seemed sub optimal and would add a layer of complexity we were all uncomfortable with.

Re:Let Postgres Do the Work (2013) by jstimpfle on Saturday November 26, 2016 @07:36PM (#13046950)
ONE join? A join is usually just a linear scan through one table, doing hashtable lookups on the other table. If there is no hash table, at the worst both tables have to be sorted on the join key and scanned linearly.

The absolute worst case of a "join" is a cartesian product which runs in quadratic time. 6000^2 = 36000000, which should still easily finish in ~1 second.

Re:Let Postgres Do the Work (2013) by rtpg on Saturday November 26, 2016 @11:30PM (#13047718)
This really sounds like you're having an issue linked to the ORM.

I don't know the Rails equivalent, but Django's ORM has a way of prefetching related fields on a model in a single query.

My bet is that your code wasn't 1 query, but actually 4000 * 240 queries, which will always take a while.

EDIT: I just saw in a sibling comment that the actual data fetching was only taking 30s. I imagine you were doing that part mostly right

Re:Let Postgres Do the Work (2013) by memracom on Saturday November 26, 2016 @07:48PM (#13047007)
You can install R or Python into a PostgreSQL server as a language extension and then use it to define SQL functions. This is really useful to define functions which can reduce the size of the recordset transmitted across the network.

You wouldn't likely put the whole app to run on the database server, but typically PostgreSQL makes good use of the available RAM and disk, but leaves lots of CPU cores idle. So running some R and Python on the PostgreSQL server is an overall improvement in performance.

P.S. Yes, I know that PostgreSQL 9.6.1 will use multiple cores for aggregate and join queries on large tables, but few people have upgraded to that yet.

Re:Let Postgres Do the Work (2013) by mst on Saturday November 26, 2016 @04:57PM (#13046118)
My immediate guess would be that they already knew SQL and had the data in there - and both R and pandas have a tricky initial learning curve if you're not familiar with dataframe style operations (hell, I was a pure math major some years ago and I have to contort my brain to do dataframes whereas I long since got comfortable with SQL).
Re:Let Postgres Do the Work (2013) by tkyjonathan on Tuesday November 29, 2016 @09:14AM (#13063114)
It could be that each of the 4000 records needs another 10k records each to get the report you are looking for.
Re:Let Postgres Do the Work (2013) by azernik on Saturday November 26, 2016 @04:08PM (#13045866)
Oh absolutely. I've tried doing complicated logic with (MySQL) stored procedures. It's painful. Definitely send the queries from a sane programming language.

One way to think about this is that you just want to take control back from the ORM of the exact database queries sent down the pipe. Which, BTW, is why every ORM worth its salt has functions for dropping down to plain SQL - teach that as best practice for doing computation in SQL queries, not stored procedures.

Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @04:14PM (#13045896)
Most ORMs will not do the level of aggregations you want or need without writing raw SQL within your ORM. If you are already writing raw SQL in your ORM for aggregation level computations, you might as well create a stored procedure, make it IMMUTABLE (if you can) and gain those performance benefits.

At the end of the day, I would just use the tool that has the most comfortability with the team on the project. You can obviously start with an ORM, but there may come a point in time where performance needs a little boost.

Re:Let Postgres Do the Work (2013) by azernik on Saturday November 26, 2016 @04:26PM (#13045958)
What are the real performance benefits of stored procedures over sending the hand-written queries over the wire through your ORM? I suspect small to none, and at the cost of having code that lives in the database rather than in version-controlled code.
Re:Let Postgres Do the Work (2013) by gaius on Saturday November 26, 2016 @04:31PM (#13045979)
code that lives in the database rather than in version-controlled code

If you think you can't version control stored procs, then you are doing something badly, badly wrong.

What's the difference between a "script" which is a text file on a filesystem at the end of the day, versus a "stored proc" which is a block of text in a database, loaded off a file in a filesystem?

Re:Let Postgres Do the Work (2013) by jstanley on Saturday November 26, 2016 @04:38PM (#13046025)
The "script" gets deployed simply and reliably using your existing tooling, and stays in sync with the surrounding code.
Re:Let Postgres Do the Work (2013) by tokenizerrr on Saturday November 26, 2016 @04:57PM (#13046113)
Replace the stored procedure from version control at deploy time.
Re:Let Postgres Do the Work (2013) by azernik on Saturday November 26, 2016 @05:17PM (#13046231)
And then you have to make sure your deploys are atomic so that the app code and stored procedures don't get out of sync on failed deploys...

Or your stored procedures now become one more thing that has to be backwards compatible, in addition to your DB schema...

I've run into all of these problems with stored procedures, and they SUCK. Good deployment tooling can solve them, but it's a lot of effort for questionable reward.

Re:Let Postgres Do the Work (2013) by gaius on Sunday November 27, 2016 @04:05AM (#13048401)
a lot of effort for questionable reward

It's a small amount of effort for a thousand-fold increase in performance and not having to re-implement the same functionality in every app and in every language that connects to that DB. YMMV.

Re:Let Postgres Do the Work (2013) by icebraining on Sunday November 27, 2016 @04:45AM (#13048492)
The argument was between using SQL in stored procedures or SQL in queries sent by the application, so you don't have to re-implement anything (just share the SQL queries between apps), and I very much doubt you'll get a thousand-fold increase in performance.
Re:Let Postgres Do the Work (2013) by spdionis on Monday November 28, 2016 @10:29AM (#13055982)
Thousand-fold increase in performance by shaving off network and query compilation time?
Re:Let Postgres Do the Work (2013) by kofejnik on Saturday November 26, 2016 @05:47PM (#13046403)
this absolutely mirrors my experience
Re:Let Postgres Do the Work (2013) by grzm on Saturday November 26, 2016 @04:38PM (#13046023)
One performance benefit of stored procedures in Postgres is that you can perform a multi-statement transaction in the stored procedure, saving the network overhead of multiple round-trips between the application and the database. This may not be a common occurrence for many applications, but it is a legitimate performance benefit when the need arises.
Re:Let Postgres Do the Work (2013) by anarazel on Saturday November 26, 2016 @04:57PM (#13046119)
Note that the network protocol allows pipelining for multiple statements (including transaction control ones), too. Unfortunately only few client libraries (e.g. pgjdbc) make use of that :(
Re:Let Postgres Do the Work (2013) by azernik on Saturday November 26, 2016 @05:16PM (#13046221)
I don't know about Rails and ActiveRecord, but I can do this in Slick.
Re:Let Postgres Do the Work (2013) by kofejnik on Saturday November 26, 2016 @08:09PM (#13047100)
you can do multi-statement and nested transactions even in Rails
Re:Let Postgres Do the Work (2013) by mst on Saturday November 26, 2016 @04:59PM (#13046129)
If you're already version controlling your schema, the CREATE OR REPLACE FUNCTION call lives in whatever's version controlling your schema.

If you're not already version controlling your schema, you should seriously consider starting whether you're using sprocs or not.

Re:Let Postgres Do the Work (2013) by feld on Sunday November 27, 2016 @09:04AM (#13049165)
You can literally do version control in the database as well. Upgrade and downgrade your entire scheme at will. Unfortunately the only open source software I'm aware of that does this is Archiveopteryx

http://archiveopteryx.org/db/mailstore

http://archiveopteryx.org/db/schema

Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @04:33PM (#13045994)
Honestly, it's yak shaving at that point. Like the article pointed out, you can create an index on a stored procedure that is IMMUTABLE. That will have some effect on the performance of the query.

There are other patterns you can use to track via version control the changes for your stored procedure.

Re:Let Postgres Do the Work (2013) by quink on Saturday November 26, 2016 @03:59PM (#13045815)
> report

Always pure SQL, at least as you're using a DBMS. Never anything else.

SQL can stretch to full Turing completeness, so it'll always be good enough for that. And between JSON support and PLPGSQL most everything can be done in Postgres.

Re:Let Postgres Do the Work (2013) by andrewprock on Saturday November 26, 2016 @05:08PM (#13046177)
Unfortunately, "pure sql" usually means passing in strings, which leaves you without good tooling to manage the code. This can greatly slow down the speed of delivery. It's great if you are in an ossified section of the code/database, but it can be very brittle (or expensive to validate) otherwise.
Re:Let Postgres Do the Work (2013) by quink on Saturday November 26, 2016 @06:07PM (#13046533)
Modern RDBMSes can handle most of the ORM use cases at their end. What's wrong with VIEWs, for instance?

Plus it solves the whole schema impedance mismatch thing to just leave it up to the DB.

ORMs aren't that great. Having spent the past five years primarily with an object database that is also fully SQL (ISC Caché) I find myself preferring SQL more and more. Even when you have the ORM within the bloody database I'm going SQL.

The string thing isn't an SQL problem, it's a tooling one.

The only tool per se should be pgAdmin or an equivalent. And in your application the only "SQL" that should be written is SELECT * FROM viewforthisfunctionality and select runupsertwithjsonobjectthatpgorplpgsqlcanreadjus tfine(json)

Re:Let Postgres Do the Work (2013) by herge on Saturday November 26, 2016 @03:51PM (#13045779)
Have you considered a hybrid approach, like doing the heavy lifting/querying/calculating in Postgres, but the final formatting of the report (something that might change often, or be very finicky to implement in pl/sql or in the db) is left to ruby?
Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @03:54PM (#13045792)
That's exactly what we did. We return an anonymous table of records in Postgres that are then passed to ruby to render an excel file. The SQL query takes 27s on average. The ruby view generation is fast as well but I haven't profiled that part of the report.
Re:Let Postgres Do the Work (2013) by koolba on Saturday November 26, 2016 @04:45PM (#13046049)
> That's exactly what we did. We return an anonymous table of records in Postgres that are then passed to ruby to render an excel file. The SQL query takes 27s on average. The ruby view generation is fast as well but I haven't profiled that part of the report.

What's an "anonymous table of records"? Do you mean it's just a result set (i.e. query)?

Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @04:49PM (#13046073)
Yea. It returns a table of rows, where the return columns are defined in the stored procedure. I may have used the wrong nomenclature. I was trying to differentiate between a table in a database schema versus one defined in a function.
Re:Let Postgres Do the Work (2013) by mst on Saturday November 26, 2016 @05:01PM (#13046142)
"The stored procedure returns a multi-row resultset via a SETOF[ROWTYPE] return value" might be a clearer description?
Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @05:08PM (#13046172)
Thank you!!!!
Re:Let Postgres Do the Work (2013) by odammit on Saturday November 26, 2016 @04:14PM (#13045897)
I've had similar experiences. And it's not always Ruby processing being the slow part but returning multiple result sets to your application layer.

I've seen some huge result sets (thousands of rows) returned to do a simple aggregation for a dashboard widget.

Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @04:15PM (#13045904)
Absolutely agreed. In our particular situation, the ruby calculations were the bottlenecks based on our benchmarks.
Re:Let Postgres Do the Work (2013) by tkyjonathan on Tuesday November 29, 2016 @06:49AM (#13062433)
I took a 20-page multi-threaded ruby code that took 2-4 days to complete and changed it to a 1-page MySQL stored procedure with some @variables. Brought it down to 4 minutes to run. And to top it off, reduce the overall server load from 96% to 20%.

I kept that CPU usage chart and added it to my CV as it was quite impressive to look at.

Re:Let Postgres Do the Work (2013) by dom0 on Sunday November 27, 2016 @08:33AM (#13049025)
Correct usage of ORMs if it's not in the database is another important and easy thing to get right. This often gives a 10-1000 times performance improvement just due to the avoided "ping pong" game by using eg. query annotations that tell the ORM which related objects you'll access.
Re:Let Postgres Do the Work (2013) by davyjones on Saturday November 26, 2016 @08:01PM (#13047072)
6 hours to 15 minutes here. This is with the right indices and stock install. I am sure an expert can do much better.
Re:Let Postgres Do the Work (2013) by bbcbasic on Saturday November 26, 2016 @05:20PM (#13046243)
27s is still an awfully time. Why does it take so long?
Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @05:25PM (#13046274)
We didn't make it immutable and we didn't add an index to it. Those are our next action items to help improve the performance of it.

Honestly, instead of looking into more performance gains, I wanted the team to get a huge win and for the team to look good in front of senior management :D.

Re:Let Postgres Do the Work (2013) by majewsky on Saturday November 26, 2016 @06:30PM (#13046658)
If you want to optimize for looking good in front of senior management, you would put a sleep() in there to make it run for 5 minutes. Then when senior mgmt comes back and complains that 5 minutes is still too much, you remove the sleep(), bill a few hours to time tracking, and praise the new performance improvements that you just deployed. ;)
Re:Let Postgres Do the Work (2013) by striking on Saturday November 26, 2016 @07:52PM (#13047019)
Re:Let Postgres Do the Work (2013) by majewsky on Saturday November 26, 2016 @08:55PM (#13047274)
Yes, this is exactly the article that I had in mind earlier. Thanks for digging it up.
Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @07:08PM (#13046824)
Hahahahahaha.
Re:Let Postgres Do the Work (2013) by bbcbasic on Saturday November 26, 2016 @05:28PM (#13046293)
Ha ha politics driven development. Love it.
Re:Let Postgres Do the Work (2013) by phereford on Saturday November 26, 2016 @05:43PM (#13046379)
Ehh. More about boosting confidence and team morale than politics. The goal was achieved, and we can yak shave during slack time if need be.