- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在从表中提取数据库记录
,并且希望将它们写入
到文件
中。我遇到的情况是我的表中有数十亿行。因此,当内存不足时,我无法一次性加载所有记录并写入它们。所以我一次向文件写入一条记录。我当前的代码是:
from impala.dbapi import connect
connection = connect()
cur = connection.cursor()
sql = "<SQL Statement>"
cur.execute(sql)
write_file = open("file1.csv",'w')
write_file.write("<column header>")
row = cur.fetchone()
while row is not None:
write_file.write("\n" + str(row)[1:-1])
row = cur.fetchone()
write_file.close()
这样做会花费大量时间,因为一次要顺序处理那么多记录。我想尝试的是一次写入一个记录 block (例如一次写入 100 万条记录)。如何一次写入一个记录 block
而不是一次写入一条记录?
注意:我使用的数据库是impala
更新: 这是执行上述代码的分析结果
ncalls tottime percall cumtime percall filename:lineno(function)
148032 0.162 0.000 0.563 0.000 TBinaryProtocol.py:109(writeI32)
49344 0.077 0.000 0.261 0.000 TBinaryProtocol.py:121(writeString)
49344 0.256 0.000 146.854 0.003 TBinaryProtocol.py:125(readMessageBegin)
49344 0.011 0.000 0.011 0.000 TBinaryProtocol.py:145(readMessageEnd)
1 0.000 0.000 0.000 0.000 TBinaryProtocol.py:20(<module>)
148032 0.257 0.000 146.350 0.001 TBinaryProtocol.py:205(readI32)
49344 0.084 0.000 0.549 0.000 TBinaryProtocol.py:220(readString)
1 0.000 0.000 0.000 0.000 TBinaryProtocol.py:226(TBinaryProtocolFactory)
1 0.000 0.000 0.000 0.000 TBinaryProtocol.py:236(TBinaryProtocolAccelerated)
1 0.000 0.000 0.000 0.000 TBinaryProtocol.py:24(TBinaryProtocol)
1 0.000 0.000 0.000 0.000 TBinaryProtocol.py:258(TBinaryProtocolAcceleratedFactory)
1 0.000 0.000 0.000 0.000 TBinaryProtocol.py:39(__init__)
49344 0.134 0.000 0.828 0.000 TBinaryProtocol.py:44(writeMessageBegin)
49344 0.010 0.000 0.010 0.000 TBinaryProtocol.py:54(writeMessageEnd)
1 0.000 0.000 0.000 0.000 TCLIService.py:2364(GetOperationStatus_args)
155 0.000 0.000 0.000 0.000 TCLIService.py:2375(__init__)
155 0.001 0.000 0.002 0.000 TCLIService.py:2398(write)
1 0.000 0.000 0.000 0.000 TCLIService.py:2425(GetOperationStatus_result)
155 0.000 0.000 0.000 0.000 TCLIService.py:2435(__init__)
155 0.001 0.000 0.002 0.000 TCLIService.py:2438(read)
1 0.000 0.000 0.000 0.000 TCLIService.py:2485(CancelOperation_args)
1 0.000 0.000 0.000 0.000 TCLIService.py:2546(CancelOperation_result)
1 0.000 0.000 2.112 2.112 TCLIService.py:259(ExecuteStatement)
1 0.000 0.000 2.112 2.112 TCLIService.py:275(recv_ExecuteStatement)
1 0.000 0.000 0.000 0.000 TCLIService.py:2761(write)
1 0.000 0.000 0.000 0.000 TCLIService.py:2788(GetResultSetMetadata_result)
1 0.000 0.000 0.000 0.000 TCLIService.py:2798(__init__)
1 0.000 0.000 0.000 0.000 TCLIService.py:2801(read)
1 0.000 0.000 0.000 0.000 TCLIService.py:2848(FetchResults_args)
49186 0.030 0.000 0.030 0.000 TCLIService.py:2859(__init__)
49186 0.171 0.000 0.418 0.000 TCLIService.py:2882(write)
1 0.000 0.000 0.000 0.000 TCLIService.py:2909(FetchResults_result)
49186 0.047 0.000 0.047 0.000 TCLIService.py:2919(__init__)
49186 0.195 0.000 93.752 0.002 TCLIService.py:2922(read)
155 0.001 0.000 0.114 0.001 TCLIService.py:499(GetOperationStatus)
155 0.001 0.000 0.012 0.000 TCLIService.py:507(send_GetOperationStatus)
155 0.001 0.000 0.101 0.001 TCLIService.py:515(recv_GetOperationStatus)
1 0.000 0.000 0.002 0.002 TCLIService.py:589(GetResultSetMetadata)
1 0.000 0.000 0.000 0.000 TCLIService.py:597(send_GetResultSetMetadata)
1 0.000 0.000 0.002 0.002 TCLIService.py:605(recv_GetResultSetMetadata)
49186 0.145 0.000 241.626 0.005 TCLIService.py:619(FetchResults)
49186 0.276 0.000 2.709 0.000 TCLIService.py:627(send_FetchResults)
49186 0.318 0.000 238.771 0.005 TCLIService.py:635(recv_FetchResults)
1 0.000 0.000 0.000 0.000 TCLIService.py:770(Processor)
1 0.001 0.001 0.001 0.001 TCLIService.py:9(<module>)
1 0.000 0.000 0.000 0.000 TProtocol.py:20(<module>)
1 0.000 0.000 0.000 0.000 TProtocol.py:23(TProtocolException)
1 0.000 0.000 0.000 0.000 TProtocol.py:39(TProtocolBase)
1 0.000 0.000 0.000 0.000 TProtocol.py:419(TProtocolFactory)
1 0.000 0.000 0.000 0.000 TProtocol.py:42(__init__)
2245378 4.023 0.000 190.070 0.000 TSocket.py:103(read)
49344 0.153 0.000 0.925 0.000 TSocket.py:123(write)
49344 0.012 0.000 0.012 0.000 TSocket.py:137(flush)
1 0.000 0.000 0.000 0.000 TSocket.py:141(TServerSocket)
1 0.000 0.000 0.001 0.001 TSocket.py:20(<module>)
1 0.000 0.000 0.000 0.000 TSocket.py:28(TSocketBase)
1 0.000 0.000 0.000 0.000 TSocket.py:29(_resolveAddr)
1 0.000 0.000 0.000 0.000 TSocket.py:47(TSocket)
1 0.000 0.000 0.000 0.000 TSocket.py:50(__init__)
49344 0.022 0.000 0.022 0.000 TSocket.py:69(isOpen)
1 0.000 0.000 0.000 0.000 TSocket.py:72(setTimeout)
1 0.000 0.000 0.000 0.000 TSocket.py:81(open)
1 0.000 0.000 0.000 0.000 TTransport.py:103(TServerTransportBase)
1 0.000 0.000 0.000 0.000 TTransport.py:116(TTransportFactoryBase)
1 0.000 0.000 0.000 0.000 TTransport.py:123(TBufferedTransportFactory)
1 0.000 0.000 0.000 0.000 TTransport.py:131(TBufferedTransport)
1 0.000 0.000 0.000 0.000 TTransport.py:139(__init__)
49344 0.057 0.000 0.079 0.000 TTransport.py:145(isOpen)
1 0.000 0.000 0.000 0.000 TTransport.py:148(open)
197376 0.473 0.000 145.718 0.001 TTransport.py:154(read)
246720 0.177 0.000 0.376 0.000 TTransport.py:162(write)
49344 0.177 0.000 1.156 0.000 TTransport.py:165(flush)
49344 0.015 0.000 0.015 0.000 TTransport.py:173(cstringio_buf)
2196034 8.376 0.000 57.806 0.000 TTransport.py:177(cstringio_refill)
197376 0.389 0.000 146.149 0.001 TTransport.py:54(readAll)
46 0.000 0.000 0.000 0.000 abc.py:89(<genexpr>)
34 0.000 0.000 0.000 0.000 collections.py:329(<genexpr>)
4 0.000 0.000 0.000 0.000 collections.py:353(<genexpr>)
4 0.000 0.000 0.000 0.000 collections.py:355(<genexpr>)
4 0.000 0.000 0.000 0.000 collections.py:38(__init__)
1 0.000 0.000 0.000 0.000 collections.py:387(Counter)
44 0.000 0.000 0.000 0.000 collections.py:54(__setitem__)
28 0.000 0.000 0.000 0.000 collections.py:73(__iter__)
3 0.000 0.000 0.000 0.000 decimal.py:3782(__init__)
27 0.000 0.000 0.000 0.000 decimal.py:3809(<genexpr>)
27 0.000 0.000 0.000 0.000 decimal.py:3816(<genexpr>)
49186 0.018 0.000 0.018 0.000 hiveserver2.py:113(buffersize)
49344 3.269 0.000 1590.514 0.032 hiveserver2.py:116(wrapper)
100680255 29.571 0.000 29.571 0.000 hiveserver2.py:122(has_result_set)
1 0.000 0.000 39.283 39.283 hiveserver2.py:140(execute)
1 0.000 0.000 2.112 2.112 hiveserver2.py:142(op)
1 0.000 0.000 0.000 0.000 hiveserver2.py:146(_get_socket)
2 0.004 0.002 0.029 0.014 hiveserver2.py:15(<module>)
1 0.000 0.000 39.283 39.283 hiveserver2.py:154(_execute_sync)
1 0.000 0.000 0.000 0.000 hiveserver2.py:158(_get_transport)
1 0.000 0.000 0.000 0.000 hiveserver2.py:166(_reset_state)
1 0.002 0.002 37.169 37.169 hiveserver2.py:175(_wait_to_finish)
1 0.000 0.000 0.000 0.000 hiveserver2.py:187(connect_to_impala)
154 0.000 0.000 0.000 0.000 hiveserver2.py:189(_get_sleep_interval)
50340127 50.937 0.000 1760.004 0.000 hiveserver2.py:207(fetchone)
1 0.000 0.000 0.001 0.001 hiveserver2.py:209(open_session)
1 0.000 0.000 2.112 2.112 hiveserver2.py:226(execute_statement)
1 0.000 0.000 0.002 0.002 hiveserver2.py:237(get_result_schema)
1 0.000 0.000 0.000 0.000 hiveserver2.py:24(HiveServer2Connection)
50340127 52.496 0.000 1693.039 0.000 hiveserver2.py:250(next)
49186 1153.582 0.023 1584.908 0.032 hiveserver2.py:263(fetch_results)
1 0.000 0.000 0.000 0.000 hiveserver2.py:30(__init__)
155 0.002 0.000 0.115 0.001 hiveserver2.py:406(get_operation_status)
1 0.000 0.000 0.001 0.001 hiveserver2.py:49(cursor)
1 0.000 0.000 0.000 0.000 hiveserver2.py:66(HiveServer2Cursor)
1 0.000 0.000 0.000 0.000 hiveserver2.py:71(__init__)
49344 0.039 0.000 0.039 0.000 hiveserver2.py:76(err_if_rpc_not_ok)
49186 0.017 0.000 0.017 0.000 hiveserver2.py:89(description)
391 0.000 0.000 0.000 0.000 sre_parse.py:130(__getitem__)
42 0.000 0.000 0.000 0.000 sre_parse.py:134(__setitem__)
145 0.000 0.000 0.000 0.000 sre_parse.py:138(append)
135/54 0.000 0.000 0.000 0.000 sre_parse.py:140(getwidth)
12 0.000 0.000 0.000 0.000 sre_parse.py:178(__init__)
1116 0.001 0.000 0.001 0.000 sre_parse.py:182(__next)
308 0.000 0.000 0.000 0.000 sre_parse.py:195(match)
983 0.000 0.000 0.002 0.000 sre_parse.py:201(get)
1 0.000 0.000 0.000 0.000 sre_parse.py:205(tell)
114 0.000 0.000 0.000 0.000 sre_parse.py:210(isident)
21 0.000 0.000 0.000 0.000 sre_parse.py:216(isname)
2 0.000 0.000 0.000 0.000 sre_parse.py:225(_class_escape)
32 0.000 0.000 0.000 0.000 sre_parse.py:257(_escape)
47/12 0.000 0.000 0.004 0.000 sre_parse.py:301(_parse_sub)
52/13 0.001 0.000 0.004 0.000 sre_parse.py:379(_parse)
12 0.000 0.000 0.000 0.000 sre_parse.py:67(__init__)
12 0.000 0.000 0.004 0.000 sre_parse.py:675(parse)
29 0.000 0.000 0.000 0.000 sre_parse.py:72(opengroup)
29 0.000 0.000 0.000 0.000 sre_parse.py:83(closegroup)
98 0.000 0.000 0.000 0.000 sre_parse.py:90(__init__)
442674 0.315 0.000 0.315 0.000 ttypes.py:2296(__init__)
590232 0.262 0.000 0.262 0.000 ttypes.py:2380(__init__)
1032906 2.061 0.000 2.061 0.000 ttypes.py:2644(__init__)
49186 0.033 0.000 0.033 0.000 ttypes.py:2786(__init__)
49344 0.050 0.000 0.050 0.000 ttypes.py:2896(__init__)
155 0.000 0.000 0.000 0.000 ttypes.py:5195(__init__)
155 0.000 0.000 0.000 0.000 ttypes.py:5266(__init__)
21 0.000 0.000 0.000 0.000 ttypes.py:540(__init__)
49186 0.055 0.000 0.055 0.000 ttypes.py:5764(__init__)
1 0.000 0.000 0.000 0.000 ttypes.py:5840(TFetchResultsResp)
49186 0.044 0.000 0.044 0.000 ttypes.py:5855(__init__)
1 286.275 286.275 2121.074 2121.074 test.py:24(func1)
1 0.001 0.001 2121.112 2121.112 test.py:3(<module>)
12 0.000 0.000 0.000 0.000 {_sre.compile}
35 0.000 0.000 0.000 0.000 {_sre.getlower}
148032 0.134 0.000 0.134 0.000 {_struct.pack}
148032 0.107 0.000 0.107 0.000 {_struct.unpack}
2294724 4.074 0.000 4.074 0.000 {cStringIO.StringIO}
251 0.000 0.000 0.000 0.000 {getattr}
99230 0.061 0.000 0.061 0.000 {isinstance}
24/13 0.000 0.000 0.000 0.000 {issubclass}
55672069/55672017 7.519 0.000 7.519 0.000 {len}
49351 0.061 0.000 0.061 0.000 {max}
4 0.000 0.000 0.000 0.000 {method '__contains__' of 'frozenset' objects}
11 0.000 0.000 0.000 0.000 {method '__subclasses__' of 'type' objects}
1107483909 105.411 0.000 105.411 0.000 {method 'append' of 'list' objects}
4 0.220 0.055 0.220 0.055 {method 'close' of 'file' objects}
1 0.000 0.000 0.000 0.000 {method 'connect' of '_socket.socket' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.000 0.000 {method 'endswith' of 'str' objects}
49198 0.249 0.000 0.249 0.000 {method 'extend' of 'list' objects}
49344 0.021 0.000 0.021 0.000 {method 'getvalue' of 'cStringIO.StringO' objects}
50340126 32.139 0.000 32.139 0.000 {method 'pop' of 'list' objects}
246720 0.140 0.000 0.140 0.000 {method 'read' of 'cStringIO.StringI' objects}
23 0.000 0.000 0.000 0.000 {method 'readline' of 'file' objects}
2245378 185.259 0.000 185.259 0.000 {method 'recv' of '_socket.socket' objects}
49344 0.764 0.000 0.764 0.000 {method 'send' of '_socket.socket' objects}
2 0.000 0.000 0.000 0.000 {method 'setter' of 'property' objects}
1 0.000 0.000 0.000 0.000 {method 'settimeout' of '_socket.socket' objects}
45 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects}
11 0.000 0.000 0.000 0.000 {method 'startswith' of 'str' objects}
45 0.000 0.000 0.000 0.000 {method 'strip' of 'str' objects}
246720 0.198 0.000 0.198 0.000 {method 'write' of 'cStringIO.StringO' objects}
50340130 34.491 0.000 34.491 0.000 {method 'write' of 'file' objects}
193 0.000 0.000 0.000 0.000 {min}
4 0.800 0.200 0.800 0.200 {open}
1057142741 84.179 0.000 84.179 0.000 {ord}
1 0.000 0.000 0.000 0.000 {posix.getcwd}
1 0.000 0.000 0.000 0.000 {posix.lstat}
1 0.000 0.000 0.000 0.000 {posix.urandom}
1 0.000 0.000 0.000 0.000 {repr}
146 0.000 0.000 0.000 0.000 {setattr}
2 0.000 0.000 0.000 0.000 {sys._getframe}
3 0.000 0.000 0.000 0.000 {thread.allocate_lock}
2 0.000 0.000 0.000 0.000 {thread.get_ident}
49344 32.932 0.001 93.520 0.002 {thrift.protocol.fastbinary.decode_binary}
49344 0.184 0.000 0.184 0.000 {thrift.protocol.fastbinary.encode_binary}
154 37.048 0.241 37.048 0.241 {time.sleep}
1 0.000 0.000 0.000 0.000 {time.strftime}
159 0.000 0.000 0.000 0.000 {time.time}
最佳答案
如果将探查器的输出保存到文件中(我保存到文件 a
中)并按第二列 tottime
(函数花费的时间)对其进行排序本身排除调用的函数)并获取前 10 个(不确定给它一个 head
是否合适):</p>
[max@supernova:~/tmp] $ sort --key=2nr a | head
49186 1153.582 0.023 1584.908 0.032 hiveserver2.py:263(fetch_results)
1 286.275 286.275 2121.074 2121.074 test.py:24(func1)
2245378 185.259 0.000 185.259 0.000 {method 'recv' of '_socket.socket' objects}
1107483909 105.411 0.000 105.411 0.000 {method 'append' of 'list' objects}
1057142741 84.179 0.000 84.179 0.000 {ord}
50340127 52.496 0.000 1693.039 0.000 hiveserver2.py:250(next)
50340127 50.937 0.000 1760.004 0.000 hiveserver2.py:207(fetchone)
154 37.048 0.241 37.048 0.241 {time.sleep}
50340130 34.491 0.000 34.491 0.000 {method 'write' of 'file' objects}
49344 32.932 0.001 93.520 0.002 {thrift.protocol.fastbinary.decode_binary}
解读:
大部分时间花费在对 hiveserver2.py:263(fetch_results)
的 49186 次调用上。我对那个库不熟悉,但是我会检查是否可以减少调用次数。通常,存在函数序言和结尾成本、请求/响应开销等,因此通过减少调用数量,您可以减少一些开销。可能会进行批量结果行提取(即一次多于一行)。
不确定 test.py:24(func1) 的作用,但它是探查器输出中的第二个热点,尽管它只被调用一次。它会执行任何类型的循环吗?
接下来是 185.259
个时间单位,它们花费在对 {method 'recv' of '_socket.socket' 对象}
的 2245378 次调用上。 recv
调用次数似乎过多,可能是因为使用了 fetchone
,因为看起来它通过套接字为每一行发送一个新请求。
下一个{“list”对象的“append”方法}
,调用它的位置信息不足。
与 {ord}
相同。
接下来,两个条目 hiveserver2.py:250(next)
和 hiveserver2.py:207(fetchone)
占总时间的大部分 (第四列)。
考虑到上面的解释,看起来调用 fetchone
是相当次优的。
如果保证结果集适合可用内存,我会尝试调用fetchall
,而不是调用fetchone
,否则fetchmany
,以便使用尽可能少的请求/批处理从数据库加载记录集。
关于python - 如何在python中将数据库记录逐 block 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31409224/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!