有效处理/排除大型查询

时间:2012-07-31 21:44:20

标签: c# python sql sql-server sql-server-2008

我需要在我的巨大查询中添加另外两个左外连接。这对我来说变得困难,因为查询太大,我现在无法处理。我想知道是否有办法将查询自动转换为维恩图。

那里有工具可以进行查询并绘制代表查询的venndiagrams吗?

我在sql server,c#或python中寻找解决方案?

是否有以图形方式表示查询的自动方式?

WITH cte_biggie 
     AS (SELECT 
     LEFT('I'+Replicate(' ', 2), 2) 
     + LEFT(CONVERT(VARCHAR, Getdate(), 110)+Replicate(' ', 10), 10) 
     + LEFT('MILLENN'+Replicate(' ', 16), 16) 
     + LEFT('TERR'+Replicate(' ', 12), 12) 
     + LEFT(' '+Replicate(' ', 16), 16) 
     + LEFT('IMS'+Replicate(' ', 8), 8) 
     + LEFT(' '+Replicate(' ', 15), 15) 
     + LEFT( LEFT( LEFT(Isnull(MLIS.requestor_first_name, 'XXX'), 
     Charindex( 
     ' ' 
        , Isnull 
     (MLIS.requestor_first_name, 'XXX') + ' ' ) - 1), Charindex( ' ', 
     LEFT(Isnull(MLIS.requestor_first_name, 'XXX'), Charindex( ' ', 
     Isnull(MLIS.requestor_first_name, 'XXX') + ' ' ) - 1) + 
     ' ' ) - 1) +Replicate(' ', 25), 25) 
     + LEFT(' '+Replicate(' ', 20), 20) 
     + LEFT( LEFT( LEFT(Isnull(MLIS.requestor_last_name, ''), 
     Charindex( 
     ',', 
        Isnull( 
     MLIS.requestor_last_name, '') + ',' ) - 1), Charindex( ',', 
     LEFT(Isnull(MLIS.requestor_last_name, ''), Charindex( ',', 
     Isnull(MLIS.requestor_last_name, '') + ',' ) - 1) + 
     ',' ) - 1) +Replicate(' ', 25), 25) 
     + LEFT(' '+Replicate(' ', 5), 5) 
     + LEFT(' '+Replicate(' ', 3), 3) 
     + LEFT(' '+Replicate(' ', 3), 3) 
     + LEFT(' '+Replicate(' ', 3), 3) 
     + LEFT(Isnull(MLIS.requestor_npi, '')+Replicate(' ', 12), 12) 
     + LEFT('13'+Replicate(' ', 2), 2) 
     + LEFT(' '+Replicate(' ', 15), 15) 
     + LEFT(' '+Replicate(' ', 30), 30) 
     + LEFT(' '+Replicate(' ', 15), 15) 
     + LEFT(' '+Replicate(' ', 4), 4) 
     + LEFT(' '+Replicate(' ', 2), 2) 
     + LEFT(' '+Replicate(' ', 2), 2) 
     + LEFT(' '+Replicate(' ', 1), 1) 
     + LEFT(' '+Replicate(' ', 10), 10) 
     + LEFT(' '+Replicate(' ', 2), 2) 
     + LEFT(' '+Replicate(' ', 20), 20) 
     + LEFT(' '+Replicate(' ', 10), 10) 
     + LEFT(' '+Replicate(' ', 4), 4) 
     + LEFT(' '+Replicate(' ', 100), 100) 
     + LEFT(' '+Replicate(' ', 20), 20) 
     + LEFT(' '+Replicate(' ', 12), 12) 
     + LEFT(' '+Replicate(' ', 2), 2) 
     + LEFT(' '+Replicate(' ', 1), 1) 
     + LEFT(Isnull(SplitAddresses.address1, '')+Replicate(' ', 50), 50) 
     + LEFT(Isnull(SplitAddresses.address2, '')+Replicate(' ', 50), 50) 
     + LEFT(' '+Replicate(' ', 50), 50) 
     + LEFT(' '+Replicate(' ', 50), 50) 
     + LEFT(Isnull(CLIENT.mlis_client_city, 'XXX')+Replicate(' ', 30), 30) 
     + LEFT(Isnull(CLIENT.mlis_client_state, '')+Replicate(' ', 15), 15) 
     + LEFT(Isnull(CLIENT.mlis_client_zipcode, 'XXX')+Replicate(' ', 10), 10) 
     + LEFT(' '+Replicate(' ', 20), 20) 
     + LEFT(' '+Replicate(' ', 9), 9) 
     + LEFT(' '+Replicate(' ', 7), 7) 
     + LEFT(' '+Replicate(' ', 3), 3) 
     + LEFT(' '+Replicate(' ', 1), 1) 
     + LEFT('MILLENN'+Replicate(' ', 10), 10) 
     + LEFT('1'+Replicate(' ', 1), 1) 
     + LEFT('DrScrub'+Replicate(' ', 15), 15) 
     + LEFT(' '+Replicate(' ', 10), 10) 
     + LEFT(' '+Replicate(' ', 101), 101) PHYSICIAN_INFO 
         --+LEFT(' '+REPLICATE(' ',1),1)  
         FROM   (SELECT DISTINCT mlis_practice_id, 
                                 requestor_npi, 
                                 requestor_first_name, 
                                 requestor_last_name 
                 FROM   complete 
                 WHERE  
                         Len(requestor_last_name) > 0 
                         ) MLIS 
                LEFT JOIN (SELECT * 
                           FROM   millennium_dw..d_physician 
                           WHERE  received_ims_information = 1) PHYSICIAN 
                       ON MLIS.requestor_npi = Cast(PHYSICIAN.physician_npi AS 
                                                    VARCHAR) 
                --we can remove this cast later 
                LEFT JOIN (SELECT * 
                           FROM   millennium_dw..d_client 
                           WHERE  rec_active_flg = 1) CLIENT 
                       ON MLIS.mlis_practice_id = CLIENT.client_id 
                LEFT JOIN (SELECT DISTINCT T.mlis_client_address_1, 
                                           T.client_id, 
                                           LEFT(T.mlis_client_address_1, Isnull( 
                                           SplitAddresses.pos - 1, 
                                           2147483647)) 
                                           Address1, 
                                           Substring(T.mlis_client_address_1, 
                                           SplitAddresses.pos + 1, 
                                           2147483647) Address2 
                           FROM   d_client T 

                                  OUTER apply (SELECT TOP 1 NULLIF( 
                                              Patindex(delimiter, 
                                              T.mlis_client_address_1), 0 
                                                            ) Pos 
                                               FROM 
                                  ( VALUES ('% Suite %'), 
                                           ('% Ste %'), 
                                           ('% Bldg %'), 
                                           ('% Building %'), 
                                           ('%#%') ) SplitAddresses 
                                  ( 
                                  delimiter) 
                                               WHERE  T.mlis_client_address_1 
                                                      LIKE 
                                                      splitaddresses.delimiter 
                                               ORDER  BY pos) SplitAddresses) 
                          SplitAddresses 
                       ON SplitAddresses.client_id = CLIENT.client_id 
         WHERE  PHYSICIAN.physician_npi IS NULL 
                AND CLIENT.received_mlis_information = 1
                AND LEN(address1)>0
                AND LEN(CLIENT.mlis_client_state)>0
                AND address1!='NO ADDRESS'
                ) 
