开发者

design for DB when needed to know lastest for something

let's say I have the following objects:

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

for each site, there is a list of tests that are registered for him.

now I want to know for each site version the latest testresults for them, grouped by idTest. and also those who are not tested, with null as value.(b)

I've implimented a DB for this.

but I have trouble to get the query I've listed (b) , it takes a considerbly long time.

I'm thinking of remodeling it. any advices will help

the new one will consist of a table LATEST that will act as a cache,

so I'll use triggers on insert to update the right row,

and will select from it.

is that right?

should I remodel this?

query:

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`.`igno开发者_开发知识库red` = 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)))


Try it like this:

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

Also, you definitely want to create an index on dash.testresult (idTest_id, idSiteVersion_id, timeEnd) if you don't already have one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