Efficient data access for easy-to-write complex transformations

  • Thread starter Deleted member 43669
  • Start date
Status
Not open for further replies.
D

Deleted member 43669

Guest
Let's say I have a small relational database (largest table ~100.000 rows) and I want to be people to write very simple scripts which will allow to "extract" objects from the information in the database in non trivial ways, "dot-walking" ways. I would like them to say stuff such as:

Code:
for each row foo in table foos where foos.x = 'b'
  create a new bar object with fields:
    bar.x = foo.a
    bar.y = foo.b
    bar.z = foo.c.d.e
    bar.t = if foo.d.a > 3 then 4 else 1

where foo.c.d.e means access through foreign key relationships.

I was thinking having people write SQL views that look like the output objects (which are basically plain-key value), and write denormalizing views to simplify the dot walking stuff but a) people tend to be allergic to SQL and b) some SQL views would be very ugly and unmaintanable with lots of CASE, COALESCE, LEFT JOINs, etc.

One alternative would be to use say, Python, use an ORM and the rules would be plain Python code, but I'm concerned that even though the dataset is small, we would be burnt by thousands of N+1 queries problems.

Is there something that would me let me do ORM-like access on in-memory data with minimal per-query cost which would allow quick loading of the data from the database into memory? I think LINQ does it, but we are most familiar with Python...
 
D

Deleted member 43669

Guest
Thinking more in depth about this, I think I want an ORM which:

1) Can load an entire database into memory and perform queries with 0 overhead. I'm thinking that maybe SQLite will have a very low low overhead and nearly eliminate the n+1 problem completely, but I need to run some benchmarks

2) Optionally, require no explicit model definition and infer everything from the schema

...

[url=http:/civis/viewtopic.php?p=31922659#p31922659:zyhowmbt said:
Bastard[/url]":zyhowmbt]Take a look at F# type providers, they will let you interactively "walk" the data as it were.

Hmmm, .NET is not much of an option right now, but I'm guessing there is a type provider which effectively gives you a full ORM without defining models. Are there any in-process .NET embedded databases?

[url=http:/civis/viewtopic.php?p=31923777#p31923777:zyhowmbt said:
Fruity Pebbles[/url]":zyhowmbt]Have you looked at Django? In my experience (ableit brief) Django's ORM tends to produce fairly efficient SQL queries.

We actually used Django for the previous version of this software, and n+1 queries (amongst other things) killed us. I know you can select_related, but it's a pain writing it everywhere and in some cases you can still be screwed by making too many queries. I do agree that, like most ORMs, most queries it performs are not terrible (because it's queries match SQL pretty closely).
 
D

Deleted member 43669

Guest
[url=http:/civis/viewtopic.php?p=31925089#p31925089:et3a2ass said:
Faramir[/url]":et3a2ass]I don't think it is an ORM's job to force the DBM to cache an entire database in RAM.

Sorry, that's not what I meant- I meant something that loads an entire database as data structures and then allows you to work on that data on RAM with its internal representation- not the RDBMS'. Also it ideally allows me to access related entities via foreign keys just like an ORM does.
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
[url=http:/civis/viewtopic.php?p=31925089#p31925089:120slaja said:
Faramir[/url]":120slaja]I don't think it is an ORM's job to force the DBM to cache an entire database in RAM.

Sorry, that's not what I meant- I meant something that loads an entire database as data structures and then allows you to work on that data on RAM with its internal representation- not the RDBMS'. Also it ideally allows me to access related entities via foreign keys just like an ORM does.
Okay, got it. Seems like the kind of thing that would be relatively easy to do given that you want to do it once with a known database. Whereas if you wanted to make a tool to do in all kinds of situations, not only would that tool be hard to make, but it wouldn't be as easy for the end users to use as the custom one.

Do you have a lot of different databases you want to apply this against?
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Look at LINQPad

Back it up with a local clone of the database via something like sql server express (refresh it however you like) and hey presto, dot walking, reasonably simple syntax (IMO) and if yuou pay for it (dirt cheap) intellisense.

Has full sql support too (and will do f# if you like)

It can function against non MS databases
 
D

Deleted member 43669

Guest
[url=http:/civis/viewtopic.php?p=31927237#p31927237:1b746l5z said:
zelmak[/url]":1b746l5z]Sounds like you don't really need a database at all... ?

That much is true. But designing lots of data structures, loading data in them and allowing reasonable querying/related entities traversal seems... tedious. Databases and ORMs do this for me, but they tend to incur small overhead for each query which in my case could be problematic.

[url=http:/civis/viewtopic.php?p=31927271#p31927271:1b746l5z said:
Faramir[/url]":1b746l5z]
[url=http:/civis/viewtopic.php?p=31925089#p31925089:1b746l5z said:
Faramir[/url]":1b746l5z]I don't think it is an ORM's job to force the DBM to cache an entire database in RAM.

Sorry, that's not what I meant- I meant something that loads an entire database as data structures and then allows you to work on that data on RAM with its internal representation- not the RDBMS'. Also it ideally allows me to access related entities via foreign keys just like an ORM does.
Okay, got it. Seems like the kind of thing that would be relatively easy to do given that you want to do it once with a known database. Whereas if you wanted to make a tool to do in all kinds of situations, not only would that tool be hard to make, but it wouldn't be as easy for the end users to use as the custom one.

Do you have a lot of different databases you want to apply this against?

No, it's a single database. Although it would be really great if the code required to add new tables was minimal, of course
 

Alamout

Ars Legatus Legionis
27,285
[url=http:/civis/viewtopic.php?p=31924399#p31924399:f5icz616 said:
koala[/url]":f5icz616]Well, with 100k rows on a simple SQLite database, I can perform 100k queries using Django in ~45s, which works out to 2275 queries/s, which for my purposes would probably be enough.
I don't know if it would make a difference, but you could instantiate a SQLite database in memory (i.e. con = sqlite3.connect(":memory:") ) and load the data into that. Loading time should be quick and might amortize over faster queries, although I wouldn't be surprised if the sqlite3 package was smart enough to get things right on its own.
 
Status
Not open for further replies.