将大数据导出为CSV文件

Kev*_*vin 1 postgresql coldfusion coldfusion-9 postgresql-8.4

我当前的任务要求我从一个非常大的数据库中导出大约100,000行数据.

我非常擅长处理大数据,我很想听听过去曾经历过这些问题的人们的一些最佳实践和指导方针,这些问题过去是为了使这个帖子非主观.

更多细节:

  • 数据库完全没有标准化(非常难看)

  • 我必须处理至少100,000行

  • 该任务在午夜运行,用户较少

  • 目前使用ColdFusion 9,PostgreSQL 8.4

谢谢!

这是应用Craig解决方案后我的代码的样子:

<cfset base_path = GetDirectoryFromPath(ExpandPath("*.*")) & "some_parent\some_child\">

<cfif not DirectoryExists(base_path)>
    <cfdirectory directory="#base_path#" action="create" mode="777">
</cfif>

<cfset this_batch_path = DateFormat(Now(), 'mmddyyyy') & TimeFormat(Now(), 'hhmmss') & "\">
<cfdirectory directory="#base_path##this_batch_path#" action="create" mode="777">

<cfset this_filename = "someprefix_" & DateFormat(Now(), 'yyyymmdd') & ".csv">
<cffile action="write" file="#base_path##this_batch_path##this_filename#" output="">

<cfset escaped_copy_path = ListChangeDelims(base_path & this_batch_path & this_filename, "\\", "\")>

<cfquery name="qMyQuery" datasource="some_db" username="some_uname" password="some_pword" result="something">
    COPY some_table TO '#escaped_copy_path#' WITH CSV HEADER;
</cfquery>
Run Code Online (Sandbox Code Playgroud)

现在我需要获取复制行的计数.在PGSQL 8.4文档中:

输出

成功完成后,COPY命令将返回表单的命令标记

COPY计数

计数是复制的行数.

但即使使用结果标记和查询本身,我似乎无法使其工作.

Cra*_*ger 5

100,000行并不大,除非这些行非常宽,有很多大值.

只需使用psql\copy (SELECT ...) TO '/some/local/file' WITH (FORMAT CSV, HEADER)

如果您愿意,可以估算数据大小:

select pg_size_pretty(sum( octet_length(t::text) )) FROM mytable t WHERE ...;
Run Code Online (Sandbox Code Playgroud)

对于实际的大数据提取运行,有时您可能想要使用Talend Studio,Pentaho Kettle或CloverETL等ETL工具.

顺便说一下,现在是时候开始考虑从8.4升级,因为它现在已经过时了.