admin管理员组文章数量:1122832
Using SQL Server 2017. I have a table with an indexed column [UniqueIdentifierId]
, making a select to count records take hours although table is indexed.
CREATE TABLE [ZetEvent2UniqueIdentifier]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[EventId] [int] NOT NULL,
[UniqueIdentifierId] [varchar](100) NULL,
[Destinations] [varchar](120) NULL,
[Localization] [varchar](50) NULL,
[EDPType] [tinyint] NULL,
[Export] [bit] NULL,
[FirstArrival] [bit] NULL,
CONSTRAINT [PK_ZetEvent2UniqueIdentifier]
PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_ZetEvent2UniqueIdentifier_UniqueIdentifierId]
ON [ZetEvent2UniqueIdentifier] ([UniqueIdentifierId] 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]
GO
If I do
SELECT COUNT(*)
FROM ZetEvent2UniqueIdentifier zeu
WHERE UniqueIdentifierId = '054117860540129BbLRGcI'
it takes hours. Explain shows that the index is used. Why is it so slow? A bug?
Using SQL Server 2017. I have a table with an indexed column [UniqueIdentifierId]
, making a select to count records take hours although table is indexed.
CREATE TABLE [ZetEvent2UniqueIdentifier]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[EventId] [int] NOT NULL,
[UniqueIdentifierId] [varchar](100) NULL,
[Destinations] [varchar](120) NULL,
[Localization] [varchar](50) NULL,
[EDPType] [tinyint] NULL,
[Export] [bit] NULL,
[FirstArrival] [bit] NULL,
CONSTRAINT [PK_ZetEvent2UniqueIdentifier]
PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_ZetEvent2UniqueIdentifier_UniqueIdentifierId]
ON [ZetEvent2UniqueIdentifier] ([UniqueIdentifierId] 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]
GO
If I do
SELECT COUNT(*)
FROM ZetEvent2UniqueIdentifier zeu
WHERE UniqueIdentifierId = '054117860540129BbLRGcI'
it takes hours. Explain shows that the index is used. Why is it so slow? A bug?
Share Improve this question edited Nov 22, 2024 at 14:02 O. Jones 108k17 gold badges128 silver badges180 bronze badges asked Nov 22, 2024 at 13:56 Alain BourgeoisAlain Bourgeois 891 silver badge5 bronze badges 5 |1 Answer
Reset to default -3We have insufficient information to determine why this is happening. There are a number of situations that might be behind it. The following are some aspects to look into.
- How big is the table? Mere size should not be enough to produce hours-long execution times, but it might be a contributing factor.
- What hardware is involved? Again, given modern technology, unlikely on it’s own, but could also be a contributing factor.
- Volume of usage. Again likely not a sole cause, but if hundreds or thousands of operations are simultaneously hitting your system, this could impact things.
- Contention. If other operations are opening and holding locks on the table (such as updates, deletes, perhaps reindexing operations), this would block the select from executing. Note that some locks might block this, others might not.
- Fringe cases. There might be some (relatively) non-standard situation present that’s behind this situation
My guess is it’s a combination of some of the above. Without hands-out review and observation, I would not know how to determine what your actual situation is.
本文标签: query optimizationSQL Server doesn39t use existing indexStack Overflow
版权声明:本文标题:query optimization - SQL Server doesn't use existing index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303310a1931839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
OPTION(RECOMPILE)
perhaps. If the values are unique, perhaps you can changeselect count(*)
toselect top 1 1
. Can also be that it's busy gathering statistics so it hasn't started executing query yet – siggemannen Commented Nov 22, 2024 at 14:06UniqueXxxx
isn't indexed by a unique index? I guess there is given you need to count how many rows have this "unique" value but it seems odd. – Damien_The_Unbeliever Commented Nov 22, 2024 at 14:08'054117860540129BbLRGcI'
exactly? – Thom A Commented Nov 22, 2024 at 14:20SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
right before yourSELECT COUNT(*)
query. This causes a so-called dirty read and so may not be super precise. But that may not matter. At any rate, it doesn't wait for other transactions as much, and so is usually faster. – O. Jones Commented Nov 22, 2024 at 17:59