一个MongoDB的集合最好的办法,让我在这里解释一下这个项目的问题第一:什么是查询使用Python 3
我正在开发一个Web应用程序,使用该库CherryPy的, PyMongo和数据库后端是一个MongoDB数据库,我使用Python 3作为开发语言。
我的数据库集合包含260.640的文件,这是在格式这里简化:
{"_id":1,"time":"2014-01-01 00:00:00","value":"1.37468"}
集合中的所有文件,有一个ID(打算从0至260640)和时间,这是每增加一分钟(所以我总共有6个月的数据)。
我在Windows控制台中运行Mongod.exe服务器,在另一个Windows控制台中运行我的python web服务器,并使用谷歌浏览器查看网页。
我的目标:
我想查询数据库集合,这样我就可以用行的HTML表两个日期之间,例如:2014年1月1日00:00:00和2014-01-10 00:00:00,然后在CherryPy正在制作的网页上查看该表格。
我的问题:
使用正在这里提供了问题的代码,我可以查询数据库,并显示在网页上的表,但是,大约需要30-50秒,以显示大约7200行,这只是大约5天的数据,当我需要显示10天的数据甚至是一个月的数据时,我们讨论的是等待时间更长,问题首先是用户必须等待,而且如果用户选择较长的时间间隔,浏览器可能会超时,从而导致应用程序死机。
我的慢代码:
这里的是,当前工作的代码,而只是作为一个“标准的汽车”,我需要一个“超级跑车”。
def onem(self):
# Get the MongoClient from the PyMongo lib.
client = MongoClient()
# our database name is raw_data
db = client.raw_data
# Get the starting date from the database (returns a valid date string).
date_from = self.getFromDateToDatePickerFromDB();
# Get the end date from the database (returns a valid date string).
date_to = self.getToDateToDatePicker();
# Query the database for the collection of documents.
collection = db.data.find({'time' : {'$gte' : date_from, '$lt' : date_to}})
# Define a variable to hold the temp rows.
html = ""
# for each document in our collection.
for document in collection:
# build the table.
html = html + '''
<tr>
<td>''' + str(int(document['_id'])) + '''</td>
<td>''' + str(document['time']) + '''</td>
<td>''' + str(document['value']) + '''</td>
</tr>
'''
table = '''
<table border="1" celspacing="0" cellpadding="0" width="100%">
<thead>
<tr>
<td>ID</td>
<td>TIME</td>
<td>VALUE</td>
</tr>
</thead>
<tbody>
''' + html + '''
</tbody>
</table>
'''
# return the valid html with the table to a function which
# outputs an html template to the browser.
return self.GetStaticHTML(table)
# Tell CherryPy that we are done working on the webpage and it lets us show it to the browser.
onem.exposed = True
如果你们知道一个更好的方式来查询的MongoDB数据库比所提供的代码:
collection = db.data.find({'time' : {'$gte' : date_from, '$lt' : date_to}})
或者,如果你知道一种方法,以加快数据库,代码或任何东西,然后我真的很想听到它。
谢谢你,
1.你认为我能赢得多少速度? - 我会马上看看它,我应该索引文档中的所有字段还是只在时间字段中获得最佳性能? 2.我说的“在一张桌子上”的任务,但也许我们应该使用分页。 感谢您的回复:) –
如果查询确实是热点,您只能通过分析代码来确定问题,这可能是一个显着的改进。搜索可能从O(N)到O(log N)。通常,您可以索引要查询的字段。结帐http://docs.mongodb.org/manual/applications/indexes/ – Bernhard