我正在解析一个巨大的(我的意思是真的huuuuge)xml文件。它包含了一些像这样的文章:
<article key="journals/cgf/HaeglerWAGM10" mdate="2010-11-12">
<author>Simon Haegler</author>
<author>Peter Wonka</author>
<author>Stefan Müller Arisona</author>
<author>Luc J. Van Gool</author>
<author>Pascal Müller</author>
<title>Grammar-based Encoding of Facades.</title>
<pages>1479-1487</pages>
<year>2010</year>
<volume>29</volume>
<journal>Comput. Graph. Forum</journal>
<number>4</number>
<ee>http://dx.doi.org/10.1111/j.1467-8659.2010.01745.x</ee>
<url>db/journals/cgf/cgf29.html#HaeglerWAGM10</url>
</article>
我逐步浏览文件并通过lxml解析这些文章。如果我运行代码而不将项目存储到我的数据库中(通知populate_database()
它会在~3秒内完成大约1000次。但是如果我激活存储(uncomment
populate_database()`)它会产生一些每秒10次。这是正常的吗?我记得一次解析文件一次,而数据库并不是一个瓶颈。但我有一个不同的方法......(通过我的文件找到它...)< / p>
这是功能,让我头疼。我评论了这三个cursor.executes
并且代码再次竞争。所以它接缝,MySQL出现问题或执行错误(我的noobisch猜测)。有什么建议吗?
def add_paper(paper, cursor):
questionmarks = str(('?',)*len(paper)).replace("'", "")
# The line above: produces (?, ?, ?, ... ,?) for oursql query
keys, values = paper.keys(), paper.values()
keys = str(tuple(keys)).replace("'", "")
# The line above: produces (mdate, title, ... date, some_key)
query_paper = '''INSERT INTO dblp2.papers {0} VALUES {1};'''.\
format(keys, questionmarks)
values = tuple(v.encode('utf8') for v in values)
cursor.execute(query_paper, values)
paper_id = cursor.lastrowid
return paper_id
def populate_database(paper, authors, cursor):
paper_id = add_paper(paper, cursor)
query_author ="""INSERT INTO dblp2.authors
(name) VALUES (?) ON DUPLICATE KEY UPDATE
id=LAST_INSERT_ID(id)"""
query_link_table = "INSERT INTO dblp2.author_paper
(author_id, paper_id) VALUES (?, ?)"
for author in authors:
cursor.execute(query_author, (author.encode('utf8'),))
author_id = cursor.lastrowid
cursor.execute(query_link_table, (author_id, paper_id))
我添加了cProfile
的分析输出:
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 15.666 15.666 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 __init__.py:49(normalize_encoding)
1 0.000 0.000 0.000 0.000 __init__.py:71(search_function)
510 0.002 0.000 0.002 0.000 _elementpath.py:222(_build_path_iterator)
510 0.005 0.000 0.008 0.000 _elementpath.py:260(iterfind)
408 0.005 0.000 0.017 0.000 _elementpath.py:270(find)
102 0.003 0.000 0.011 0.000 _elementpath.py:285(findall)
10 0.000 0.000 0.000 0.000 _elementpath.py:70(xpath_tokenizer)
5 0.000 0.000 0.000 0.000 _elementpath.py:85(prepare_child)
987 0.009 0.000 0.013 0.000 _elementpath.py:87(select)
1 0.000 0.000 0.000 0.000 codecs.py:77(__new__)
1 0.000 0.000 0.000 0.000 utf_8.py:15(decode)
1 0.000 0.000 0.000 0.000 utf_8.py:33(getregentry)
102 0.008 0.000 5.601 0.055 xml2db.py:25(add_paper)
680 0.003 0.000 0.006 0.000 xml2db.py:31(<genexpr>)
102 0.005 0.000 15.468 0.152 xml2db.py:36(populate_database)
477 0.003 0.000 0.013 0.000 xml2db.py:45(clean_parse)
101 0.002 0.000 0.005 0.000 xml2db.py:52(clear_element)
103 0.019 0.000 0.024 0.000 xml2db.py:57(extract_paper_elements)
1 0.017 0.017 15.557 15.557 xml2db.py:63(fast_iter)
1 0.004 0.004 15.665 15.665 xml2db.py:89(main)
1 0.000 0.000 0.000 0.000 {__import__}
1 0.000 0.000 0.000 0.000 {_codecs.utf_8_decode}
1 0.000 0.000 0.000 0.000 {built-in method __new__ of type object at 0x8245fc0}
5 0.000 0.000 0.000 0.000 {built-in method findall}
1 0.000 0.000 0.000 0.000 {hasattr}
2 0.000 0.000 0.000 0.000 {isinstance}
515 0.001 0.000 0.001 0.000 {iter}
107 0.000 0.000 0.000 0.000 {len}
477 0.010 0.000 0.010 0.000 {lxml.etree.strip_tags}
5 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
101 0.002 0.000 0.002 0.000 {method 'clear' of 'lxml.etree._Element' objects}
1 0.000 0.000 0.000 0.000 {method 'cursor' of 'oursql.Connection' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
778 0.007 0.000 0.007 0.000 {method 'encode' of 'str' objects}
5 0.000 0.000 0.000 0.000 {method 'encode' of 'unicode' objects}
516 15.544 0.030 15.544 0.030 {method 'execute' of 'oursql.Cursor' objects}
408 0.004 0.000 0.023 0.000 {method 'find' of 'lxml.etree._Element' objects}
102 0.001 0.000 0.012 0.000 {method 'findall' of 'lxml.etree._Element' objects}
103 0.001 0.000 0.001 0.000 {method 'format' of 'str' objects}
2 0.000 0.000 0.000 0.000 {method 'get' of 'dict' objects}
204 0.001 0.000 0.001 0.000 {method 'get' of 'lxml.etree._Element' objects}
100 0.000 0.000 0.000 0.000 {method 'getparent' of 'lxml.etree._Element' objects}
201 0.001 0.000 0.001 0.000 {method 'getprevious' of 'lxml.etree._Element' objects}
510 0.004 0.000 0.004 0.000 {method 'iterchildren' of 'lxml.etree._Element' objects}
1 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects}
102 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}
204 0.001 0.000 0.001 0.000 {method 'replace' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'translate' of 'str' objects}
102 0.000 0.000 0.000 0.000 {method 'values' of 'dict' objects}
2 0.000 0.000 0.000 0.000 {time.time}
答案 0 :(得分:2)
在我看来,你正在运行一大堆单独的insert
语句。打开mysql db的日志记录应该会显示一堆这样的语句:
....
INSERT INTO dblp2.authors (name) VALUES (a) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
INSERT INTO dblp2.authors (name) VALUES (b) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
INSERT INTO dblp2.authors (name) VALUES (c) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
INSERT INTO dblp2.authors (name) VALUES (d) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
INSERT INTO dblp2.authors (name) VALUES (e) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
INSERT INTO dblp2.authors (name) VALUES (f) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
....
您希望运行paramaterized/multiple insert
语句,或将所有内容打包到csv文件中并执行bulk insert
。
批量插入更快,并且它支持替换重复行(check the docs)。如果您想要真正的更新,请使用多个插入或尝试此temp table idea。
多个插入语句有点像这样:
cursor.execute(query_author, [author.encode('utf8') for author in authors])
应该在日志中为您提供如下条目:
INSERT INTO dblp2.authors (name) VALUES (a,b,c,d,e,f) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
第二个表的ID可能存在一些问题。
答案 1 :(得分:0)
每个数据库都提供EXPLAIN命令来确定执行计划。
此外:Python有一个探查器来检测哪些代码很慢。
首先:你进行分析,如果你不再进一步,你就会回来。