Pandas加载CSV的速度比SQL快 [英] Pandas is faster to load CSV than SQL

查看:730
本文介绍了Pandas加载CSV的速度比SQL快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎从CSV加载数据的速度比使用Pandas从SQL(Postgre SQL)加载数据快。 (我有SSD)

It seems that loading data from a CSV is faster than from SQL (Postgre SQL) with Pandas. (I have a SSD)

这是我的测试代码:

import pandas as pd
import numpy as np

start = time.time()
df = pd.read_csv('foo.csv')
df *= 3
duration = time.time() - start
print('{0}s'.format(duration))

engine = create_engine('postgresql://user:password@host:port/schema')
start = time.time()
df = pd.read_sql_query("select * from mytable", engine)
df *= 3
duration = time.time() - start
print('{0}s'.format(duration))

foo.csv和数据库是相同的(两者中的数据和列相同,4列,100000行充满随机int)。

The foo.csv and the database are the same (same amount of data and columns in both, 4 columns, 100 000 rows full of random int).

CSV需要0.05s

CSV takes 0.05s

SQL需要0.5s

SQL takes 0.5s

您认为这正常吗CSV比SQL快10倍吗?我想知道我是否在这里丢失了东西……

Do you think it's normal that CSV is 10 time faster than SQL ? I'm wondering if I'm missing something here...

推荐答案

这是正常现象,读取csv文件是始终是简单加载数据的最快方法之一

This is a normal behavior, reading a csv file is always one of the quickest way to simply load data

CSV非常幼稚和简单。直接从中加载将非常快。对于具有复杂结构的海量数据库,不能选择CSV。 SQL超级快地从表中选择数据,然后将该数据返回给您。自然,如果您可以选择,修改和处理数据,则这将增加通话的间接费用。

A CSV is very naive and simple. loading directly from it will be very quick. For massive database with complex structure CSV is not an option. SQL is super fast to select data from table an return that data to you. naturally, if you can select, modify and manipulate data it will add an overhead time cost to your call.

想象一下,您在csv中有一个时间序列从1920年到2017年,使用的是csv,但您只想获取2010年至今的数据。

csv方法是要加载整个csv,然后选择2010年到2017年。

csv approach would be to load the entire csv then select the years 2010 to 2017.

SQL方法是通过SQL选择功能预先选择年份

SQL approach would be to pre-select the years via SQL select function

在这种情况下,SQL会更快。

In that scenario, SQL would be MUCH faster.

这篇关于Pandas加载CSV的速度比SQL快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