简体   繁体   English

在Sqlite中提高查询速度

[英]Increase Query Speed in Sqlite

I am a very newbie in using python and sqlite. 我是使用python和sqlite的新手。 I am trying to create a script that reads a data from a table (rawdata) and then performs some calculations which is then stored in a new table. 我正在尝试创建一个脚本,该脚本从表(rawdata)中读取数据,然后执行一些计算,然后将其存储在新表中。 I am counting the number race that a player has won before that date at a particular track position and calculating the percentage. 我计算的是该日期之前玩家在特定赛道位置上赢得的数字竞赛,并计算出百分比。 There are 15 track positions in total. 共有15个赛道位置。 Overall the script is very slow. 总体而言,脚本非常慢。 Any suggestions to improve its speed. 任何提高速度的建议。 I have already used the PRAGMA parameters. 我已经使用过PRAGMA参数。

Below is the script. 下面是脚本。

for item in result:
        l1 = str(item[0])
        l2 = item[1]
        l3 = int(item[2])

        winpost = []
        key = l1.split("|")
        dt = l2

        ###Denominator--------------
        cursor.execute(
            "SELECT rowid FROM rawdata WHERE Track = ? AND Date< ? AND Distance = ? AND Surface =? AND OfficialFinish=1",
            (key[2], dt, str(key[4]), str(key[5]),))
        result_den1 = cursor.fetchall()
        cursor.execute(
            "SELECT rowid FROM rawdata WHERE Track = ? AND RaceSN<= ? AND Date= ? AND Distance = ? AND Surface =? AND OfficialFinish=1",
            (key[2], int(key[3]), dt, str(key[4]), str(key[5]),))
        result_den2 = cursor.fetchall()
        totalmat = len(result_den1) + len(result_den2)

        if totalmat > 0:

            for i in range(1, 16):
                cursor.execute(
                    "SELECT rowid FROM rawdata WHERE Track = ? AND Date< ? AND PolPosition = ? AND Distance = ? AND Surface =? AND OfficialFinish=1",
                    (key[2], dt, i, str(key[4]), str(key[5]),))
                result_num1 = cursor.fetchall()
                cursor.execute(
                    "SELECT rowid FROM rawdata WHERE Track = ? AND RaceSN<= ?  AND Date= ? AND PolPosition = ? AND Distance = ? AND Surface =? AND OfficialFinish=1",
                    (key[2], int(key[3]), dt, i, str(key[4]), str(key[5]),))
                result_num2 = cursor.fetchall()
                winpost.append(len(result_num1) + len(result_num2))

            winpost = [float(x) / totalmat for x in winpost]
            rank = rankmin(winpost)
            franks = list(rank)
            franks.insert(0, int(key[3]))
            franks.insert(0, dt)
            franks.insert(0, l1)
            table1.append(franks)
            franks = []

    cursor.executemany("INSERT INTO posttable VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table1)

Sending and retrieving an SQL query is "expensive" in terms of time. 就时间而言,发送和检索SQL查询是“昂贵的”。 The easiest way to speed things up would be to use SQL functions to reduce the number of queries. 加快速度的最简单方法是使用SQL函数来减少查询数量。

For example, the first two queries could be reduced to a single call using COUNT(), UNION, and Aliases. 例如,可以使用COUNT(),UNION和Aliases将前两个查询简化为单个调用。

SELECT COUNT(*)
 FROM
 ( SELECT rowid FROM rawdata where ...
   UNION
   SELECT rowid FROM rawdata where ...
 ) totalmatch

In this case we take the two original queries (with your conditions in place of the "...") combine them with a UNION statement, give that union the alias "totalmatch", and count all the rows in it. 在这种情况下,我们将两个原始查询(用您的条件代替“ ...”)与UNION语句结合起来,为该并集赋予别名“ totalmatch”,并计算其中的所有行。

Same thing can be done with the second set of queries. 使用第二组查询可以完成相同的操作。 Instead of cycling 16 times over 2 queries (resulting in 32 calls to the SQL engine) you can replace it with one query by also using GROUP BY. 您可以通过使用GROUP BY将其替换为一个查询,而不必对2个查询循环16次(导致对SQL引擎的32次调用)。

SELECT PolPosition, COUNT(PolPosition)
FROM
( SELECT PolPosition FROM rawdata WHERE ...
  UNION
  SELECt PolPosition FROM rawdata WHERE ...
) totalmatch
GROUP BY PolPosition

In this case we take the exact same query as before and group it by PolPosition, using COUNT to display how many rows are in each group. 在这种情况下,我们采用与之前完全相同的查询,并按PolPosition将其分组,使用COUNT显示每个组中有多少行。

W3Schools is a great resource for how these functions work: http://www.w3schools.com/sql/default.asp W3Schools是有关这些功能如何工作的重要资源: http : //www.w3schools.com/sql/default.asp

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM