假设我有以下对象:
Site:
idSite,
name
and other static data.
Test:
idTest
idSite
name
and other static data.
Site Version:
idSiteVersion,
idSite
name of version
Test Result:
ןdTestResult
idSiteVersion,
idTest,
timeEnded
对于每个站点,都有一个为他注册的测试列表。
现在我想知道每个site version
最新的testresults
,按idTest
分组。
以及未经测试的人,以null为值。(b)
我为此建立了一个数据库。
但是我很难得到我列出的查询(b),这需要相当长的时间。
我正在考虑改造它。任何建议都会有所帮助
新的将包含一个表LATEST
,它将充当缓存,
所以我将在insert上使用触发器来更新右行,
并将从中进行选择。
是吗?
我应该改造吗?
查询:
select `tr1`.`id` AS `id`,
`dsv`.`idSite_id` AS `idSite`,
`tr1`.`idSiteVersion_id` AS `idSiteVersion`,
`tr1`.`idTest_id` AS `idTest`,
`tr1`.`result` AS `result`,
`tr1`.`timeStarted` AS `timeStarted`,
from
`dash`.`testresult` `tr1`
join `dash`.`siteversion` `dsv` on `dsv`.`id` = `tr1`.`idSiteVersion_id`
join `dash`.`test` `dtest` on `dtest`.`id` = `tr1`.`idTest_id`
where `dtest`.`ignored` = 0 and `tr1`.`timeEnd` =
( select max(`tr2`.`timeEnd`) from `dash`.`testresult` `tr2`
where
((`tr2`.`idTest_id` = `tr1`.`idTest_id`) and
(`tr2`.`idSiteVersion_id` = `tr1`.`idSiteVersion_id`) and
(`tr2`.`timeEnd` is not null)))
答案 0 :(得分:0)
试试这样:
SELECT
tr1.id AS id,
dsv.idSite_id AS idSite,
tr1.idSiteVersion_id AS idSiteVersion,
tr1.idTest_id AS idTest,
tr1.result AS result,
tr1.timeStarted AS timeStarted,
FROM
dash.testresult AS tr1
NATURAL JOIN (
SELECT idTest_id, idSiteVersion_id, MAX(timeEnd) AS timeEnd
FROM dash.testresult
WHERE timeEnd IS NOT NULL
GROUP BY idTest_id, idSiteVersion_id
) AS tr2
JOIN dash.siteversion AS dsv
ON dsv.id = tr1.idSiteVersion_id
JOIN dash.test AS dtest
ON dtest.id = tr1.idTest_id AND dtest.ignored = 0
此外,如果您还没有索引,那么您肯定想在dash.testresult (idTest_id, idSiteVersion_id, timeEnd)
上创建一个索引。