When storing a tree in Postgres, should I use a recursive query to find all parents and children, or store...











up vote
0
down vote

favorite












I'm storing a tree of small strings in Postgres. It looks like this:



"Languages"
|
|--- "French"
|
|--- "Verbs"
|--- "Pronunciation"
|--- "German"
|
|--- "Pronunciation"
|--- "Cuisine"
"Music"
|
|--- "Guitar"
|--- "Voice"
|
|--- "Breathing Exercises"
"Reminders"
"Vehicles"
|
|--- "Car"
|
|--- "Road Rules"
|
|--- "Fines"
|--- "Highways"
|--- "Bike"
|
|--- "Repair Guide"


You get the idea. These are tag names; there will be another table full of flashcards/notes each of which is associated with one or more of these tags. One note might be tagged Languages.French.Verbs and Reminders for example, or a note about Queen Elizabeth might be tagged People.Historical and Countries.UK.History (where a . indicates a level in the hierarchy). In my application I want to browse the list of tags and their associated notes like a filesystem (tags as folders, notes as files within them) and see the same note appearing at multiple points because of these multiple tags.



I've been researching Postgres, which I'm not very familiar with (my classes used SQLite), and I can imagine two ways of accomplishing this -- but I'm not sure which is ideal/correct or what the tradeoffs are. Would love some advice about that.



When the user searches for some notes (either by tag name, note title, or text content) I want to show them a list of the results, and the full hierarchy of the tags associated with that note. As in, they search "Elizabeth", and note #1 is tagged People.Historical and Countries.UK.History. If I get the search results by searching in their notes table, and each note stores one parent ID, then how do I efficiently build up the full tag names?



Should I store a parent ID with each tag and do recursive queries? Would that necessarily be a separate recursive query for each leaf-node tag I'm looking up (as in one query for History and its parents and one for Historical)?



