我在使用inner join
運行查詢。用於執行聚合(SUM)的表具有在其上創建的索引,其列名稱爲dbo.tb_Sub_Contract.ContractGeneratedNo
。如何減少sql server查詢時間
任何表的最大行數大約爲250條記錄。查詢需要很長時間才能執行,我怎樣才能縮短執行時間?
SELECT
dbo.tb_Sub_Contract.ContractGeneratedNo,
SUM(dbo.tb_Sub_Contract.GrossAmount) AS GrossTotalAmount,
SUM(dbo.tb_Sub_Contract.WithheldAmount) AS WithheldTotalAmount,
dbo.tb_General_Contract.FilingMonth,
dbo.tb_General_Contract.FromSequenceNo,
dbo.tb_General_Contract.BoxNo,
CAST(dbo.tb_General_Contract.CompanyTRN AS Varchar(20)) + '-' + CAST(ISNULL(dbo.tb_General_Contract.CompanyBranch, '') AS Varchar(2)) AS Company_TRN_Branch,
COUNT(dbo.tb_Sub_Contract.SubContractId) AS NumberofContractors,
dbo.Get_Company_Name(CAST(dbo.tb_General_Contract.CompanyTRN AS Varchar(20)) + '-' + CAST(ISNULL(dbo.tb_General_Contract.CompanyBranch, '') AS Varchar(2))) AS Taxpayer_Name,
dbo.tb_General_Contract.PostedDate,
dbo.tb_Station.Collectorate,
dbo.tb_Station.StationCode
FROM
dbo.tb_General_Contract
INNER JOIN
dbo.tb_Sub_Contract ON dbo.tb_General_Contract.ContractGeneratedNo = dbo.tb_Sub_Contract.ContractGeneratedNo
INNER JOIN
dbo.tb_Station ON dbo.tb_General_Contract.StationId = dbo.tb_Station.StationCode
WHERE
(dbo.tb_Sub_Contract.IsActive = 1) AND (dbo.tb_General_Contract.IsActive = 1)
GROUP BY
dbo.tb_Sub_Contract.ContractGeneratedNo, dbo.tb_General_Contract.FilingMonth,
dbo.tb_General_Contract.FromSequenceNo, dbo.tb_General_Contract.BoxNo,
dbo.tb_General_Contract.CompanyTRN, dbo.tb_General_Contract.CompanyBranch,
dbo.tb_General_Contract.PostedDate, dbo.tb_Station.Collectorate,
dbo.tb_Station.StationCode
見表的兩個表結構下面聚集
CREATE TABLE [dbo].[tb_Sub_Contract](
[SubContractId] [bigint] IDENTITY(1,1) NOT NULL,
[ContractGeneratedNo] [varchar](100) NOT NULL,
[ContractTypeId] [int] NOT NULL,
[SubContractorName] [varchar](255) NOT NULL,
[PeriodBegin] [datetime] NOT NULL,
[PeriodEnd] [datetime] NOT NULL,
[GrossAmount] [decimal](14, 2) NOT NULL,
[WithheldAmount] [decimal](14, 2) NOT NULL,
[WithheldAmount_Cal] [decimal](14, 2) NOT NULL,
[trn_nbr] [bigint] NULL,
[ActionTakenId] [int] NULL,
[PostedBy] [bigint] NULL,
[PostedDate] [datetime] NULL,
[ModifiedBy] [bigint] NULL,
[ModificationDate] [datetime] NULL,
[IsActive] [bit] NULL,
[trn_nbrBranch] [smallint] NULL,
CONSTRAINT [PK_tb_Sub_Contract] PRIMARY KEY CLUSTERED
(
[SubContractId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[tb_General_Contract](
[ContractId] [bigint] IDENTITY(1,1) NOT NULL,
[ContractGeneratedNo] [varchar](100) NULL,
[CompanyTRN] [bigint] NULL,
[CompanyBranch] [smallint] NULL,
[FilingMonth] [datetime] NULL,
[FromSequenceNo] [varchar](50) NULL,
[BoxNo] [varchar](50) NULL,
[ActionTakenId] [int] NULL,
[PostedBy] [bigint] NULL,
[PostedDate] [datetime] NULL,
[IsActive] [bit] NULL,
[ModifiedBy] [bigint] NULL,
[ModificationDate] [datetime] NULL,
[StationId] [char](3) NULL,
[StationIdFrom] [char](3) NULL,
CONSTRAINT [PK_tb_General_Contract] PRIMARY KEY CLUSTERED
(
[ContractId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
你有什麼指標? –
如果按CTRL-L(顯示計劃),查詢的最大貢獻者(百分比)是多少? –