--AND MLIS.REQUESTOR_NPI IS NOT NULL) 
SELECT * 
FROM   cte_biggie 

1 个答案:

答案 0 :(得分:1)

可以一次查询CTE:

DECLARE @start_date AS DATETIME = '2012-02-02'; 
WITH L0 AS (SELECT 1 AS C UNION ALL SELECT 1),       --      2 
     L1 AS (SELECT L0.C FROM L0 CROSS JOIN L0 AS B), --      4 
     L2 AS (SELECT L1.C FROM L1 CROSS JOIN L1 AS B), --     16 
     L3 AS (SELECT L2.C FROM L2 CROSS JOIN L2 AS B), --    256 
     L4 AS (SELECT L3.C FROM L3 CROSS JOIN L3 AS B), --  65536 
      N AS (SELECT ROW_NUMBER() OVER(ORDER BY L4.C) AS n FROM L4), 
      D AS (SELECT DATEADD(SECOND, n - 1, @start_date) AS d, n FROM N) 
SELECT d from D where n <= (60 * 60 * 24) 

请注意,最终SELECT可以使用任何或所有CTE中间结果。这可以大大简化开发和测试。

编辑:您的情况就是这样。

WITH 
  MLIS as ( -- Description of what this piece is supposed to mean.
    SELECT DISTINCT mlis_practice_id, requestor_npi, requestor_first_name, requestor_last_name  
      FROM complete  
      WHERE Len(requestor_last_name) > 0 ),
  Physician as ( -- Description of what this piece is supposed to mean.
    SELECT *
      FROM millennium_dw..d_physician  
      WHERE received_ims_information = 1 ),
  Client as ( -- Description of what this piece is supposed to mean.
    SELECT *  
      FROM millennium_dw..d_client  
      WHERE rec_active_flg = 1 ), 
  cte_biggie as ( -- Something I've totally bodged that should use the above CTEs.
    SELECT LEFT('I'+Replicate(' ', 2), 2), ... )
SELECT * from CTE_Biggie