r/AskComputerScience • u/Bethelyhills • Dec 06 '19
ELI5: What makes SQL faster than just reading through a CSV file using a language like Python or Java?
3
u/fragmede Dec 06 '19
Since this is AskCS, the ELIamaFreshman answer is binary trees. It takes O(N), worst case, to find something reading a CSV linearly. However, read it into memory in a balanced red-black tree, and worst case becomes O(log N). Do a ton of work around that concept, save those trees to disk (as indexes) and start selling database software.
Julia Evans has a great comic about the SQL side of things too
2
87
u/SS7Junkie Dec 06 '19 edited Dec 06 '19
SQL is how you ask for information. CSV is one way of representing a chunk of info. I’m going to restate your question to make it apples to apples.
What makes querying a relational database faster than querying a CSV?”
ELI5: Information in a CSV is like putting a room full of people in a straight line and going one by one to find people with red shirts.
Information in a relational database is like putting a room full of people in groups based on their short color and tying strings from each person to other people with the same hair color and to other people of the same eye color. When you want all red shirts with blue eyes you just go to the red shirt group and follow the strings. Then you don’t have to waste time with the people with blue shirts or orange shirts.
Edit: I should add that sometimes you may not want to go through the effort of running string everywhere. Maybe if the crowd is small and you want something simple it’s worth just lining them up and going one by one.
Sometimes CSV is the best choice.
5
u/WardenUnleashed Dec 06 '19
Dont forget about the caching aspects as well!
If you’ve already asked me to find all the people with red shirts and blue eyes and I know no one has walked in or out of the room(or changed shirts), I don’t have to figure it out again because I already know the answer!
→ More replies (1)3
u/de_rats_2004_crzy Dec 06 '19
Dumb question but your clarification at the start makes me ask ... can you use SQL to query a CSV file?? Is the answer basically “in theory yes but in practice no”?
→ More replies (2)2
u/deong Dec 06 '19
In his clarification, he mentioned “relational database”. Think of that like a word document or an Excel file. A Word doc is something you create with your data, but you need a special piece of software to read or edit it — in this case, Microsoft Word. A database is similarly a thing that is created to contain and organize your data and that requires a special piece of software to read and edit — a database engine like Microsoft SQL Server 2017 or Oracle. Those programs also store your data in special formats designed to specifically enable certain types of queries and updates to be much faster (and generally lots of other features as well).
Those products are what take SQL queries and efficiently find the data you want. Other people have written software that can take SQL and query CSV instead, but since they just have the CSV to use to access the data instead of the specially optimized formats, you don’t get the faster reads and other features.
2
6
Dec 06 '19
It's not the language itself but the implementation that is responsible for the speed. Depending on what you are doing with that CSV file, particularly if it is simple, you could do better without significant effort. But using libraries to convert a CSV to SQL database which you could then query using SQL might be an easier task because your implementation will be using higher-level tools.
2
u/unfixpoint Dec 06 '19
Imagine you have a book with all the people in the world and their birthdays in it in any order (ie. CSV). Now you want to find out your friend's birthday (say Alice), you will have to look through every single entry and check if it's your friend.
But if that book would be sorted by the names in alphabetical number (SQL), you could quickly find her. Because Alice starts with an A you can stop searching when the names with B come.
Depending on the five year old you could change the name such that it starts with another letter, or suggest more complicated/quicker ways of searching.
2
u/DukeBerith Dec 06 '19
If you can store your csv in a similar way to sql, it'll be just as fast but will take more code to do the same thing.
For example you feed in all the data row by row, but indexes will exist in the form of hash maps, you'll have to write your own methods and heuristics on when you decide to use hash join vs nested loops.
You'll also probably need to keep it all in memory or write your own paging algorithm to minimise disk i/o which is what can make queries run slow.
Behind the scenes, this is all sql engines are doing. They have optimisers that look at your queries and decide when to clean stuff up for you, when to use an index or not, determine the best way to sort your data.
5
u/fauxpasgrapher Dec 06 '19
I'm thinking it's indexes and partitioning.
6
u/unfixpoint Dec 06 '19
Got a pretty smart five year old there. I didn't know about indices and partitioning when I was five years old, haha
3
5
u/shotdown_by_maria Dec 06 '19
Here's a pretty good answer on Stack Overflow, although maybe not ELI5.
Basically the difference is that SQL can be indexed, like a dictionary or an old-fashioned library card catalog, which allows for fast lookups. In contrast, CSV files need to be read and parsed line-by-line.