如何使此SQL查询更有效?

时间:2015-05-27 21:49:08

标签: sql sql-server performance optimization

我有一个查询试图从多个表中提取数据,但是当我运行它时,它需要很长时间(很长一段时间我甚至没能等待足够长的时间)。我知道它效率非常低,并希望得到一些关于如何更好地编写的输入。这是:

SELECT 
        P.patient_name, 
        LOH.patient_id, 
        LOH.requesting_location, 
        LOH.sample_date,
        LOH.lab_doing_work,
        L.location_name, 
        LOD.test_code, 
        LOD.test_rdx,
        LSR.tube_type 
FROM 
        mis_db.dbo.lab_order_header AS LOH, 
        mis_db.dbo.patient AS P, 
        mis_db.dbo.lab_order_detail AS LOD, 
        mis_db.dbo.lab_sample_rule AS LSR,
        mis_db.dbo.location AS L
WHERE 
        LOH.requesting_location = '000839' AND
        LOH.lab_order_id = LOD.lab_order_id AND
        LOH.sample_date IN ('05/28/2015', '05/29/2015') 
        --LOH.patient_id = LOD.patient_id 
        --LOD.sample_date = LOH.sample_date 
ORDER BY 
        P.patient_name DESC 

3 个答案:

答案 0 :(得分:0)

尝试这个(或类似的东西)

SELECT P.patient_name, 
    lo.patient_id, lo.requesting_location, 
    lo.sample_date, lo.lab_doing_work,
    l.location_name, d.test_code, d.test_rdx,
    d.tube_type 
FROM mis_db.dbo.lab_order_header lo 
    join mis_db.dbo.patient p on p.patient_id = lo.Patient_id
    join mis_db.dbo.lab_order_detail d on d.lab_order_id = lo.lab_order_id
    join mis_db.dbo.lab_sample_rule r on r.rule_id = lo.ruleId   -- ???? 
    join mis_db.dbo.location  l on l.locationid = lo.requesting_location  
WHERE lo.requesting_location = '000839' AND
     lo.sample_date IN ('05/28/2015', '05/29/2015') 
ORDER BY p.patient_name DESC 

答案 1 :(得分:0)

我最终选择了以下内容,并且能够获得我想要的结果:

SELECT       LOH.patient_id,
               patient_name,
                         [mis_db_rpt].[common].[string_date_format](LOD.sample_date) AS
               [Draw Date],
               test_description,
               LOD.test_code,
               LOH.lab_doing_work,
               tube_type,
               L.short_name
FROM                   [mis_db].[dbo].[lab_order_header] 
               LOH
      INNER JOIN
                         [mis_db].[dbo].[lab_order_detail] 
               LOD 
      ON     LOH.lab_order_id = LOD.lab_order_id
      INNER JOIN
                         [mis_db].[dbo].[patient] 
               P 
      ON     P.patient_id = LOD.patient_id         
      INNER JOIN
                         [mis_db].[dbo].[sample_tube]
               ST
      ON     LOD.sample_id = ST.sample_id
      INNER JOIN
                         [mis_db].[dbo].[location] AS
               L
      ON     LOH.lab_doing_work = L.location_id
      INNER JOIN
                         [mis_db].[dbo].[lab_test] AS
               LT
      ON     LOD.test_code = LT.test_code         
WHERE       LOH.requesting_location = '000839' AND
LOD.sample_date IN ('05/28/2015', '05/29/2015') 
ORDER BY  LOD.sample_date,
                         patient_name,
                         LOD.patient_id,
                         test_description

答案 2 :(得分:0)

我会尝试

  1. 单击以在SSMS中运行估计的执行计划,并查看它是否表明任何缺失的索引。我认为lo.requesting_location和sample_date上的非聚集索引可能有助于过滤器
  2. 同样在p.patient_name上的desc索引可能有助于按顺序执行订单。
  3. 尝试将IN日期过滤器更改为“介于'05 / 28/2015'和'05 / 29/2015'之间