我非常沮丧地试图让SQLite查询完成执行,目前没有明智的想法。另外,我是编写SQL查询领域的新手,我还在学习数据库索引的概念等。
我正在将应用程序从Access迁移到SQLite,并且查询大约需要1分钟才能在Access中运行。查询如下(我也无法从SQLite迁移回Access):
SELECT pgDescription, oSimulationID, oSimRunID, ipParticipantID, oPeriod,
Some_Aggregation_Function(dpVal) FROM
(
SELECT pgDescription, oSimulationID, oSimRunID, ipParticipantID, oPeriod, dpVal,
ptPropertyTypeName FROM tmpPartArgs INNER JOIN
(
SELECT pgDescription, oSimulationID, oSimRunID, ipParticipantID,
oPeriod, dpVal, dpParticipantID, ptPropertyTypeName FROM
(
SELECT o.SimulationID AS oSimulationID, o.SimRunID AS oSimRunID,
o.Period AS oPeriod, dpVal, dpParticipantID, ptPropertyTypeName FROM Occurrence AS o INNER JOIN
(
SELECT * FROM
(
SELECT dp.Val AS dpVal, dp.SimulationID AS
dpSimulationID, dp.SimRunID AS dpSimRunID,dp.ParticipantID AS dpParticipantID,
dp.OccurrenceID AS dpOccurrenceID, pt.PropertyTypeName AS ptPropertyTypeName FROM
PropertyType AS pt INNER JOIN DynamicProperty AS dp ON pt.PropertyTypeID = dp.PropertyTypeID
) AS query0 WHERE query0.ptPropertyTypeName = \"" + args.propertyName + "\"
) AS query1 ON (o.SimulationID = query1.dpSimulationID)
AND (o.SimRunID = query1.dpSimRunID) AND (o.OccurrenceID = query1.dpOccurrenceID)
) AS query2 INNER JOIN
(
SELECT pg.Description AS pgDescription, ip.ParticipantID AS
ipParticipantID FROM ParticipantGroup AS pg INNER JOIN InitialParticipant AS ip ON pg.ParticipantGroupID = ip.ParticipantGroupID
) AS query3 ON query2.dpParticipantID = query3.ipParticipantID
) AS query4 ON tmpPartArgs.participantID = query4.dpParticipantID
) AS query5 INNER JOIN tmpSimArgs ON (query5.oSimRunID = tmpSimArgs.SimRunID) AND (query5.oSimulationID = tmpSimArgs.SimulationID)
GROUP BY query5.oSimulationID, query5.oSimRunID, query5.oPeriod,
query5.ipParticipantID, query5.pgDescription ORDER BY query5.oPeriod ASC;
以上查询在Access中运行大约一分钟。 query1,query2,query3,query4和query5的输出大约是1,200,000条记录。在SQLite中它没有完成执行,我不知道为什么?我不明白为什么上面的查询需要一分钟才能在Access中执行,并且没有在SQLite中完成执行。关于上述查询最令人困惑的事情是子查询号。 5,即
SELECT pgDescription, oSimulationID, oSimRunID, ipParticipantID, oPeriod, dpVal,
ptPropertyTypeName FROM tmpPartArgs INNER JOIN
(
SELECT pgDescription, oSimulationID, oSimRunID, ipParticipantID,
oPeriod, dpVal, dpParticipantID, ptPropertyTypeName FROM
(
SELECT o.SimulationID AS oSimulationID, o.SimRunID AS oSimRunID,
o.Period AS oPeriod, dpVal, dpParticipantID, ptPropertyTypeName FROM Occurrence AS o INNER JOIN
(
SELECT * FROM
(
SELECT dp.Val AS dpVal, dp.SimulationID AS
dpSimulationID, dp.SimRunID AS dpSimRunID,dp.ParticipantID AS dpParticipantID,
dp.OccurrenceID AS dpOccurrenceID, pt.PropertyTypeName AS ptPropertyTypeName FROM
PropertyType AS pt INNER JOIN DynamicProperty AS dp ON pt.PropertyTypeID = dp.PropertyTypeID
) AS query0 WHERE query0.ptPropertyTypeName = \"" + args.propertyName + "\"
) AS query1 ON (o.SimulationID = query1.dpSimulationID)
AND (o.SimRunID = query1.dpSimRunID) AND (o.OccurrenceID = query1.dpOccurrenceID)
) AS query2 INNER JOIN
(
SELECT pg.Description AS pgDescription, ip.ParticipantID AS
ipParticipantID FROM ParticipantGroup AS pg INNER JOIN InitialParticipant AS ip ON pg.ParticipantGroupID = ip.ParticipantGroupID
) AS query3 ON query2.dpParticipantID = query3.ipParticipantID
) AS query4 ON tmpPartArgs.participantID = query4.dpParticipantID
) AS query5
在几秒钟内运行,即语句'reader = com.ExecuteReader();'只需几秒钟即可运行,但如果我尝试使用上述查询返回的记录创建一个表,则表示无法完成。谁能告诉我为什么会发生这种情况?我的意思是,它可以在几秒钟内检索大约1,200,000条记录,但是当我尝试用这些记录创建一个表时出现问题,即。 CREATE TABLE query5 AS SELECT pgDescription,oSimulationID,oSimRunID,ipParticipantID,...未完成执行。请帮忙!任何类型的指针都会非常有用。
我想创建一个名为query5的临时表的唯一原因是因为我可以在该临时表上创建索引,即query5。我假设我的原始查询(即第一段代码)由于索引问题而无法完成。所以我的另一个问题是如何在一分钟内有效地在表格中插入大约1,200,000条记录?
表格具有以下结构:
发生
CREATE TABLE "Occurrence" ("SimulationID" smallint(5) NOT NULL DEFAULT 0, "SimRunID" smallint(5) NOT NULL DEFAULT 0, "OccurrenceID" int(10) NOT NULL DEFAULT 0, "OccurrenceTypeID" smallint(5) NOT NULL DEFAULT 0, "Period" smallint(5) NOT NULL DEFAULT 0, "HasSucceeded" bool NOT NULL DEFAULT 0, PRIMARY KEY ("SimulationID", "SimRunID", "OccurrenceID"))
Occurrence表有大约740,000条记录
DynamicProperty
CREATE TABLE "DynamicProperty" ("SimulationID" smallint(5) NOT NULL DEFAULT 0 ,"SimRunID" smallint(5) NOT NULL DEFAULT 0 ,"ParticipantID" int(10) NOT NULL DEFAULT 0 ,"PropertyTypeID" smallint(5) NOT NULL DEFAULT 0 ,"OccurrenceID" int(10) NOT NULL DEFAULT 0 ,"Val" DOUBLE DEFAULT 0 , PRIMARY KEY ("SimulationID", "SimRunID", "ParticipantID", "PropertyTypeID", "OccurrenceID") )
DynamicProperty表有大约6,250,000条记录
属性类型
CREATE TABLE `PropertyType` (`PropertyTypeID` smallint (5),`PropertyTypeName` varchar (50),`IsAgentProperty` bool NOT NULL,`IsActionProperty` bool NOT NULL, `IsKnowledgeItemProperty` bool NOT NULL,`IsStatic` bool NOT NULL,`IsKnowledgeStoreProperty` bool NOT NULL,`IsSimulationProperty` bool NOT NULL,`IsAssignable` bool NOT NULL, PRIMARY KEY(`PropertyTypeID`))
PropertyType有大约50条记录。
InitialParticipant
CREATE TABLE "InitialParticipant" ("ParticipantID" smallint(5) PRIMARY KEY NOT NULL DEFAULT 0, "ParticipantTypeID" smallint(5) NOT NULL DEFAULT 0, "ParticipantGroupID" smallint(5) NOT NULL DEFAULT 0)
InitialParticipant有大约120条记录
ParticipantGroup
CREATE TABLE `ParticipantGroup` (`ParticipantGroupID` smallint (5),`ParticipantGroupTypeID` smallint (5),`Description` varchar (50), PRIMARY KEY
(ParticipantGroupID
))
ParticipantGroup有大约25条记录。
tmpSimArgs
CREATE TABLE tmpSimArgs (SimulationID smallint(5), SimRunID int(10))
tmpSimArgs有大约20条记录。
tmpPartArgs
CREATE TABLE tmpPartArgs(participantID INT)
tmpPartArgs有大约80条记录
数据库具有以下索引:
各表的所有主键如上所示。
CREATE INDEX tmpSimArgs_SimulationID_idx ON tmpSimArgs(SimulationID ASC);
CREATE INDEX tmpSimArgs_SimRunID_idx ON tmpSimArgs(SimRunID ASC);
CREATE INDEX tmpPartArgs_participantID_idx ON tmpPartArgs(participantID ASC);
请帮助!!