简体   繁体   English

如何使此更新查询更有效

[英]How can I make this update query more efficient

I'm using SQLite3. 我正在使用SQLite3。 Can anyone see how to make this query more efficient? 谁能看到如何提高此查询的效率? Essentially, I have a table into which I insert some records with a given InsertDate. 本质上,我有一个表,可以在其中插入带有给定InsertDate的一些记录。 The table might already contain duplicates (on 4 other fields) to the newly inserted records. 该表可能已经包含新插入记录的重复项(在其他4个字段上)。 I want to update a certain field in all those duplicates 我想更新所有重复项中的某个字段

To find the duplicate rows I use this query (identifiers changed for clarity) 要查找重复的行,我使用此查询(为清楚起见,更改了标识符)

SELECT Table1.rowid
FROM   Table1 
INNER JOIN
      (
       SELECT Field1, Field2, Field3, Field4, InsertDate
       FROM Table1
       WHERE InsertDate = '2013-07-11' 
       ) AS T
ON
       T.Field1 = Table1.Field1
   AND T.Field2 = Table1.Field2
   AND T.Field3 = Table1.Field3
   AND T.Field4 = Table1.Field4

WHERE Table1.InsertDate <> T.InsertDate  --(could use WHERE Table1.InsertDate <> '2013-07-11')

That select query takes 6s to run with indexes on Fields 1,2 and 3 (interestingly I found having an index on Field4 increased the time to 9s so I didn't have one) 该选择查询需要6s来运行字段1,2和3上的索引(有趣的是,我发现在Field4上拥有索引将时间增加到9s,所以我没有一个)

I then use that query in the update query, deciding which records to update by having the select following an IN clause in the update statement ie 然后,我在更新查询中使用该查询,通过在更新语句中的IN子句之后进行选择来决定要更新的记录,即

UPDATE Table 1
SET Field 5 = 'A'
WHERE Table1.RowID IN

    -- here comes the original select query
    (
    SELECT Table1.rowid
    FROM   Table1 
    INNER JOIN
       (
       SELECT FIELD1, Field2, Field3, Field4, InsertDate
       FROM Table1
       WHERE InsertDate = '2013-07-11' 
       ) AS T
    ON
        T.Field1 = Table1.Field1
    AND T.Field2 = Table1.Field2
    AND T.Field3 = Table1.Field3
    AND T.Field4 = Table1.Field4

    WHERE Table1.InsertDate <> T.InsertDate
    )

But this take nearly 25 seconds to run! 但这需要将近25秒钟才能运行!

I tried making a temp table out of the intial SELECT, intending to use that in the update query but that also took around 25s to make the table so it doen't look like its the actual updating that's slowing things down, it seens to be using my 6s select query with 'IN'. 我尝试用初始SELECT制作一个临时表,打算在更新查询中使用它,但制作表也花费了大约25秒钟,因此它看起来不像它的实际更新那样使速度变慢,它看起来像是用我的6s选择查询加上“ IN”。

Typically I would be updating about 9000 rows out of about 300,000. 通常,我将在大约300,000中更新大约9000行。

Any suggestions to improve this query would be very welcome. 任何改进此查询的建议都将受到欢迎。

If I understand the logic correctly, you don't need rowid : 如果我正确理解逻辑,则不需要rowid

UPDATE Table 1
    SET Field 5 = 'A'
    WHERE InsertDate <> '2013-07-11' and
          EXISTS (select 1
                  from table1 tt
                  where tt.InsertDate = '2013-07-11' and
                        T.Field1 = tt.Field1 and
                        T.Field2 = tt.Field2 and
                        T.Field3 = tt.Field3 and
                        T.Field4 = tt.Field4        
                 );

Then, create an index on table1(field1, field2, field3, field4, insertdate) and table1(insertdate) . 然后,在table1(field1, field2, field3, field4, insertdate)table1(insertdate)上创建索引。

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

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