SQL Performance从同一个表中多次排除

时间:2013-08-16 22:33:38

标签: sql performance

我有一张桌子,我有一个人员列表,假设我有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
)

你可以看到我在这里进行了多级过滤......

  • 1我得到了t1的人。[dthrfinmouvement] ='4000-01-01'
  • 第二,我得到的人有另一个标准排除第一组
  • 第三,我让人们有另一个标准排除第一和第二 第二组
  • 等。

当我到达第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 |
+------------------+-------------------------------+------------------+------------------+-----------------------+

感谢。

1 个答案:

答案 0 :(得分:0)

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

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