59 points · by okket · 10y ago · 33 comments
This is why any decent columnar/timeseries db has its own often arrish query language to take advantages of ordering information inherent in the data. There is an order in that index and SQL table, you just don't have good tools to access and use it.
This touches on a conversation I was having a couple weeks ago where somebody was saying that column/array storage is purely a backend consideration and don't need to care about it in the front end of the database. That view gets you ugly primitives like LAG/LEAD. Being able to exploit your backend decision is paramount to performance.
Products like Informix or KDB+ just give you full access to the data on and off disk as an array so you ca build complex windowing primitives (and even store them and reuse them later).
(PS: I just find this really funny. KDB+ is probably is the only database I know that literally does streaming analytics on a Raspberry Pi. No joke, the full DB on Linux ARM/Pi. https://kx.com/download/ )
When the "Why's that company so big? I can do that in a weekend." article came out last week:
https://news.ycombinator.com/item?id=12626314
I immediately thought of Kx and KDB and how it is literally the exact opposite. If people only knew how small the firm started with Arthur hacking away seemingly out developing entire corporations they would laugh.
"Yeah give me about two years and a dev team of a about 20 then about 10 for QA and we've have something shippable... abother year or two for performance... 5 years we'll be in the hunt." Yeah, we'll this guy over here basically did it in six months with only two others. And it's 100x faster than anything else. Why can't you do that?
Given his history too with A+ at Morgan Stanley, it wouldn't be the first time a team of a small handful he was at the core of beat the socks off everybody else either. Some people just have this amazing ability to see simplicity. I've had the pleasure of being around a few people boss like that.
You have Hadoop/MR and Monet in the OSS group. SysbaseIQ, KDB, Teradata, Vertica, VectorWise, etc. all provide either extended SQL or additional query support to use ordering information in some (often weak) way.
You can even count Oracle In Memory Analytics with TSQL with its cursor model if you really want.
For example for KDB to get the last price and sum of volumer for each second of AAPL you would do in KSQL:
select last px, sum vol by time.second from trade where sym=`AAPL
on it can be done in its lower level language K/Q as (approx, I'm a little rusty) g:group trade.time.second @ where trade.sym=`AAPL
flip`px`vol!(last each trade.px g; sum each trade.vol g)Perhaps you're hinting at the idea that it's too easy to screw this up as a user, for it to be inefficient
The case it does work is that you are ordered on timestamp (for example), the query planner properly figures to use any filtering indexes and sequential indexes, ... magic occurs
Now the job is making an SQL statement that the query planner can actual understand and optimize since SQL lacks so much of the machinery for order-dependent processing (this was a feature when SQL was conceived remember).
Of course, SQL is by far more general purpose than producing ordered stuff. In fact, ordering is a completely non-relational thing that was even criticised in SQL in early days (such as offsets, limits, duplication, etc.). But SQL has learned to shoehorn foreign concepts into the language / platform for a long time, so I'm just curious about a concrete example where SQL window functions fail (and couldn't be fixed) in SQL for you.
There were a couple really, really cool tech talks I've been to. The title alone gives it props :)
"where Rumsfeldism & Kx collide"
https://kx.com/2014/12/07/visualizing-really-big-data/
in case you're curious about one of the other: a polymer hypergrid with an absurd amount of real time updating data:
http://openfin.github.io/fin-hypergrid-polymer-demo/componen...
Agreed.
A few years of writing expressive, powerful, performant SQL in big blocks will really change your mental model of data across _all_ languages.
I highly recommend a deep dive into the relational world, and I feel super grateful for the SQL-focused job I held previously. Brought those lessons back with me to both FP and OO.
It would also be great if windows could be function parameters in Postgres. I like to abstract complicated conditionals and calculations into functions, but it falls down if you're operating over a window.
min(ST_Distance(this.point, foo.point)) over (blah)
That is, for each 'foo', get me the minimum distance to another foo in the current window.The second peeve is to do with filters, e.g.:
count(*) filter (where this.time - foo.time < 5 and type_id = 42) over (blah)
That is, how many events of type 42 are there up to 5 seconds before the current event.In both cases, it's sad not being able to address the current row as well as the row in the window. I'm not saying either form is completely impossible to get working somehow, I just think it'd be a nice feature.
Do note that Oracle has the KEEP clause that might probably solve your issue with aggregate functions only... But it's a rather esoteric, vendor-specific extension.
Anyway, I do agree, what you're describing would be a very nice feature.
https://dveeden.github.io/modern-sql-in-mysql/
(aka "Look, window functions are coming")