使SQL查询更快

时间:2016-02-01 08:24:08

标签: sql sql-server performance

我有以下查询它工作正常,但需要时间。 而且桌子越来越大,所以需要花费越来越多的时间。 有没有办法让这个查询更快?  查询在这里:

SELECT        patient.bartar_id, patient.bartar_enteringthesystem, patient.bartar_proviencename, patient.bartar_cityname, patient.bartar_coloplastrepname, patient.bartar_consultorname, 
                     patient.bartar_provienceofsurgeryname, patient.bartar_cityofsurgeryname, patient.bartar_surgeryhospitalname, patient.bartar_doctorsname, patient.bartar_patientstatusname, patient.bartar_ostomytypename, 
                     patient.bartar_insurancetypename, patient.bartar_ostomytimename, patient.bartar_dateofseurgery, patient.bartar_deathhealeddate, patient.bartar_customercode, patient.bartar_name, patient.bartar_family, 
                     patient.bartar_age, patient.bartar_birthyear, patient.bartar_connectornursename, patient.bartar_hastakmiliname, patient.bartar_doctorsname AS Expr1, patient.bartar_patientstatusname AS Expr2, 
                     patient.bartar_generalstatusname, patient.new_description AS CCDesc, patient.bartar_visitname, patient.bartar_repvisitname, patient.bartar_salename, patient.bartar_customersstatusname, r.bartar_date, 
                     r.bartar_delay, r.bartar_nextcall, r.new_newcaller, r.bartar_bagPerMonth, r.bartar_brand, r.bartar_paste, r.bartar_bag, r.bartar_acc, r.bartar_insuranceinfo, r.bartar_pastePerMonth
FROM            Filteredbartar_newpaitient AS patient INNER JOIN

其余的就在这里:

(SELECT DISTINCT bartar_patientname,
(SELECT        TOP (1) bartar_date
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_date IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_date,
(SELECT        TOP (1) bartar_delay
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_delay IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_delay,
(SELECT        TOP (1) bartar_nextcall
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_nextcall IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_nextcall,
(SELECT        TOP (1) new_newcaller
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (new_newcaller IS NOT NULL)
ORDER BY bartar_date DESC) AS new_newcaller,
(SELECT        TOP (1) bartar_brandname
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_brand IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_brand,
(SELECT        TOP (1) bartar_pastename
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_paste IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_paste,
(SELECT        TOP (1) bartar_bagname
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_bag IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_bag,
(SELECT        TOP (1) bartar_accname
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_acc IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_acc,
(SELECT        TOP (1) bartar_pastepermonth
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_pastepermonth IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_pastePerMonth,
(SELECT        TOP (1) bartar_bagepermonth
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_bagepermonth IS NOT NULL)
ORDER BY bartar_date DESC) AS bartar_bagPerMonth,
(SELECT        TOP (1) bartar_insuranceinfoname
FROM            Filteredbartar_callcenterreport AS c2
WHERE        (bartar_patientname = cte.bartar_patientname) AND (bartar_insuranceinfo IS NOT NULL)ORDER BY bartar_date DESC) AS bartar_insuranceinfo
                           FROM            Filteredbartar_callcenterreport AS cte) AS r ON r.bartar_patientname = patient.bartar_newpaitientid
                           --where patient.bartar_enteringthesystem > @Fromdate and patient.bartar_enteringthesystem < @Todate
ORDER BY patient.bartar_enteringthesystem ASC

1 个答案:

答案 0 :(得分:1)

摆脱所有前1名子 加入或离开加入

def no_post_submitted?(user)
  user.post.count(0)
  "{user.name} has not submitted any posts yet."
end