我已经完成了改进以下查询的任务,因为它有点慢。
SELECT 9 AS FieldCount,
A.AccountName AS Field1,
U.FullName AS Field2,
A.City AS Field3,
A.State AS Field4,
A.Zip5 AS Field5,
COUNT(DISTiNCT C.Contact_ID) AS Field6,
COUNT(DISTiNCT L.Lead_ID) AS Field7,
A.Account_ID AS Field8,
A.CreateDateTime AS Field9,
ROW_NUMBER() OVER (ORDER BY A.CreateDateTime DESC) As RowNum
FROM
dynamic_Account A
LEFT JOIN dynamic_Contact C
ON A.Account_ID = C.Account_ID
LEFT JOIN dynamic_Lead L
ON A.Account_ID = L.Account_ID,
static_List AS LS,
static_List AS LC,
static_User AS U
WHERE (A.Account_ID > 0)
AND (A.Source_ID = LS.List_ID)
AND (A.Category_ID = LC.List_ID)
AND (A.AccountSR01_ID = U.User_ID)
GROUP BY
AccountName,
LC.Title,
U.FullName,
A.City,
A.State,
A.Zip5,
A.Account_ID,
A.CreateDateTime
任何人都可以给我一个关于如何改进关节或在哪里放置索引的指针?感谢
编辑: 下面是我从sql management studio生成的表的创建脚本。 dynamic_Account:
CREATE TABLE [dbo].[dynamic_Account](
[Account_ID] [int] IDENTITY(1,1) NOT NULL,
[RecordStatus] [int] NULL,
[AttributeList] [nvarchar](max) NULL,
[CreateUser_ID] [int] NULL,
[CreateDateTime] [datetime] NULL,
[ChangeUser_ID] [int] NULL,
[ChangeDateTime] [datetime] NULL,
[ViewUser_ID] [int] NULL,
[ViewDateTime] [datetime] NULL,
[InactiveFlag] [bit] NOT NULL,
[Source_ID] [int] NULL,
[Campaign_ID] [int] NULL,
[Comments] [nvarchar](max) NULL,
[Parent_ID] [int] NULL,
[Subsidiary_ID] [int] NULL,
[IndividualFlag] [bit] NOT NULL,
[AccountType] [nvarchar](50) NULL,
[AccountStage] [nvarchar](50) NULL,
[AccountName] [nvarchar](100) NULL,
[CompanyName] [nvarchar](100) NULL,
[Address1] [nvarchar](100) NULL,
[Address2] [nvarchar](100) NULL,
[Address3] [nvarchar](100) NULL,
[Address4] [nvarchar](100) NULL,
[City] [nvarchar](100) NULL,
[State] [nvarchar](100) NULL,
[PostalCode] [nvarchar](20) NULL,
[Zip5] [nvarchar](5) NULL,
[Zip4] [nvarchar](4) NULL,
[Country] [nvarchar](50) NULL,
[DoNotMailFlag] [bit] NOT NULL,
[TerritoryList] [nvarchar](255) NULL,
[PhoneNumber] [nvarchar](30) NULL,
[DoNotCallFlag] [bit] NOT NULL,
[FaxNumber] [nvarchar](30) NULL,
[Category_ID] [int] NULL,
[OtherNameList] [nvarchar](255) NULL,
[WebSite] [nvarchar](255) NULL,
[Geolocation] [nvarchar](100) NULL,
[OverrideTerritoryFlag] [bit] NOT NULL,
[AccountSR01_ID] [int] NULL,
[AccountSR02_ID] [int] NULL,
[AccountSR03_ID] [int] NULL,
[AccountSR04_ID] [int] NULL,
[AccountSR05_ID] [int] NULL,
[AccountSR06_ID] [int] NULL,
[AccountSR07_ID] [int] NULL,
[AccountSR08_ID] [int] NULL,
[AccountSR09_ID] [int] NULL,
[AccountSR10_ID] [int] NULL,
[AccountSR11_ID] [int] NULL,
[AccountSR12_ID] [int] NULL,
[AccountSR13_ID] [int] NULL,
[AccountSR14_ID] [int] NULL,
[AccountSR15_ID] [int] NULL,
[AnnualRevenue] [nvarchar](100) NULL,
[EmployeeCount] [nvarchar](100) NULL,
[BusinessDuration] [nvarchar](100) NULL,
[Custom_01] [nvarchar](255) NULL,
[Custom_02] [nvarchar](255) NULL,
[Custom_03] [nvarchar](255) NULL,
[Custom_04] [nvarchar](255) NULL,
[Custom_05] [nvarchar](255) NULL,
[Custom_06] [nvarchar](255) NULL,
[Custom_07] [nvarchar](255) NULL,
[Custom_08] [nvarchar](255) NULL,
[Custom_09] [nvarchar](255) NULL,
[Custom_10] [nvarchar](255) NULL,
[Custom_11] [nvarchar](255) NULL,
[Custom_12] [nvarchar](255) NULL,
[Custom_13] [nvarchar](255) NULL,
[Custom_14] [nvarchar](255) NULL,
[Custom_15] [nvarchar](255) NULL,
[Custom_16] [nvarchar](255) NULL,
[Custom_17] [nvarchar](255) NULL,
[Custom_18] [nvarchar](255) NULL,
[Custom_19] [nvarchar](255) NULL,
[Custom_20] [nvarchar](255) NULL,
CONSTRAINT [PK_dynamic_Account_1] PRIMARY KEY CLUSTERED
(
[Account_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
dynamic_Contact:
CREATE TABLE [dbo].[dynamic_Contact](
[Contact_ID] [int] IDENTITY(1,1) NOT NULL,
[RecordStatus] [int] NULL,
[AttributeList] [nvarchar](max) NULL,
[CreateUser_ID] [int] NULL,
[CreateDateTime] [datetime] NULL,
[ChangeUser_ID] [int] NULL,
[ChangeDateTime] [datetime] NULL,
[ViewUser_ID] [int] NULL,
[ViewDateTime] [datetime] NULL,
[InactiveFlag] [bit] NOT NULL,
[Source_ID] [int] NULL,
[Campaign_ID] [int] NULL,
[Comments] [nvarchar](max) NULL,
[Subsidiary_ID] [int] NULL,
[Account_ID] [int] NULL,
[ContactName] [nvarchar](50) NULL,
[Salutation] [nvarchar](10) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleInitial] [nvarchar](10) NULL,
[LastName] [nvarchar](50) NULL,
[Department] [nvarchar](255) NULL,
[JobTitle] [nvarchar](255) NULL,
[RoleList] [nvarchar](255) NULL,
[DoNotMailFlag] [bit] NOT NULL,
[WorkPhoneNumber] [nvarchar](30) NULL,
[DoNotCallWorkFlag] [bit] NOT NULL,
[CellPhoneNumber] [nvarchar](30) NULL,
[DoNotCallCellFlag] [bit] NOT NULL,
[HomePhoneNumber] [nvarchar](30) NULL,
[DoNotCallHomeFlag] [bit] NOT NULL,
[EmailAddress] [nvarchar](100) NULL,
[DoNotEmailFlag] [bit] NOT NULL,
[AltEmailAddress] [nvarchar](100) NULL,
[DoNotEmailAltFlag] [bit] NOT NULL,
[ProductInterestList] [nvarchar](255) NULL,
[Custom_01] [nvarchar](255) NULL,
[Custom_02] [nvarchar](255) NULL,
[Custom_03] [nvarchar](255) NULL,
[Custom_04] [nvarchar](255) NULL,
[Custom_05] [nvarchar](255) NULL,
[Custom_06] [nvarchar](255) NULL,
[Custom_07] [nvarchar](255) NULL,
[Custom_08] [nvarchar](255) NULL,
[Custom_09] [nvarchar](255) NULL,
[Custom_10] [nvarchar](255) NULL,
[Custom_11] [nvarchar](255) NULL,
[Custom_12] [nvarchar](255) NULL,
[ProductInterestListOld] [nvarchar](255) NULL,
CONSTRAINT [PK_dynamic_Contact] PRIMARY KEY CLUSTERED
(
[Contact_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
dynamic_Lead:
CREATE TABLE [dbo].[dynamic_Lead](
[Lead_ID] [int] IDENTITY(1,1) NOT NULL,
[RecordStatus] [int] NULL,
[AttributeList] [nvarchar](max) NULL,
[CreateUser_ID] [int] NULL,
[CreateDateTime] [datetime] NULL,
[ChangeUser_ID] [int] NULL,
[ChangeDateTime] [datetime] NULL,
[ViewUser_ID] [int] NULL,
[ViewDateTime] [datetime] NULL,
[Source_ID] [int] NULL,
[Campaign_ID] [int] NULL,
[Comments] [nvarchar](max) NULL,
[Subsidiary_ID] [int] NULL,
[Contact_ID] [int] NULL,
[Account_ID] [int] NULL,
[LeadDateTime] [datetime] NULL,
[Description] [nvarchar](255) NULL,
[Details] [nvarchar](max) NULL,
[OverrideTerritoryFlag] [bit] NULL,
[SalesRep_ID] [int] NULL,
[SalesGroup_ID] [int] NULL,
[LeadQuality_ID] [int] NULL,
[LeadStage_ID] [int] NULL,
[LeadStatus_ID] [int] NULL,
[ActivityStatus_ID] [int] NULL,
[DisqualifiedReason_ID] [int] NULL,
[ProductInterestList] [nvarchar](255) NULL,
[Score] [int] NULL,
[Custom_01] [nvarchar](255) NULL,
[Custom_02] [nvarchar](255) NULL,
[Custom_03] [nvarchar](255) NULL,
[Custom_04] [nvarchar](255) NULL,
[Custom_05] [nvarchar](255) NULL,
[Custom_06] [nvarchar](255) NULL,
[Custom_07] [nvarchar](255) NULL,
[Custom_08] [nvarchar](255) NULL,
[Custom_09] [nvarchar](255) NULL,
[Custom_10] [nvarchar](255) NULL,
[Custom_11] [nvarchar](255) NULL,
[Custom_12] [nvarchar](255) NULL,
[ProductInterestListOld] [nvarchar](255) NULL,
CONSTRAINT [PK_dynamic_Lead] PRIMARY KEY CLUSTERED
(
[Lead_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
static_List:
CREATE TABLE [dbo].[static_List](
[List_ID] [int] IDENTITY(1,1) NOT NULL,
[RecordStatus] [int] NULL,
[AttributeList] [ntext] NULL,
[CreateUser_ID] [int] NULL,
[CreateDateTime] [datetime] NULL,
[ChangeUser_ID] [int] NULL,
[ChangeDateTime] [datetime] NULL,
[InactiveFlag] [bit] NULL,
[ListName] [nvarchar](100) NULL,
[Title] [nvarchar](100) NULL,
[Parent_ID] [int] NULL,
[DisplayIndex] [int] NULL,
[DefaultFlag] [bit] NULL,
[External_ID] [nvarchar](255) NULL,
[Custom_01] [nvarchar](255) NULL,
[Custom_02] [nvarchar](255) NULL,
[Custom_03] [nvarchar](255) NULL,
[Custom_04] [nvarchar](255) NULL,
[Custom_05] [nvarchar](255) NULL,
[Custom_06] [nvarchar](255) NULL,
[Custom_07] [nvarchar](255) NULL,
[Custom_08] [nvarchar](255) NULL,
[Custom_09] [nvarchar](255) NULL,
[Custom_10] [nvarchar](255) NULL,
[Custom_11] [nvarchar](255) NULL,
[Custom_12] [nvarchar](255) NULL,
[Custom_13] [nvarchar](255) NULL,
[Custom_14] [nvarchar](255) NULL,
[Custom_15] [nvarchar](255) NULL,
[Custom_16] [nvarchar](255) NULL,
[Custom_17] [nvarchar](255) NULL,
[Custom_18] [nvarchar](255) NULL,
[Custom_19] [nvarchar](255) NULL,
[Custom_20] [nvarchar](255) NULL,
CONSTRAINT [PK_tmp_static_List] PRIMARY KEY CLUSTERED
(
[List_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
static_User:
CREATE TABLE [dbo].[static_User](
[User_ID] [int] IDENTITY(1,1) NOT NULL,
[RecordStatus] [int] NULL,
[AttributeList] [nvarchar](max) NULL,
[CreateUser_ID] [int] NULL,
[CreateDateTime] [datetime] NULL,
[ChangeUser_ID] [int] NULL,
[ChangeDateTime] [datetime] NULL,
[InactiveFlag] [bit] NOT NULL,
[Username] [nvarchar](100) NULL,
[EmailAddress] [nvarchar](150) NULL,
[LegalName] [nvarchar](100) NULL,
[FullName] [nvarchar](100) NULL,
[FirstName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[DisplayName] [nvarchar](100) NULL,
[NickName] [nvarchar](100) NULL,
[CellPhoneNumber] [nvarchar](100) NULL,
[WorkPhoneNumber] [nvarchar](100) NULL,
[FaxNumber] [nvarchar](100) NULL,
[JobTitle] [nvarchar](100) NULL,
[Department] [nvarchar](100) NULL,
[OfficeName] [nvarchar](100) NULL,
[Address1] [nvarchar](100) NULL,
[Address2] [nvarchar](100) NULL,
[Address3] [nvarchar](100) NULL,
[City] [nvarchar](100) NULL,
[State] [nvarchar](100) NULL,
[Zip] [nvarchar](100) NULL,
[Country] [nvarchar](100) NULL,
[LastAccess] [datetime] NULL,
[Custom_01] [nvarchar](255) NULL,
[Custom_02] [nvarchar](255) NULL,
[Custom_03] [nvarchar](255) NULL,
[Custom_04] [nvarchar](255) NULL,
[Custom_05] [nvarchar](255) NULL,
[Custom_06] [nvarchar](255) NULL,
[Custom_07] [nvarchar](255) NULL,
[Custom_08] [nvarchar](255) NULL,
[Custom_09] [nvarchar](255) NULL,
[Custom_10] [nvarchar](255) NULL,
[Custom_11] [nvarchar](255) NULL,
[Custom_12] [nvarchar](255) NULL,
CONSTRAINT [PK_dbo_static_User] PRIMARY KEY CLUSTERED
(
[User_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON
行计数: dynamic_Account:180,000行
dynamic_Contact:310,000行
dynamic_Lead:130,000行
static_List:1300行
static_User:250行
我目前在任何桌子上都没有索引。
答案 0 :(得分:0)
由于您的结果不包含static_List中的任何内容,因此从FROM子句中删除static_List并在WHERE子句中使用EXISTS或IN可能会有所帮助。此外,您应该与您加入表格的方式保持一致。最后,因为你没有显示static_List表中的任何内容,所以在你的GROUP BY子句中包含它是没有意义的。
您可以添加加速流程的索引,但如下更改查询可能会产生最大的影响:
SELECT 9 AS FieldCount,
A.AccountName AS Field1,
U.FullName AS Field2,
A.City AS Field3,
A.State AS Field4,
A.Zip5 AS Field5,
COUNT(DISTiNCT C.Contact_ID) AS Field6,
COUNT(DISTiNCT L.Lead_ID) AS Field7,
A.Account_ID AS Field8,
A.CreateDateTime AS Field9,
ROW_NUMBER() OVER (ORDER BY A.CreateDateTime DESC) As RowNum
FROM
dynamic_Account A
LEFT JOIN dynamic_Contact C
ON A.Account_ID = C.Account_ID
LEFT JOIN dynamic_Lead L
ON A.Account_ID = L.Account_ID,
INNER JOIN static_User U ON A.AccountSR01_ID = U.User_ID
WHERE (A.Account_ID > 0)
AND EXISTS(SELECT 1 from static_List WHERE A.Category_ID = List_ID Or A.Source_ID = List_ID)
GROUP BY
AccountName,
U.FullName,
A.City,
A.State,
A.Zip5,
A.Account_ID,
A.CreateDateTime
答案 1 :(得分:0)
一般来说,您希望在以下内容中添加索引:
a)加入 b)按列排序 c)按列分组
如果您了解数据库正在执行的操作,则必须执行以下每项操作:
a)对表格进行排序以找出哪些匹配 b)需要对序列中的表进行排序以返回结果集 c)按照
对表格进行分组索引和查询优化是一个简单的主题,但它可能包含许多复杂性。
此外,主键会自动创建UNIQUE索引(因为主键是唯一的)。因此,只要PK存在,您就不需要对此进行索引。
这应该给你一个很好的起点。
在您的情况下,我会将索引(默认类型为" b-tree")添加到:
希望这有帮助。
答案 2 :(得分:0)
对我来说这看起来像SQL Server。
查询返回180,000行,因此它永远不会非常快。
我对表之间的关系做了一些猜测/假设。
WITH
CTE_Contacts
AS
(
SELECT
dynamic_Contact.Account_ID
,COUNT(*) AS CountContacts
FROM dynamic_Contact
GROUP BY dynamic_Contact.Account_ID
)
,CTE_Leads
AS
(
SELECT
dynamic_Lead.Account_ID
,COUNT(*) AS CountLeads
FROM dynamic_Lead
GROUP BY dynamic_Lead.Account_ID
)
SELECT
9 AS FieldCount
,A.AccountName AS Field1
,U.FullName AS Field2
,A.City AS Field3
,A.State AS Field4
,A.Zip5 AS Field5
,ISNULL(CTE_Contacts.CountContacts, 0) AS Field6
,ISNULL(CTE_Leads.CountLeads, 0) AS Field7
,A.Account_ID AS Field8
,A.CreateDateTime AS Field9
,ROW_NUMBER() OVER (ORDER BY A.CreateDateTime DESC) As RowNum
FROM
dynamic_Account AS A
INNER JOIN static_User AS U ON U.User_ID = A.AccountSR01_ID
INNER JOIN static_List AS LS ON LS.List_ID = A.Source_ID
INNER JOIN static_List AS LC ON LC.List_ID = A.Category_ID
LEFT JOIN CTE_Contacts AS C ON C.Account_ID = A.Account_ID
LEFT JOIN CTE_Leads AS L ON L.Account_ID = A.Account_ID
WHERE
(A.Account_ID > 0)
;
Account_ID
不是NULL
且从{1}开始是IDENTITY
,因此所有值都应为正数,因此没有明确的过滤器Account_ID > 0
。加入static_List
并没有多大意义,除非A.Source_ID
和A.Category_ID
可以包含static_List
中不存在的值(例如{{}} 1}})。
创建索引
这些非常重要(即使您不改变查询本身):
NULL
dynamic_Contact.Account_ID
这些并不重要,但可能有用:
dynamic_Lead.Account_ID
dynamic_Account.AccountSR01_ID
dynamic_Account.Source_ID
dynamic_Account.Category_ID
创建外键
表格之间的所有关系。