0

我有一张桌子,上面有一份人员名单,假设我在这张桌子上列出了 100 个人

我需要过滤掉使用不同标准的人并将他们分组,问题是当我开始在第 4 到第 5 级排除时,出现性能问题并且速度变慢

with lst_tous_movements as (
    select
        t1.refid_eClinibase
        t1.[dthrfinmouvement]
        t1.[unite_service_id]
        t1.[unite_service_suiv_id]
    from sometable t1
)

,lst_patients_hospitalisés as (
    select distinct
        t1.refid_eClinibase
    from lst_tous_movements t1
    where
    t1.[dthrfinmouvement] = '4000-01-01'
)
,lst_patients_admisUIB_transferes as (
    select distinct
        t1.refid_eClinibase
    from lst_tous_movements t1
    left join lst_patients_hospitalisés t2 on t1.refid_eClinibase = t2.refid_eClinibase
    where
    t1.[unite_service_id] = 4
    and t1.[unite_service_suiv_id] <> 0 
    and t2.refid_eClinibase is null
)
,lst_patients_admisUIB_nonTransferes as (
    select distinct
        t1.refid_eClinibase
    from lst_tous_movements t1
    left join lst_patients_admisUIB_transferes t2 on t1.refid_eClinibase = t2.refid_eClinibase
    left join lst_patients_hospitalisés t3 on t1.refid_eClinibase = t3.refid_eClinibase
    where

    t1.[unite_service_id] = 4
    and t1.[unite_service_suiv_id] = 0  
    and t2.refid_eClinibase is null
    and t3.refid_eClinibase is null
)
,lst_patients_autres as (
    select distinct
        t1.refid_eClinibase
    from lst_patients t1
    left join lst_patients_admisUIB_transferes t2 on t1.refid_eClinibase = t2.refid_eClinibase 
    left join lst_patients_hospitalisés t3 on t1.refid_eClinibase = t3.refid_eClinibase
    left join lst_patients_admisUIB_nonTransferes t4 on t1.refid_eClinibase = t4.refid_eClinibase
    where
    t2.refid_eClinibase is null
    and t3.refid_eClinibase is null
    and t4.refid_eClinibase is null
)

如您所见,我在这里进行了多级过滤...

  • 第一个我得到了 t1.[dthrfinmouvement] = '4000-01-01' 的人
  • 第二,我得到了不包括第一组的其他标准的人
  • 第三,我得到的人还有另一个标准,不包括第一组和第二组
  • ETC..

当我到达第 4 级时,我的查询需要 6 - 10 秒才能完成

有什么办法可以加快速度吗?

这是我正在使用的数据集:

