简体   繁体   中英

How can I make this SQL query more efficient?

I have a query trying to pull data from multiple tables but when I run it, it takes a really long time (So long I haven't even been able to wait long enough). I know it's extremely inefficient and wanted to get some input as to how it can be written better. Here it is:

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 

try this (or something like it)

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 

I ended up going with the following and was able to get the results I wanted:

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

I would try

  1. Click to run the estimated execution plan in SSMS and see if it suggests any missing indexes. I would think a non clustered index on lo.requesting_location and sample_date might help with the filter
  2. Also in desc index on p.patient_name may help with the performance of the order by.
  3. Try changing the IN date filter to "between '05/28/2015' and '05/29/2015'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM