gpt4 book ai didi

sql - 表变大后,更新查询将永远不会完成

转载 作者:行者123 更新时间:2023-12-03 19:35:03 25 4
gpt4 key购买 nike

当表具有几百甚至几千行时,此update语句运行得很好,但是一旦子字段表中的行达到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);

最佳答案

您未提供表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;

关于sql - 表变大后,更新查询将永远不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com