简体   繁体   English

需要一种更有效的方法来从数据库中检索多个日期系列

[英]Need more efficient way to retrieve several dated series from a DB

I have a database table that includes TRADE_DATE, CURRVAL, and ITEM fields. 我有一个数据库表,其中包含TRADE_DATE,CURRVAL和ITEM字段。 I first have two arrays/lists: arrVars (strings), dates (dates). 我首先有两个数组/列表:arrVars(字符串),日期(日期)。 Each string in arrVars represents an ITEM for which I need to retrieve the CURRVALs for each TRADE_DATE in dates. arrVars中的每个字符串都代表一个项目,我需要为它检索日期中每个TRADE_DATE的CURRVAL。 I'm new to Python, and I'm certainly no expert w/ databases, and I'm sure there are ways to speed my code up. 我是Python的新手,我当然也不具备数据库专家,而且我敢肯定,有很多方法可以加快我的代码的速度。

First part is just creating the dates list from my first db connection. 第一部分只是从我的第一个数据库连接创建日期列表。 I'm just iterating through each row and appending it into the dates list. 我只是遍历每一行并将其添加到日期列表中。 Is there a better way? 有没有更好的办法?

i = 0
for row in cursor.fetchall():
    dates.append(row[0])
    i+=1

Second, I'm looping through each ITEM in arrVars and then looping through each TRADE_DATE in dates to create each array of CURRVALs and put the arrays into a matrix. 其次,我循环遍历arrVars中的每个ITEM,然后循环遍历日期中的每个TRADE_DATE以创建每个CURRVAL数组,并将这些数组放入矩阵中。 This is pretty damn slow, so I'm hoping there is a better way as well. 这真是太慢了,所以我希望还有更好的方法。

M = []
dtFormat = '%Y/%m/%d'
for item in arrVars:
    tmp = []
    for dt in dates:
        strSQL = "SELECT CURRVAL FROM tblGanData WHERE ITEM = '" + item + "' AND TRADE_DATE = #" + dt.strftime(dtFormat) + "#"
        cursor.execute(strSQL)
        tmp.append(cursor.fetchone()[0])
    M.append(tmp)

Thank you!! 谢谢!!

For the first bit, you might want do to something like this: 首先,您可能需要执行以下操作:

dates = [row[0] for row in cursor.fetchall()]

But I'd be very interested in seeing the SQL statement that you're using for that cursor. 但是我对查看您用于该游标的SQL语句非常感兴趣。

select some_date from my_table

is going to be faster than 将会比

select * from my_table

(How much faster depends on how many rows you are getting back and the speed of the network connection between your client and server.) (快多少取决于您返回的行数以及客户端和服务器之间的网络连接速度。)

For your second part, you're executing one query (with the full round-trip cost) for each Item/Date combination. 在第二部分中,您将为每个“项目/日期”组合执行一个查询(包含全部往返费用)。

So maybe something like this 所以也许像这样

# build a list of all the dates
dates_str = ",".join(['#' + dt.strftime(dtFormat) + "#" 
                      for dt in dates])

# build a list of all the items
items_str = ",".join(["'" + item + "'" for item in items])

# run one SQL query that gets everything
cursor.execute("""
    select item, trade_date, currval
    from tblGanData
    where item in (%s)
    and trade_date in (%s)
    order by item, trade_date
""" % (items_str, dates_str))

You will have to do some logic when fetching the values to turn the list of currvals into a matrix, let me know if you need help with that. 在获取值以将曲线表列表转换为矩阵时,您将必须执行一些逻辑,让我知道是否需要帮助。

PS How many items/dates are we talking about here? PS我们在这里谈论多少个项目/日期?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:从多个列表中获取随机切片的更有效方法 - Python: More efficient way to get random slices from several lists 从大型文件检索行的更有效方法 - More efficient way to retrieve lines from a huge file 有没有更有效的方法将映射应用于熊猫系列? - Is there a more efficient way to apply a mapping to a pandas series? 在间隙处拆分时间序列数据(pd.Series)的更有效方法? - A more efficient way to split timeseries data (pd.Series) at gaps? Python Selenium 添加多个用户的更有效方法 - Python Selenium More Efficient Way of Adding Several Users 从Python中的csv列检索每个唯一值的首次出现的更有效方法 - More efficient way to retrieve first occurrence of every unique value from a csv column in Python 需要一种更有效的方法来从单个大型 dataframe 创建数据帧字典 - Need a more efficient way of creating a dictionary of dataframes from a single large dataframe 有没有一种有效的方法从字典中检索值 - Is there an efficient way to retrieve values from dictionary 在 3d numpy 数组的一系列二维子数组上执行 function 的更有效方法是什么? - What's a more efficient way to execute a function over a series of 2d subarrays from a 3d numpy array? 从字典中处理 KeyError 的更有效方法 - More efficient way to deal with KeyError from dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM