我正在执行以下查询。只有17个记录需要80秒。
任何人都可以告诉我理由是否知道。我已经尝试过索引。
SELECT DISTINCT t.i_UserID,
u.vch_LoginName,
t.vch_PreviousEmailAddress AS 'vch_EmailAddress',
u.vch_DisplayName,
t.d_TransactionDate AS 'd_DateAdded',
'Old' AS 'vch_RecordStatus'
FROM tblEmailTransaction t
INNER JOIN tblUser u
ON t.i_UserID = u.i_UserID
WHERE t.vch_PreviousEmailAddress LIKE '%kala%'
答案 0 :(得分:1)
更改vch_PreviousEmailAddress
Latin1_General_100_BIN2
列的排序规则
创建覆盖索引:
CREATE NONCLUSTERED INDEX ix
ON dbo.tblEmailTransaction (vch_PreviousEmailAddress)
INCLUDE (i_UserID, d_TransactionDate)
GO
欢迎使用此查询:
SELECT t.i_UserID,
u.vch_LoginName,
t.vch_PreviousEmailAddress AS vch_EmailAddress,
u.vch_DisplayName,
t.d_TransactionDate AS d_DateAdded,
'Old' AS vch_RecordStatus
FROM (
SELECT DISTINCT i_UserID,
vch_PreviousEmailAddress,
d_TransactionDate
FROM dbo.tblEmailTransaction
WHERE vch_PreviousEmailAddress LIKE '%kala%' COLLATE Latin1_General_100_BIN2
) t
JOIN dbo.tblUser u ON t.i_UserID = u.i_UserID
答案 1 :(得分:0)
另一件事,我觉得在解决这样的问题时很有用:
尝试运行以下脚本。它会告诉您可以向SQL Server数据库询问哪些索引,哪些索引会得到最大的改善。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 100
ROUND(s.avg_total_user_cost * s.avg_user_impact * (s.user_seeks + s.user_scans),0) AS 'Total Cost',
s.avg_user_impact,
d.statement AS 'Table name',
d.equality_columns,
d.inequality_columns,
d.included_columns,
'CREATE INDEX [IndexName] ON ' + d.statement + ' ( '
+ case when (d.equality_columns IS NULL OR d.inequality_columns IS NULL)
then ISNULL(d.equality_columns, '') + ISNULL(d.inequality_columns, '')
else ISNULL(d.equality_columns, '') + ', ' + ISNULL(d.inequality_columns, '')
end + ' ) '
+ CASE WHEN d.included_columns IS NULL THEN '' ELSE 'INCLUDE ( ' + d.included_columns + ' )' end AS 'CREATE INDEX command'
FROM sys.dm_db_missing_index_groups g,
sys.dm_db_missing_index_group_stats s,
sys.dm_db_missing_index_details d
WHERE d.database_id = DB_ID()
AND s.group_handle = g.index_group_handle
AND d.index_handle = g.index_handle
ORDER BY [Total Cost] DESC
右侧列显示您需要运行的CREATE INDEX命令,以创建该索引。
这个救生员脚本中的一个,我经常在我们的内部数据库上运行。
但是,在您的示例中,这可能会告诉您需要vch_PreviousEmailAddress
表中tblEmailTransaction
字段的索引。
答案 2 :(得分:0)
可能的瓶颈是2:
Like Statement:正如Devart所建议的那样,语句被认为性能不佳,尝试以这种方式指定集合:
WHERE vch_PreviousEmailAddress LIKE '%kala%' COLLATE Latin1_General_100_BIN2
要更好地查看查询,必须使用查询运行此命令:
SET IO STATISTICS ON
它将写入查询所做的所有IO Access,我们可以看到发生了什么。
最后一个问题? 这两个表包含多少行?
侨