I have to fetch data in tree structure. My table is having id and parent_tag_id column. If i am passing any id then i have to fetch all the childern of given id.
How to implement to fetch data in tree structure in apache ignite?
The only way of doing this efficiently (and in general, any parent -> child relations) is to use SQL queries. There are number of approach of dealing with hierarchical data with SQL.
Related
I'm trying to create a query in Athena that solves this problem:
I have records that look like this
{'id': 'a', 'children': ['b','c']}
which create a hierarchical structure, like a tree but with indeterminate children.
I have more than one root, that is, more than one element that is not children of anyone.
I want to get the complete structure for one of them. How can I do that using a SQL query? I've seen that recursive queries are not allowed in Athena.
What you want to achieve is called "recursive queries" or "recursive CTEs" (common table expressions). Presto 340 adds experimental support for them, but Athena is based on Presto 0.172 and does not have the feature. Unfortunately, there is no general replacement for the feature.
Without support for the feature in the query engine, you need to pull the parent/child relationships and calculate the result within your app.
I have a hierarchical data. The most common queries will be "get parent branch for node" and "get subtree of node". Updates and inserts are not likely to occur often. I am choosing between nested sets and hierarchyid. As far as I am concerned, search on nested set should be pretty fast on indexed columns, however, I have no clue about internal implementation of hierarchyid. What should I use in order to achieve highest performance possible?
Having used HierarchyID and self-referencing tables in different projects, I'd say HierarchyId wins hands down in terms of ease of querying.
See Querying a Hierarchical Table Using Hierarchy Methods to see how easy it can be with the built-in query methods for HierarchyID.
We receive bulk data from our customers in a spread sheet. I loaded them in a temporary table as of now. I tried to normalize the data and create a parent table and each parent to have 4 or 5 child rows. Is there a way to insert all the parents and their children using queries? I dont want to write an Application to do that
If you're specifically looking to write only queries to perform this task, check out the following article that outlines a way to set up a linked server to an excel doc.
http://www.sql-server-helper.com/tips/read-import-excel-file-p01.aspx
If you get this set up correctly (which can be a pain) it's as easy as querying the data directly.
I have a table MYTYPE in Oracle 10g representing a tree structure, which is something like this:
ID | PARENTID | DETAIL
I would like to select all rows in MYTYPE which are descendants of a particular ID, such that I can create queries elsewhere such as:
SELECT *
FROM MYDETAIL
WHERE MYTYPEID IN [all MYTYPE which are descendants of some ID];
What is a cost-efficient way of building the descendant set, preferably without using PL/SQL?
Oracle didn't support the ANSI hierarchical syntax of using a recursive Subquery Factoring (CTE in SQL Server syntax) until 11g R2, so you have to use Oracle's native CONNECT BY syntax (supported since v2):
SELECT t.*
FROM MYTABLE t
START WITH t.parentid = ?
CONNECT BY PRIOR t.id = t.parentid
Replace the question mark with the parent you want to find the hierarchical data based on.
Reference:
ASK TOM: "connect by"
Managing hierarchical data using ID,ParentID columns in an RDBMS is known as the Adjacency List model. While very easy to implement and maintain (i.e. insert, update, delete), it's expensive to determine lineage (i.e. ancestors and descendants). As other answers already have written, Oracle's CONNECT BY will work, but this is an expensive operation. You may be better off representing your data differently.
For your case, the easiest solution might adding a what's called a Hierarchy Bridge table to your schema and adding a LEVEL column to your original table. The table has columns ID,DescendantID whereby selecting on ID gives all descendant records, and selecting by DescentantID gives all ancestor records. LEVEL is necessary on the base table to order records. In this way you make a tradeoff of expensive updates for cheap reads, which is what your question implies you want.
Other possibilities that involve changing your base data include Nested Set and Materialized Path representations. That offer similar tradeoffs of more expensive writes for much cheaper reads. For a complete list of options, pros and cons, and some implementation notes, see my previous question on the topic.
Oracle can do recursive queries.
Try looking into start with ... connect by, something like this:
Select *
from MYDETAIL
Starting with PARENTID= 1 --or whatever the root ID is
connect by PARENTID = prior ID
http://psoug.org/reference/connectby.html
Here is the details for 'connect by' features in oracle.
http://psoug.org/reference/connectby.html
My data fits a tree form naturally. Therefore, I have a simple SQL table to store the data: {id, parentid, data1, ..., dataN}
I want to be able to "zoom in" on the data and produce a report which summarizes the data found below the current branch.
That is, when standing in the root, I want to have the totals of all the data. When I have traveled down a certain branch of the tree, I want to only have the summation of the data found only for that node and its child nodes.
How do I write such a query in SQL?
Thanks in advance!
/John
Since sqlite does not support CONNECT BY, you will not be able to perform this calculation in a single query unless you use nested sets or materialized paths for your data.
Alternatively, do it "the hard way" and traverse your tree recursively, one query for each child node starting at the parent-of-interest.
Also see:
Managing Hierarchical Data in MySQL
Recursive Hierarchies: The Relational Taboo!
Vlad's reference on nested sets looks pretty good. If you want something that covers trees and hierarchies in more detail then you can also check out Joe Celko's book.
The "ID, ParentID" adjacency list model is really an "old time" way of looking at hierarchies in a relational database model.