如何在没有存在的情况下对大型数据库执行查询

时间:2014-01-28 09:10:25

标签: mysql sql optimization join subquery

MYSQL 5.1.52-community 我有一个250万行的大桌子:

我写了这个查询来解析数据:

SELECT dem.iddem,
       rrbc.p0012 AS libdemandeur,
       rrbp.p0012 AS libaffectation,
       dem.regate,
       dem.bp,
       dem.typeabsence,
       dem.datedeb,
       dem.datefin
FROM   demandes dem
       LEFT OUTER JOIN regate_regate rrbp
                    ON rrbp.p0016 = '038'
                       AND dem.bp = rrbp.regate
       LEFT OUTER JOIN regate_regate rrbc
                    ON ( rrbc.p0016 = '038'
                         AND dem.regate = rrbc.regate )
       INNER JOIN planning_demandes pd
               ON dem.iddem = pd.id_dem
WHERE  1
       AND EXISTS(SELECT 1
                  FROM   planning_demandes b
                  WHERE  pd.id_dem = b.id_dem
                         AND idrh IS NOT NULL
                         AND idrh <> 'férié'
                         AND idrh <> 'Pferié')
       AND EXISTS(SELECT 1
                  FROM   planning_demandes c
                  WHERE  pd.id_dem = c.id_dem
                         AND idrh IS NULL)
       AND dem.etat NOT IN ( '0', '1', '2', '7', '8' )
GROUP  BY iddem
ORDER  BY dem.datedeb ASC

我想知道是否有人想快速加载此请求:

也许子查询在大桌子上是个坏主意? 我创建了这个索引:在planning_demandes上创建索引IDX_DEM_IDRH(id_dem,idrh);

EXPLAIN

INDEX OF PLANNING_DEMANDES AND DEMANDES

1 个答案:

答案 0 :(得分:0)

请检查此查询的解释计划,并验证它是否使用索引IDX_DEM_IDRH。还要检查哪个连接占用了最长时间。

语法:

explain plan for <your select query>;
select * from table(dbms_xplan);

我认为你应该在planning_demandes.id_dem和planning_demandes.idrh上有单独的索引。还要考虑demandes.iddem的索引。