Skip to content

Instantly share code, notes, and snippets.

@MrSimsek
Last active August 6, 2017 18:29
Show Gist options
  • Save MrSimsek/c6c0e8c3112c8a6e140e25ca78ae5513 to your computer and use it in GitHub Desktop.
Save MrSimsek/c6c0e8c3112c8a6e140e25ca78ae5513 to your computer and use it in GitHub Desktop.
Genre Database Optimization
# For databases that each item might have more than one tags like genres.
# It is not an optimal solution to append appropriate genres to each items seperately. For example; when we search for adventure novels,
# the database nelow will firts loop through each book and then each genre it contains which will give a O(N^2).
{
"novels" : {
"The Lord of The Rings" : {
"author" : "JRR Tolkien",
"genre" : ["Fantasy", "Adventure"]
},
"The Godfather" : {
"author" : "Marco Puzo",
"genre" : ["Crime", "Thriller"]
}
"Corleone Family" : {
"author" : "Ed Falco",
"genre" : ["Crime", "Thriller"]
}
"Childhood's End" : {
"author" : "Arthur C. Clarke",
"genres" : "Science Fiction", "Adventure"
}
}
}
# The optimal approach for this situation is assigning a seperate parent "genre"
# property that contains genres as keys and books as values like below format.
# This will give a much faster search result with O(N).
{
"novels" : {
"The Lord of The Rings" : {
"author" : "JRR Tolkien",
},
"The Godfather" : {
"author" : "Marco Puzo",
}
"Corleone Family" : {
"author" : "Ed Falco",
}
"Childhood's End" : {
"author" : "Arthur C. Clarke",
}
}
"genres" : {
"Fantasy" : ["The Lord of The Rings"],
"Adventure" : ["The Lord of The Rings", "Childhood's End"],
"Crime" : ["The Godfather", "Corleone Family"],
"Thriller" : ["The Godfather","Corleone Family"],
"Science Fiction" : ["Childhood's End"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment