Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance issue when getting descendants. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Gee-Bee
Copy link

@Gee-Bee Gee-Bee commented Apr 29, 2015

When building tree, database is hitted for each node, looking up its parent node. The culprit is here:

if(cache[item.parent])
cache[item.parent][item] = values
.
My branch fixes this issue.

This is example output before:

pry(main)> PWD.nodes_and_descendants(to_depth: 2)                                                                                                                [88/1827]
  PWD Load (3.3ms)  SELECT "pwds".* FROM (WITH RECURSIVE crumbs AS (
          SELECT pwds.*, 0 AS depth FROM pwds WHERE parent_id IS NULL
          UNION ALL
          SELECT alias1.*, crumbs.depth + 1 FROM crumbs JOIN pwds alias1 on alias1.parent_id = crumbs.id
          WHERE crumbs.depth + 1 < 2
        ) SELECT * FROM crumbs) as pwds WHERE (pwds.depth >= 0) AND (pwds.depth + 1 < 4)
  PWD Load (0.4ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.4ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.4ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.6ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.2ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.2ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.4ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.9ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.4ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.5ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.3ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.2ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]
  PWD Load (0.2ms)  SELECT  "pwds".* FROM "pwds" WHERE "pwds"."id" = $1 LIMIT 1  [["id", 3]]

and after:

pry(main)> PWD.nodes_and_descendants(to_depth: 2)
  PWD Load (1.8ms)  SELECT "pwds".* FROM (WITH RECURSIVE crumbs AS (
          SELECT pwds.*, 0 AS depth FROM pwds WHERE parent_id IS NULL
          UNION ALL
          SELECT alias1.*, crumbs.depth + 1 FROM crumbs JOIN pwds alias1 on alias1.parent_id = crumbs.id
          WHERE crumbs.depth + 1 < 2
        ) SELECT * FROM crumbs) as pwds WHERE (pwds.depth >= 0) AND (pwds.depth + 1 < 4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant