使用SQL Server2016。我具有下表,该表具有大量记录(超过3000万个记录)。
CREATE TABLE [dbo].[TABLE1]
(
[DATE_TIME] [DATETIME] NULL,
[TEXT] [VARCHAR](500) NULL,
[MSG] [VARCHAR](500) NULL,
[MSGID] [INT] NULL,
[SEVERITY] VARCHAR(50) NULL
)
CREATE NONCLUSTERED INDEX [TABLE1_IX1]
ON [dbo].[TABLE1] ([DATE_TIME] ASC, [MSGID] ASC, [SEVERITY] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
以下查询非常慢:
SELECT
[DATE_TIME], [TEXT], [MSG], [MSGID], [SEVERITY]
FROM
[TABLE1]
WHERE
FORMAT([DATE_TIME], 'yyyy-MM-dd') IN ('2019-06-25', '2019-06-24',etc.....)
AND [MSGID] IN (8016, 11, 3072, 23, 3062, etc....)
AND [SEVERITY] <> 'Medium'
ORDER BY
[DATE_TIME] DESC
请帮助优化。
答案 0 :(得分:2)
格式具有一些强大的功能,但性能会受到影响。
尝试
WHERE convert(date,[DATE_TIME]) in (...)
答案 1 :(得分:1)
您提到DATE_TIME列包含时间部分。在这种情况下,我建议创建一个仅包含datepart的持久化计算列:
<input className="text-center"
type="number"
min="1"
class="numberInput"
onChange={(e) => this.onChangeQty(e)}
/>
onChangeQty(e)
{
const element = document.getElementsByClassName('numberInput');
function handleKeypress(e) {
element[0].value = '';
}
element[0].addEventListener('keyup', handleKeypress);
}
索引它并在where子句中使用它:
X_DATE AS CAST(DATE_TIME AS DATE) PERSISTED
答案 2 :(得分:1)
使用直接日期比较。因为您似乎有一定范围,所以我建议:
WHERE [DATE_TIME] >= ? AND
[DATE_TIME] < '2019-06-26' AND
[MSGID] IN (8016, 11, 3072, 23, 3062, etc....) AND
[SEVERITY] <> 'Medium'
如果可以的话,这可以最佳地利用索引和分区。您也可以转换为date
。那是一种功能的用途,它将同时使用索引。我不知道convert是否会修剪分区。