Would it be better if I stored two arrays with each tag, one with children and one with parents? That would avoid needing to do complex lookups each time, but adding and removing children would become more complex. If I expect to do 100 lookups for every add or remove, does that make sense, or is this a stupid idea?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm storing a tree of small strings in Postgres. It looks like this:



    "Languages"
    |
    |--- "French"
    |
    |--- "Verbs"
    |--- "Pronunciation"
    |--- "German"
    |
    |--- "Pronunciation"
    |--- "Cuisine"
    "Music"
    |
    |--- "Guitar"
    |--- "Voice"
    |
    |--- "Breathing Exercises"
    "Reminders"
    "Vehicles"
    |
    |--- "Car"
    |
    |--- "Road Rules"
    |
    |--- "Fines"
    |--- "Highways"
    |--- "Bike"
    |
    |--- "Repair Guide"


    You get the idea. These are tag names; there will be another table full of flashcards/notes each of which is associated with one or more of these tags. One note might be tagged Languages.French.Verbs and Reminders for example, or a note about Queen Elizabeth might be tagged People.Historical and Countries.UK.History (where a . indicates a level in the hierarchy). In my application I want to browse the list of tags and their associated notes like a filesystem (tags as folders, notes as files within them) and see the same note appearing at multiple points because of these multiple tags.



    I've been researching Postgres, which I'm not very familiar with (my classes used SQLite), and I can imagine two ways of accomplishing this -- but I'm not sure which is ideal/correct or what the tradeoffs are. Would love some advice about that.



    When the user searches for some notes (either by tag name, note title, or text content) I want to show them a list of the results, and the full hierarchy of the tags associated with that note. As in, they search "Elizabeth", and note #1 is tagged People.Historical and Countries.UK.History. If I get the search results by searching in their notes table, and each note stores one parent ID, then how do I efficiently build up the full tag names?



    Should I store a parent ID with each tag and do recursive queries? Would that necessarily be a separate recursive query for each leaf-node tag I'm looking up (as in one query for History and its parents and one for Historical)?



    Would it be better if I stored two arrays with each tag, one with children and one with parents? That would avoid needing to do complex lookups each time, but adding and removing children would become more complex. If I expect to do 100 lookups for every add or remove, does that make sense, or is this a stupid idea?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm storing a tree of small strings in Postgres. It looks like this:



      "Languages"
      |
      |--- "French"
      |
      |--- "Verbs"
      |--- "Pronunciation"
      |--- "German"
      |
      |--- "Pronunciation"
      |--- "Cuisine"
      "Music"
      |
      |--- "Guitar"
      |--- "Voice"
      |
      |--- "Breathing Exercises"
      "Reminders"
      "Vehicles"
      |
      |--- "Car"
      |
      |--- "Road Rules"
      |
      |--- "Fines"
      |--- "Highways"
      |--- "Bike"
      |
      |--- "Repair Guide"


      You get the idea. These are tag names; there will be another table full of flashcards/notes each of which is associated with one or more of these tags. One note might be tagged Languages.French.Verbs and Reminders for example, or a note about Queen Elizabeth might be tagged People.Historical and Countries.UK.History (where a . indicates a level in the hierarchy). In my application I want to browse the list of tags and their associated notes like a filesystem (tags as folders, notes as files within them) and see the same note appearing at multiple points because of these multiple tags.



      I've been researching Postgres, which I'm not very familiar with (my classes used SQLite), and I can imagine two ways of accomplishing this -- but I'm not sure which is ideal/correct or what the tradeoffs are. Would love some advice about that.



      When the user searches for some notes (either by tag name, note title, or text content) I want to show them a list of the results, and the full hierarchy of the tags associated with that note. As in, they search "Elizabeth", and note #1 is tagged People.Historical and Countries.UK.History. If I get the search results by searching in their notes table, and each note stores one parent ID, then how do I efficiently build up the full tag names?



      Should I store a parent ID with each tag and do recursive queries? Would that necessarily be a separate recursive query for each leaf-node tag I'm looking up (as in one query for History and its parents and one for Historical)?



      Would it be better if I stored two arrays with each tag, one with children and one with parents? That would avoid needing to do complex lookups each time, but adding and removing children would become more complex. If I expect to do 100 lookups for every add or remove, does that make sense, or is this a stupid idea?










      share|improve this question













      I'm storing a tree of small strings in Postgres. It looks like this:



      "Languages"
      |
      |--- "French"
      |
      |--- "Verbs"
      |--- "Pronunciation"
      |--- "German"
      |
      |--- "Pronunciation"
      |--- "Cuisine"
      "Music"
      |
      |--- "Guitar"
      |--- "Voice"
      |
      |--- "Breathing Exercises"
      "Reminders"
      "Vehicles"
      |
      |--- "Car"
      |
      |--- "Road Rules"
      |
      |--- "Fines"
      |--- "Highways"
      |--- "Bike"
      |
      |--- "Repair Guide"


      You get the idea. These are tag names; there will be another table full of flashcards/notes each of which is associated with one or more of these tags. One note might be tagged Languages.French.Verbs and Reminders for example, or a note about Queen Elizabeth might be tagged People.Historical and Countries.UK.History (where a . indicates a level in the hierarchy). In my application I want to browse the list of tags and their associated notes like a filesystem (tags as folders, notes as files within them) and see the same note appearing at multiple points because of these multiple tags.



      I've been researching Postgres, which I'm not very familiar with (my classes used SQLite), and I can imagine two ways of accomplishing this -- but I'm not sure which is ideal/correct or what the tradeoffs are. Would love some advice about that.



      When the user searches for some notes (either by tag name, note title, or text content) I want to show them a list of the results, and the full hierarchy of the tags associated with that note. As in, they search "Elizabeth", and note #1 is tagged People.Historical and Countries.UK.History. If I get the search results by searching in their notes table, and each note stores one parent ID, then how do I efficiently build up the full tag names?



      Should I store a parent ID with each tag and do recursive queries? Would that necessarily be a separate recursive query for each leaf-node tag I'm looking up (as in one query for History and its parents and one for Historical)?



      Would it be better if I stored two arrays with each tag, one with children and one with parents? That would avoid needing to do complex lookups each time, but adding and removing children would become more complex. If I expect to do 100 lookups for every add or remove, does that make sense, or is this a stupid idea?







      database postgresql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 21:49









      GreenTriangle

      1,0102818




      1,0102818





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243748%2fwhen-storing-a-tree-in-postgres-should-i-use-a-recursive-query-to-find-all-pare%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243748%2fwhen-storing-a-tree-in-postgres-should-i-use-a-recursive-query-to-find-all-pare%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation