连接表导致查询时间呈指数增长

时间:2012-08-20 20:30:24

标签: sql sql-server sql-server-2008 tsql

到目前为止,我的查询已经运行了30分钟:

SELECT 
   d.accn_id,
   cast(d.load_date as DATE) as LoadDate,
   cast (d.final_rpt_date as DATE) as FinalReportDate,
   sum(p.paid_amt) as SumPaidAmt,
   payors.PAYOR_ID
FROM accn_demographics d
    JOIN accn_payments p
        ON d.ACCN_ID=p.ACCN_ID    
    JOIN accn_payors payors
        ON payors.X_PAYOR_ID=p.X_PAYMENT_PAYOR_ID
WHERE
      p.POSTED = 'y'
  AND p.PMT_DATE between '20120401' and '20120430'    
GROUP BY
    d.accn_id,
    d.load_date,
    d.final_rpt_date,
    payors.PAYOR_ID

在我添加此表之前:

accn_payors

查询花了几分钟,但在添加此表后,我仍在等待30分钟后。

这是3个表格:

USE [zzzDataEOMTestingApril]
GO

/****** Object:  Table [dbo].[accn_payors]    Script Date: 08/20/2012 13:28:20 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[accn_payors](
    [ACCN_ID] [varchar](40) NULL,
    [PAYOR_PRIORITY] [int] NULL,
    [PAYOR_ID] [varchar](15) NULL,
    [PAYOR_NAME] [varchar](40) NULL,
    [GROUP_ID] [varchar](40) NULL,
    [PLAN_ID] [varchar](40) NULL,
    [SUBSCRIBER_ID] [varchar](40) NULL,
    [INSURED_RELATIONSHIP] [varchar](6) NULL,
    [INSURED_L_NAME] [varchar](40) NULL,
    [INSURED_F_NAME] [varchar](40) NULL,
    [INSURED_HOME_PHN] [varchar](40) NULL,
    [INSURED_WORK_PHN] [varchar](40) NULL,
    [INSURED_ADDR1] [varchar](60) NULL,
    [INSURED_ADDR2] [varchar](60) NULL,
    [INSURED_CITY] [varchar](60) NULL,
    [INSURED_STATE] [varchar](2) NULL,
    [INSURED_ZIPCODE] [varchar](10) NULL,
    [PAID_IN_FULL] [varchar](1) NULL,
    [CLAIM_COMMENT] [varchar](4000) NULL,
    [OTHER_INFO1] [varchar](4000) NULL,
    [OTHER_INFO2] [varchar](4000) NULL,
    [OTHER_INFO3] [varchar](4000) NULL,
    [OTHER_INFO4] [varchar](4000) NULL,
    [INTERNAL_NOTES] [varchar](4000) NULL,
    [SYSTEM_ADDED_PAYOR] [varchar](1) NULL,
    [ELIG_OK] [varchar](1) NULL,
    [ELIG_STATUS] [varchar](40) NULL,
    [ELIG_SERVICE] [varchar](40) NULL,
    [ELIG_VERIF_ID] [varchar](15) NULL,
    [AUD_REC_ID] [int] NOT NULL,
    [INSURED_DOB] [varchar](50) NULL,
    [INSURED_SEX] [varchar](3) NULL,
    [EMPLOYER_NAME] [varchar](40) NULL,
    [EMPLOYER_ADDR1] [varchar](60) NULL,
    [EMPLOYER_ADDR2] [varchar](60) NULL,
    [EMPLOYER_CITY] [varchar](60) NULL,
    [EMPLOYER_STATE] [varchar](2) NULL,
    [EMPLOYER_ZIPCODE] [varchar](10) NULL,
    [EMPLOYER_COUNTRY] [varchar](40) NULL,
    [EMPLOYER_PHONE] [varchar](40) NULL,
    [EMPLOYMENT_STATUS] [varchar](40) NULL,
    [GROUP_NAME] [varchar](40) NULL,
    [X_PAYOR_ID] [int] NULL,
    [AUDIT_DATE] [varchar](50) NULL,
    [OCCURRENCE_CODE] [varchar](30) NULL,
    [INSURED_SSN] [int] NULL,
    [OCCURRENCE_CODE_DATE] [datetime] NULL,
    [DELAY_REASON_CODE] [varchar](15) NULL,
    [CASE_ID] [varchar](60) NULL,
    [PAYOR_CODE_TYPE] [varchar](400) NULL,
    [AUTH_NUM] [varchar](40) NULL,
 CONSTRAINT [PK_accn_payors] PRIMARY KEY CLUSTERED 
(
    [AUD_REC_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [zzzDataEOMTestingApril]
GO

/****** Object:  Table [dbo].[accn_payments]    Script Date: 08/20/2012 13:28:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[accn_payments](
    [ACCN_ID] [varchar](40) NULL,
    [PMT_SEQUENCE] [int] NULL,
    [DEPOSIT_ID] [int] NULL,
    [DEPOSIT_BATCH_ID] [int] NULL,
    [DEPOSIT_BATCH_SEQ] [int] NULL,
    [PROC_CODE] [varchar](40) NULL,
    [PMT_TYPE] [varchar](40) NULL,
    [USER_ID] [varchar](20) NULL,
    [NOTE] [varchar](4000) NULL,
    [PMT_DATE] [datetime] NULL,
    [CHECK_NUM] [varchar](40) NULL,
    [RECEIPT_NUM] [varchar](40) NULL,
    [ALLOWED_AMT] [float] NULL,
    [DEDUCT_AMT] [float] NULL,
    [PAID_AMT] [float] NULL,
    [COPAY_AMT] [float] NULL,
    [POSTED] [varchar](1) NULL,
    [BULK] [varchar](1) NULL,
    [UNITS_PAID] [int] NULL,
    [BILL_AMT_FROM_EOB] [float] NULL,
    [PAYMENT_PAYOR_ID] [varchar](15) NULL,
    [PRICED_PAYOR_ID] [varchar](15) NULL,
    [AUD_REC_ID] [int] NULL,
    [X_ACCN_BILLED_PROCEDURE_ID] [int] NULL,
    [X_PRICED_PAYOR_ID] [int] NULL,
    [X_PAYMENT_PAYOR_ID] [int] NULL,
    [CLIENT_PRIMARY_FACILITY_ID] [varchar](15) NULL,
    [REMIT_FILE_NAME] [varchar](128) NULL,
    [BATCH_POSTED] [varchar](1) NULL,
    [DEPOSIT_POSTED] [varchar](1) NULL,
    [AUDIT_DATE] [datetime] NULL,
    [ACCEPT_ASSIGNMENT] [varchar](1) NULL,
    [EXPECT_PRICE_DISCREPENCY_AMT] [float] NULL,
    [PRINT_NOTE] [varchar](1) NULL,
    [PATIENT_RESP_AMT] [float] NULL,
    [EOB] [varchar](40) NULL,
    [ICN] [varchar](30) NULL,
    [DEPOSIT_NOTE] [varchar](40) NULL,
    [NETWORK_ID] [varchar](100) NULL,
    [USE_EXPECT_PRICE] [varchar](1) NULL,
    [CO_INS_AMT] [float] NULL,
    [REMIT_DATE] [datetime] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


USE [zzzDataEOMTestingApril]
GO

/****** Object:  Table [dbo].[accn_demographics]    Script Date: 08/20/2012 13:28:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[accn_demographics](
    [ACCN_ID] [varchar](40) NULL,
    [STATUS] [varchar](15) NULL,
    [CLIENT_ID] [varchar](15) NULL,
    [CLIENT_NAME] [varchar](60) NULL,
    [REQ_ID] [varchar](20) NULL,
    [DOS] [datetime] NULL,
    [SEX] [varchar](3) NULL,
    [PT_ID] [varchar](40) NULL,
    [ORDERING UPIN] [bit] NULL,
    [PT_L_NAME] [varchar](40) NULL,
    [PT_F_NAME] [varchar](40) NULL,
    [PT_AGE] [varchar](40) NULL,
    [DOB] [date] NULL,
    [PT_HOME_PHM] [varchar](40) NULL,
    [PT_WORK_PHN] [varchar](40) NULL,
    [PT_ADDR1] [varchar](60) NULL,
    [PT_ADDR2] [varchar](60) NULL,
    [PT_ZIPCODE] [varchar](10) NULL,
    [PT_CITY] [varchar](60) NULL,
    [PT_ST_ID] [varchar](2) NULL,
    [PT_SSN] [int] NULL,
    [RECEIPT_DATE] [datetime] NULL,
    [INDIGENT_PCT] [float] NULL,
    [PRICE_DATE] [datetime] NULL,
    [EXPECT_PRICE] [float] NULL,
    [BILL_PRICE] [float] NULL,
    [GROSS_PRICE] [float] NULL,
    [DUE_AMT] [float] NULL,
    [ACCOUNTING_DATE] [datetime] NULL,
    [FINAL_RPT_DATE] [datetime] NULL,
    [TIME_OF_SERVICE] [varchar](20) NULL,
    [NO_CHARGE] [varchar](1) NULL,
    [AUD_REC_ID] [int] NULL,
    [ORIGINAL_ACCOUNTING_DATE] [datetime] NULL,
    [PT_COUNTRY] [varchar](40) NULL,
    [PHLEB_FACILITY] [varchar](40) NULL,
    [FASTING_TYPE] [varchar](40) NULL,
    [PT_LOCATION] [varchar](40) NULL,
    [PHLEB_USER_ID] [varchar](40) NULL,
    [PRIMARY_CLIENT_ID] [varchar](15) NULL,
    [PHYSICIAN_SOF] [varchar](1) NULL,
    [PATIENT_SOF] [varchar](1) NULL,
    [STAT] [varchar](1) NULL,
    [CALLBACK] [varchar](1) NULL,
    [PT_REPORT_COPY] [varchar](1) NULL,
    [PT_EMAIL] [varchar](40) NULL,
    [PAID_IN_FULL] [varchar](1) NULL,
    [CLIENT_STATEMENT_DATE] [datetime] NULL,
    [RETRO_BILL_PRICE] [float] NULL,
    [PATIENT_TYPE] [varchar](40) NULL,
    [REFERRING_UPIN] [bit] NULL,
    [PRIMARY_UPIN] [bit] NULL,
    [LOAD_DATE] [datetime] NULL,
    [TRIP_STOPS] [int] NULL,
    [TRIP_MILES] [int] NULL,
    [ROUND_TRIP] [varchar](1) NULL,
    [TRIP_PATIENT_COUNT] [int] NULL,
    [ADMISSION_SOURCE] [varchar](40) NULL,
    [EMERGENCY] [varchar](1) NULL,
    [ACCIDENT_CAUSE] [varchar](40) NULL,
    [PATIENT_MARITAL_STATUS] [varchar](40) NULL,
    [ADMISSION_TYPE] [varchar](40) NULL,
    [PATIENT_STATUS] [varchar](200) NULL,
    [WORPCOMP_CASE_WORKER] [varchar](80) NULL,
    [MRO] [varchar](40) NULL,
    [X_CLIENT_ID] [int] NULL,
    [X_PRIMARY_CLIENT_ID] [int] NULL,
    [AUDIT_DATE] [datetime] NULL,
    [ORDERING_NPI] [bit] NULL,
    [REFERRING_NPI] [bit] NULL,
    [PRIMARY_NPI] [bit] NULL,
    [CLIENT_PRODUCT] [int] NULL,
    [ONSET_DATE] [date] NULL,
    [ONSET_TYPE] [varchar](10) NULL,
    [ACCIDENT_STATE_ID] [varchar](2) NULL,
    [TRADE_DISCOUNT_AMOUNT] [float] NULL,
    [RETRO_TRADE_DISC_AMT] [float] NULL,
    [PATIENT_PREGNANT] [varchar](1) NULL,
    [PATIENT_GRAVIDA] [int] NULL,
    [ORDERING_PHYS_NAME] [varchar](80) NULL,
    [X_ORDERING_PHYS_ID] [int] NULL,
    [REFERRING_PHYS_NAME] [varchar](80) NULL,
    [X_ REFERRING_PHYS_ID] [int] NULL,
    [PRIMARY_PHYS_NAME] [varchar](80) NULL,
    [X_ PRIMARY _PHYS_ID] [int] NULL,
    [ADMISSION_DT] [date] NULL,
    [ADMISSION_TIME] [varchar](5) NULL,
    [DISCHARGE_DT] [date] NULL,
    [DISCHARGE_TIME] [varchar](5) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

问题如何加快此查询?有一些我错过的索引吗?为什么只添加一个表会以指数方式增加返回此查询结果所需的时间?也许我应该改变一些数据类型?

5 个答案:

答案 0 :(得分:3)

您确定加入付款人是否正确?付款人表中有一个accnid,以及付款人ID。

尝试将您的加入条件更改为:

join accn_payors payors
     on  payors.X_PAYOR_ID=p.X_PAYMENT_PAYOR_ID and
        payors.ACCN_ID = p.ACCN_ID

是否有另一张表格,其中付款人信息与帐户无关?

答案 1 :(得分:1)

在列accn_payors.X_PAYOR_ID上添加并编制索引,并将accn_payors.PAYOR_ID添加到该索引上的“包含列”。还要在列accn_payments.X_PAYMENT_PAYOR_ID

上添加索引

答案 2 :(得分:1)

索引外键肯定会有所帮助 - 在这种情况下是索引 accn_payors.X_PAYOR_ID应该有所不同。

表中有多少数据?

答案 3 :(得分:1)

尝试下面

Select 
    sum(p.paid_amt) as SumPaidAmt,
    p.ACCN_ID,
    p.X_PAYMENT_PAYOR_ID,
    cast(d.load_date as DATE) as LoadDate,
    cast (d.final_rpt_date as DATE) as FinalReportDate,
From
(
    SELECT     
            p.paid_amt,
            p.ACCN_ID,
            p.X_PAYMENT_PAYOR_ID
    From    accn_payments p
    Where   p.POSTED = 'y'
    AND     p.PMT_DATE between '20120401' and '20120430'    
)p
INNER Join accn_demographics    d           ON d.ACCN_ID=p.ACCN_ID
JOIN accn_payors                payors      ON payors.X_PAYOR_ID=p.X_PAYMENT_PAYOR_ID
GROUP BY
    d.accn_id,
    d.load_date,
    d.final_rpt_date,
    payors.PAYOR_ID

答案 4 :(得分:0)

您是否尝试过查看SQL Management Studio中计划的查询执行?它有时会提供有关缺失索引的提示。