SQL Queries

What is the correct way to query data from an SQL Database?

Usually structured data is required in a program. To query a hierarchy of objects would require a loop of queries for every level of the data structure. But queries are slow.
The native solution to this are joins. But joins will denormalize the data.

+---------------------+----------------------------------+-------------------------------+----------+--------+
| author | title | LEFT(quote, 25) | position | tag |
+---------------------+----------------------------------+-------------------------------+----------+--------+
| Lewis Carroll | Alice's Adventures in Wonderland | In a minute or two the Ca | p.42 | animal |
| Lewis Carroll | Alice's Adventures in Wonderland | “All right,” said the Cat | p.56 | animal |
| Lewis Carroll | Alice's Adventures in Wonderland | “All right,” said the Cat | p.56 | grin |
| Richard M. Stallman | Free Software, Free Society | A hacker is someone who e | p.96 | hacker |
+---------------------+----------------------------------+-------------------------------+----------+--------+

A transformation to the hierarchical data structure would again require the previously mentioned loops.

>So what is the best way to query SQL data?
>Would it be asked to much if SQL databases would return structured data instead of denormalized tables?

Attached: pancake-final_2x_1x.png (400x300, 121K)

Other urls found in this thread:

en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL
academy.vertabelo.com/blog/do-it-in-sql-recursive-tree-traversal/
en.wikipedia.org/wiki/Nested_set_model
twitter.com/NSFWRedditImage

>The native solution to this are joins. But joins will denormalize the data.
Since the joined data is only temporarily used to display the data, that is ok.

tl;dr just fucking use mongodb

instead of loops use subquerries

return arrays if you have to

postresql supports arrays and can also return json

>Since the joined data is only temporarily used to display the data, that is ok.
My question was not targeting at a problem in the database. My point is, that a denormalized table is chunky and hard to process in a program.

>postresql supports arrays and can also return json
I did not know that, that is a pretty good thing.

>My question was not targeting at a problem in the database. My point is, that a denormalized table is chunky and hard to process in a program.
That's why you do filtering etc in the database.

>>My question was not targeting at a problem in the database. My point is, that a denormalized table is chunky and hard to process in a program.
>That's why you do filtering etc in the database.
yes but then you have to do a lot of queries to load all the data. This is actually the point of this thread.

how would you load the data of the given example in OP?

Unfortunately it is not entirely clear to me what OP wants to query for. OP says something about hierachy, but his table doesn't indicate it.

>yes but then you have to do a lot of queries
a single query with subqueries
> to load all the data.
the database optimizes the query to minimize data loading.
>This is actually the point of this thread.
this makes this thread pointless

SQL as a language is based on an algebra
this means that all operations are completely defined in their behavior and how they relate to each other
the database engine then can safely rearrange the query and drop meaningless parts.

>hierarchical data structure
en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL

>this makes this thread pointless

no this thread helps me a lot by finding out about subqueries

In the end I need a structure like this:

[
{
"author": "Lewis Carrol",
"books": [
{
"title": "Alice's Adventures in Wonderland",
"quotes": [
{
"quote": "In a minute or two the Ca…",
"position": "p.42",
"tags": ["animal"]
},
{
"quote": "“All right,” said the Cat…",
"position": "p.56",
"tags": ["animal", "grin"]
}
]
}
]
},
{
"author": "Richard M. Stallman",
"books": [
{
"title": "Free Software, Free Society",
"quotes": [
{
"quote": "A hacker is someone who e…",
"position": "p.96",
"tags": ["hacker"]
}
]
}
]
}
]


or anything similar as nested arrays or nested objects, or whatever

Just use an ORM

>Just use an ORM
Is that really the right thing to do? Every time I looked into an ORM I was not sure if it would do in the end what I wanted it to do. I also always feared the projects would get orphaned after a while.

Do you think it would be possible to maintain a ORM myself when push comes to shove?

Use neo4j
It's a graph database
It can handle those more complex structures with a different syntax
The syntax more resembles pattern matching

if you fucking think you can find new and unsolved problems in 40 year old technology, this is the wrong field for you. Fucking JS Hipsters, brocoders and other scum.
Learn your fucking craft.

academy.vertabelo.com/blog/do-it-in-sql-recursive-tree-traversal/

Attached: 1412114296003.gif (243x199, 2.19M)

Map reduce

Install Postgres.
Also, if you want to be REALLY autistic, you could write a custom function in C; also Postgres.

Attached: 1520141234372.png (720x960, 1.05M)

why are you reasoning like a high school age student

en.wikipedia.org/wiki/Nested_set_model

what is a hierarchy of objects? is that they same thing as seperate tables? RDBMS have chains of related columns between tables.Joins work in such systems when you are after data in specific columns on specific tables. What am I missing?

Does OP know what a blob is? Structured and unstructured data can reside in blobs.

KYS

>A hacker is someone who eats foot cheese and smells really bad.

>if you fucking think you can find new and unsolved problems in 40 year old technology, this is the wrong field for you. Fucking JS Hipsters, brocoders and other scum.

I created this thread to find out about the solution. I actually did not study informatics or computer science but physics and engineering, and now I came here to ask you how it is done properly.
But you might be right. Possibly a lot of Onions Boys do not know about it, and that might be the reason why I was not able to google it so far.