我创建了一个导入 600,000 行 Excel 工作表的表格。我将运行的大多数查询都将使用C_CustomerID,它们将如下例所示:
select * from testtable where C_CustomerID = 12345678
Run Code Online (Sandbox Code Playgroud)
表定义为:
CREATE TABLE [dbo].[testtable](
[id_card] [int] IDENTITY(1,1) NOT NULL,
[C_CustomerID] [int] NOT NULL,
[C_AccountID] [nvarchar](255) NULL,
[C_ProductID] [varchar](20) NULL,
.......... more columns here ...
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)
我创建了一个索引:
CREATE NONCLUSTERED INDEX [inx_profitability] ON [dbo].[testtable]
(
[C_CustomerID] ASC
)
Run Code Online (Sandbox Code Playgroud)
稍后我只会从 Excel 工作表导入更多记录。每 6 个月将导出约 500,000 条新记录。
一旦表变大,我可以做些什么来提高未来的性能?
我正在运行 SQL Server 2012 和 Microsoft SQL Server Management Studio 11.0.3128.0。
我有一个工作正常的查询:
SELECT
month_date= month(SendTime) ,year_date =year(sendtime) ,MessageType
,COUNT(MessageType) AS MESSAGE_COUNT
FROM Message
WHERE year(SendTime)= 2014
and messagetype NOT LIKE '%test%'
and messagetype NOT LIKE '%ANOTHER THING%'
and messagetype NOT LIKE '%STUFF%'
GROUP BY month(SendTime),year(sendtime), MessageType
Run Code Online (Sandbox Code Playgroud)
我注意到的是LIKE有条件它使流逝时间变慢。
使用上面的查询,它在经过时间 0.550 运行,没有它们 0.154。
有没有其他方法可以缩短这次时间并使查询更快?
没有消息类型不是索引,我不允许更改它。