如何使用mysqlimport插入压缩数据?

2024-09-30 01:30:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用mysqlimport(和python)导入一堆记录。为了节省空间,我想对每个列使用gzip压缩。mysqlimport支持这个吗?在

现在我使用StringIO压缩每个列,然后使用mysqlimport转储行。有没有更有效的方法?据我所知,StringIO增加了不必要的开销:

# compress each of the columns
for col in d.values():
    stringio = StringIO.StringIO()
    gzip_file = gzip.GzipFile(fileobj=stringio, mode='w')
    gzip_file.write(col)
    gzip_file.close()



#  mysql import script
fh = '/path/to/.sql'
command = ['mysqlimport',
           '--fields-terminated-by=,', 
           '--fields-optionally-enclosed-by="', '-i', '-C',
           '--columns=%s' % ','.join(d.keys()), '--local', fh]
FNULL = open(os.devnull, 'w')
p = subprocess.Popen(command, stdout=FNULL, stderr=subprocess.PIPE)
_, err = p.communicate()

Tags: columnsfieldsby记录colcommandfilesubprocess

热门问题