简体   繁体   English

如何优化/重新考虑分区查询的排名

[英]How to optimize/rethink a Rank over Partition query

I have a fairly straight forward query that takes forever to run - roughly a second per row that's returned.我有一个相当直截了当的查询,它需要永远运行——大约每行返回一秒钟。 The issue is the rank over partition approach, but I'm not sure how to rewrite it (cross apply?)问题是分区方法的等级,但我不确定如何重写它(交叉应用?)

Any pointers would be appreciated.任何指针将不胜感激。

Query:询问:

Select orders.orderid
    ,prescriptions.patientid as Rx_PtID
    ,prescriptions.prescriptiontypeid
    ,prescriptions.prescriptionid
    ,letter_history.employeename
    ,RANK() OVER (PARTITION BY prescriptions.prescriptionid ORDER BY letter_history.letter_date ASC) AS orank --Assigns ID based on first printing
from letter_history,orders,prescriptions
where letter_history.description LIKE CONCAT('%', orders.orderid, '%')
    AND prescriptions.patientid = orders.patientid
    AND orders.ordertypeid = 999                    --Exam Orders Only
    AND CONVERT(varchar(10),orders.orderdate, 120) = CONVERT(varchar(10),prescriptions.rxdate, 120)
    AND CAST(orders.ordereddate as date) >= @DateFrom
    AND CAST(orders.ordereddate as date) <= @DateTo

The only reason the rank() seems slow is that all the data needs to be processed in order for it to return results. rank()看起来很慢的唯一原因是需要处理所有数据才能返回结果。

If you run the query without the rank() it will look fast -- because it is faster to return the first few results than the whole result set.如果您在没有rank()的情况下运行查询,它看起来会很快——因为返回前几个结果比返回整个结果集更快。 You could check this out by make this a subquery and using count(*) to see the performance.您可以通过将其设为子查询并使用count(*)来查看性能。

Also: Learn to use proper, explicit, standard JOIN syntax.另外:学习使用正确的、明确的、标准JOIN语法。 Never use commas in the FROM clause.切勿FROM子句中使用逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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