一旦表变大,更新查询永远不会完成

时间:2016-05-04 14:24:53

标签: sql sqlite

当表有几百行甚至几千行时,此更新语句运行正常,但一旦在子字段表中达到100,000行,它就永远不会运行。

UPDATE 
Records
SET Author = (SELECT Data
          FROM Subfields
          LEFT OUTER JOIN Fields f on f.FieldID = Subfields.FieldID
          WHERE f.RecordID = Records.RecordID AND f.TagNumber = '100' and Code = 'a')

这是执行计划。它绝对使用索引,所以我不确定如何才能让它更快。

selectid    order   from    detail
0   0   0   SCAN TABLE Records
0   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 0
0   0   0   SEARCH TABLE Subfields USING INDEX Code_FieldID (Code=?)
0   1   1   SEARCH TABLE Fields AS f USING INTEGER PRIMARY KEY (rowid=?)

如果有人能帮我解决速度问题,我会非常感激。提前谢谢!

CREATE TABLE [Fields](
    [FieldID] integer PRIMARY KEY ASC AUTOINCREMENT NOT NULL, 
    [RecordID] nvarchar(2147483647) NOT NULL, 
    [TagNumber] nvarchar(2147483647) NOT NULL, 
    [Ind1] char, 
    [Ind2] char, 
    [ControlData] nvarchar(2147483647), 
    FOREIGN KEY([RecordID]) REFERENCES Records([RecordID]) ON DELETE CASCADE ON UPDATE RESTRICT);

CREATE TABLE [Records](
    [RecordID] integer PRIMARY KEY ASC AUTOINCREMENT NOT NULL, 
    [DateAdded] datetime NOT NULL, 
    [DateChanged] datetime, 
    [Author] nvarchar(2147483647), 
    [Title] nvarchar(2147483647), 
    [CopyrightDate] integer, 
    [Barcode] nvarchar(2147483647), 
    [Classification] nvarchar(2147483647), 
    [MainEntry] nvarchar(2147483647), 
    [Custom1] nvarchar(2147483647), 
    [Custom2] nvarchar(2147483647), 
    [Custom3] nvarchar(2147483647), 
    [Custom4] nvarchar(2147483647), 
    [Custom5] nvarchar(2147483647), 
    [ImportErrors] nvarchar(2147483647));

CREATE TABLE [Settings](
    [RecordListAtTop] bool, 
    [ClearDatabaseOnExit] bool, 
    [ExportFormat] char(1), 
    [CustomTag1] nvarchar(3), 
    [CustomCode1] nvarchar(1), 
    [CustomData1] nvarchar(2147483647), 
    [CustomTag2] nvarchar(3), 
    [CustomCode2] nvarchar(1), 
    [CustomData2] nvarchar(2147483647), 
    [CustomTag3] nvarchar(3), 
    [CustomCode3] nvarchar(1), 
    [CustomData3] nvarchar(2147483647), 
    [CustomTag4] nvarchar(3), 
    [CustomCode4] nvarchar(1), 
    [CustomData4] nvarchar(2147483647), 
    [CustomTag5] nvarchar(3), 
    [CustomCode5] varchar(1), 
    [CustomData5] nvarchar(2147483647));

CREATE TABLE [Subfields](
    [SubfieldID] integer PRIMARY KEY ASC AUTOINCREMENT NOT NULL, 
    [FieldID] bigint NOT NULL, 
    [Code] char NOT NULL, 
    [Data] nvarchar(2147483647) NOT NULL, 
    FOREIGN KEY([FieldID]) REFERENCES Fields([FieldID]) ON DELETE CASCADE ON UPDATE RESTRICT);

CREATE INDEX [Code_FieldID]
ON [Subfields](
    [Code], 
    [FieldID]);

CREATE INDEX [FieldID]
ON [Subfields](
    [FieldID] ASC);

CREATE INDEX [RecordID]
ON [Fields](
    [RecordID] ASC);

1 个答案:

答案 0 :(得分:1)

您没有提供表DDL,因此我不知道缺少哪些索引,请使用该信息更新您的问题。

我可以推荐别的东西,创建一个临时表来更新这个,如下所示:

CREATE TABLE TMP_FOR_UPDATE AS
(SELECT Data,f.RecordID
 FROM Subfields
 LEFT OUTER JOIN Fields f on f.FieldID = Subfields.FieldID
 WHERE f.TagNumber = '100' and Code = 'a');

UPDATE Records t
SET t.Author = (SELECT Data FROM TMP_FOR_UPDATE s
                WHERE s.RecordID = t.RecordID);

DROP TABLE TMP_FOR_UPDATE;