我设计了数据仓库应用程序,但是在从源获取数据并将它们保存到数据库时,我遇到了性能不佳的问题。- 只有大约 150 kB/s。
由于客户施加的限制,我被迫在 64 位 Win 机器上使用 Django 并将数据保存到 MS SQL Express(下面的确切版本)。我正在使用 django-mssql (1.7) 后端。
原始数据存储在.dbf
文件(Visual FoxPro)中,dbfread
将文件中的每一行作为 Python 返回dict
(这不是问题,仅通过运行读取器丢弃数据进行测试)。然后检查该字典的数据质量(sanitize_value_for_db()
下面的函数),将数据复制到 Django 数据模型(填充对象的属性;表很宽,因此每个表/对象有大约 100 列/属性),对象被保存到 db。使用 Django objects.bulk_create()
(50-100 批)。
cProfile
我使用和pstats
模块通过分析器运行代码。结果如下。我看到大部分时间都花在了 PyWin 上。但我不知道是否有什么我能做的。任何提示或意见将不胜感激。谢谢。
配置:
- Xeon E5-2403 v2 @ 1,8 GHz,30 GB RAM
- Windows Server 2012 R2(64 位)
- MS SQL Express 64 位,v 11.02.2100.60
- Python (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32#
- pywin32-219.win-amd64-py3.4
- 姜戈 (1.7.10)
- django-mssql (1.7)
配置文件(在较小的数据样本上):
ncalls tottime percall cumtime percall filename:lineno(function)
2306881 332.596 0.000 332.596 0.000 {built-in method compile}
4592205 186.028 0.000 186.028 0.000 {method 'InvokeTypes' of 'PyIDispatch' objects}
39558280 176.963 0.000 176.963 0.000 {method 'Bind' of 'PyITypeComp' objects}
9889570 173.905 0.000 712.091 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:390(_LazyAddAttr_)
17464949 151.818 0.000 279.692 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:151(_AddFunc_)
7203 142.584 0.020 2215.686 0.308 c:\Python34\Lib\site-packages\sqlserver_ado\dbapi.py:587(execute)
4 120.012 30.003 120.012 30.003 {built-in method sleep}
11484250 100.385 0.000 1269.561 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:444(__getattr__)
12167658 67.229 0.000 67.229 0.000 {method 'Invoke' of 'PyIDispatch' objects}
22100535 50.127 0.000 53.982 0.000 <string>:12(__init__)
4599456 48.409 0.000 225.498 0.000 c:\Python34\Lib\site-packages\win32com\client\__init__.py:18(__WrapDispatch)
27390427 45.257 0.000 65.391 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:420(_ResolveType)
7189 44.294 0.006 2296.538 0.319 c:\Python34\Lib\site-packages\django\db\models\query.py:911(_insert)
9889570 43.621 0.000 759.155 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:381(__LazyMap__)
2306881 43.605 0.000 130.943 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:303(MakeDispatchFuncMethod)
5275612 33.300 0.000 402.786 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:524(__setattr__)
17464949 32.793 0.000 32.793 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:58(__init__)
4599456 32.494 0.000 124.536 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:120(Dispatch)
2292471 30.889 0.000 843.841 0.000 c:\Python34\Lib\site-packages\sqlserver_ado\dbapi.py:266(_configure_parameter)
7007079 27.918 0.000 53.176 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:516(MakePublicAttributeName)
2306881 27.907 0.000 46.266 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:483(_BuildArgList)
15201179 25.593 0.000 25.593 0.000 {method 'GetTypeAttr' of 'PyITypeInfo' objects}
68338972 24.271 0.000 24.758 0.000 {built-in method isinstance}
2306881 23.056 0.000 538.037 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:314(_make_method_)
17464949 21.683 0.000 21.683 0.000 {method 'GetNames' of 'PyITypeInfo' objects}
7007079 20.396 0.000 20.396 0.000 c:\Python34\Lib\site-packages\win32com\client\build.py:546(<listcomp>)
4599456 18.166 0.000 18.166 0.000 c:\Python34\Lib\site-packages\win32com\client\dynamic.py:172(__init__)
3422203 15.624 0.000 20.870 0.000 C:\MRI\mri\dwh\daq\daq_utils.py:152(sanitize_value_for_db)