+------------------+-------------------------------+------------------+------------------+-----------------------+
| refid_eClinibase | nodossierpermanent_eClinibase | dthrfinmouvement | unite_service_id | unite_service_suiv_id |
+------------------+-------------------------------+------------------+------------------+-----------------------+
|            25611 | P0017379                      | 2013-04-27       |               58 |                     0 |
|            25611 | P0017379                      | 2013-05-02       |                4 |                     2 |
|            25611 | P0017379                      | 2013-05-18       |                2 |                     0 |
|            85886 | P0077918                      | 2013-04-10       |               58 |                     0 |
|            85886 | P0077918                      | 2013-05-06       |                6 |                    12 |
|            85886 | P0077918                      | 4000-01-01       |               12 |                     0 |
|            91312 | P0083352                      | 2013-07-24       |                3 |                    14 |
|            91312 | P0083352                      | 2013-07-24       |               14 |                     3 |
|            91312 | P0083352                      | 2013-07-30       |                3 |                     8 |
|            91312 | P0083352                      | 4000-01-01       |                8 |                     0 |
|            93835 | P0085879                      | 2013-04-30       |               58 |                     0 |
|            93835 | P0085879                      | 2013-05-07       |                4 |                     2 |
|            93835 | P0085879                      | 2013-05-16       |                2 |                     0 |
|            93835 | P0085879                      | 2013-05-22       |               58 |                     0 |
|            93835 | P0085879                      | 2013-05-24       |                4 |                     0 |
|            93835 | P0085879                      | 2013-05-31       |               58 |                     0 |
|            93836 | P0085880                      | 2013-05-20       |               58 |                     0 |
|            93836 | P0085880                      | 2013-05-22       |                4 |                     2 |
|            93836 | P0085880                      | 2013-05-31       |                2 |                     0 |
|            97509 | P0089576                      | 2013-04-09       |               58 |                     0 |
|            97509 | P0089576                      | 2013-04-11       |                4 |                     0 |
|           102787 | P0094886                      | 2013-04-08       |               58 |                     0 |
|           102787 | P0094886                      | 2013-04-11       |                4 |                     2 |
|           102787 | P0094886                      | 2013-05-21       |                2 |                     0 |
|           103029 | P0095128                      | 2013-04-04       |               58 |                     0 |
|           103029 | P0095128                      | 2013-04-10       |                4 |                     1 |
|           103029 | P0095128                      | 2013-05-03       |                1 |                     0 |
|           103813 | P0095922                      | 2013-07-02       |               58 |                     0 |
|           103813 | P0095922                      | 2013-07-03       |                4 |                     6 |
|           103813 | P0095922                      | 2013-08-14       |                6 |                     0 |
|           105106 | P0097215                      | 2013-08-09       |               58 |                     0 |
|           105106 | P0097215                      | 2013-08-13       |                4 |                     0 |
|           105106 | P0097215                      | 2013-08-14       |               58 |                     0 |
|           105106 | P0097215                      | 4000-01-01       |                4 |                     0 |
|           106223 | P0098332                      | 2013-06-11       |                1 |                     0 |
|           106223 | P0098332                      | 2013-08-01       |               58 |                     0 |
|           106223 | P0098332                      | 4000-01-01       |                1 |                     0 |
|           106245 | P0098354                      | 2013-04-02       |               58 |                     0 |
|           106245 | P0098354                      | 2013-05-24       |               58 |                     0 |
|           106245 | P0098354                      | 2013-05-29       |                4 |                     1 |
|           106245 | P0098354                      | 2013-07-12       |                1 |                     0 |
|           106280 | P0098389                      | 2013-04-07       |               58 |                     0 |
|           106280 | P0098389                      | 2013-04-09       |                4 |                     0 |
|           106416 | P0098525                      | 2013-04-19       |               58 |                     0 |
|           106416 | P0098525                      | 2013-04-23       |                4 |                     0 |
|           106444 | P0098553                      | 2013-04-22       |               58 |                     0 |
|           106444 | P0098553                      | 2013-04-25       |                4 |                     0 |
|           106609 | P0098718                      | 2013-05-08       |               58 |                     0 |
|           106609 | P0098718                      | 2013-05-10       |                4 |                    11 |
|           106609 | P0098718                      | 2013-07-24       |               11 |                    12 |
|           106609 | P0098718                      | 4000-01-01       |               12 |                     0 |
|           106616 | P0098725                      | 2013-05-09       |               58 |                     0 |
|           106616 | P0098725                      | 2013-05-09       |                4 |                     1 |
|           106616 | P0098725                      | 2013-07-27       |                1 |                     0 |
|           106698 | P0098807                      | 2013-05-16       |               58 |                     0 |
|           106698 | P0098807                      | 2013-05-22       |                4 |                     6 |
|           106698 | P0098807                      | 2013-06-14       |                6 |                     1 |
|           106698 | P0098807                      | 2013-06-28       |                1 |                     0 |
|           106714 | P0098823                      | 2013-05-20       |               58 |                     0 |
|           106714 | P0098823                      | 2013-05-21       |               58 |                     0 |
|           106714 | P0098823                      | 2013-05-24       |               58 |                     0 |
|           106729 | P0098838                      | 2013-05-21       |               58 |                     0 |
|           106729 | P0098838                      | 2013-05-23       |                4 |                     1 |
|           106729 | P0098838                      | 2013-06-03       |                1 |                     0 |
|           107038 | P0099147                      | 2013-06-25       |               58 |                     0 |
|           107038 | P0099147                      | 2013-06-28       |                4 |                     1 |
|           107038 | P0099147                      | 2013-07-04       |                1 |                     0 |
|           107038 | P0099147                      | 2013-08-13       |               58 |                     0 |
|           107038 | P0099147                      | 2013-08-15       |                4 |                     6 |
|           107038 | P0099147                      | 4000-01-01       |                6 |                     0 |
|           107082 | P0099191                      | 2013-06-29       |               58 |                     0 |
|           107082 | P0099191                      | 2013-07-04       |                4 |                     6 |
|           107082 | P0099191                      | 2013-07-19       |                6 |                     0 |
|           107157 | P0099267                      | 4000-01-01       |               13 |                     0 |
|           107336 | P0099446                      | 4000-01-01       |                6 |                     0 |
+------------------+-------------------------------+------------------+------------------+-----------------------+

谢谢。

4

1 回答 1

0

很难从问题中准确理解您的所有规则是什么,但一般方法应该是将“分组”列添加到使用CASE语句对人员进行分类的单个查询中。

CASE 中的条件按顺序评估,因此如果满足第一个条件,则甚至不会为该行评估后续条件。

这是一些可以帮助您入门的代码....

select  t1.refid_eClinibase
        ,t1.[dthrfinmouvement]
        ,t1.[unite_service_id]
        ,t1.[unite_service_suiv_id]
        CASE    WHEN [dthrfinmouvement] = '4000-01-01' THEN 'Group1 Label'
                WHEN condition2 = something THEN 'Group2 Label'
                ....
                WHEN conditionN = something THEN 'GroupN Label'
                ELSE 'Catch All Label'
        END as person_category
from sometable t1
于 2013-08-16T23:17:07.707 回答