Hey everyone,

The bulk of my database consists of a tree of models (called Node), more 
than 200,000 of them, and I very regularly need to make large queries to 
get subtrees which I'm finding is causing a significant slowdown. I'm using 
postgreSQL and have found that recursive queries 
<https://www.postgresql.org/docs/8.4/static/queries-with.html> are exactly 
what I need, but I'd like to use them nicely from within Django. Is there 
an accepted way to do this, or something I could override? Basically I'd 
like to have something similar to `Node.filter()` like 
`Node.getSubtree(root=<id>)` which executes the following:

WITH RECURSIVE nodes(id, label, parent_id) AS (
 SELECT tn.id, tn.label, tn.parent_id FROM assets_asset AS tn WHERE tn.id = 
<id>
  UNION ALL
    SELECT p.id, p.label, p.parent_id 
 FROM nodes as c, assets_asset AS p 
 WHERE c.parent_id = p.id
)
SELECT * FROM nodes AS n ORDER BY n.id ASC;


I'd appreciate any advice on how to go about this. I'm not afraid to dive 
into the source, I'm just not sure where to look.

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/aee479c2-c052-48b7-96b3-71d1c7029312%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to