[英]Select query on primary key column of 500+ million rows table is not responding
I have a table in SQL Server having 500+ million rows with default clustered index defined on the primary key of table.我在 SQL Server 中有一个表,它有 500 多万行,在表的主键上定义了默认聚集索引。 I am running this simple query which takes more than 30 minutes.
我正在运行这个需要 30 多分钟的简单查询。
Select count(ledgeridXXX) from Ledger.dbo.tblXXXX
Here is the chunk of clustered index definition这是聚集索引定义的块
CONSTRAINT [PK__tblDepar__AE70E0AFF9BAF7B9] PRIMARY KEY CLUSTERED
(
[LedgerIDXXX] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
I don't have any clue where it could be wrong and why is it taking so much time.我不知道哪里可能出错,为什么要花这么多时间。 Please suggest me some ways or techniques on how to fix it as this is a critical table and main reporting heavily relies on this table.
请建议我一些解决方法或技术,因为这是一个关键表,主要报告严重依赖于该表。
if you are just counting the number of rows, you can try this:如果你只是计算行数,你可以试试这个:
SELECT SUM(p.rows) as [cnt]
FROM sys.partitions AS p
INNER JOIN sys.tables AS t ON p.[object_id] = t.[object_id]
INNER JOIN sys.schemas AS s ON t.[schema_id] = s.[schema_id]
WHERE p.index_id = 1 /* clustered index */
AND t.name = N'tblXXXX'AND s.name = N'dbo';
the COUNT(clusteredkey)
will probably have some overhead when you have intensive workload.当您有密集的工作量时,
COUNT(clusteredkey)
可能会有一些开销。 this can take hours scanning the entire 500million rows.这可能需要数小时才能扫描整个 5 亿行。
if you are running higher version of SQL Server, you can use the columnstore index to speed up the COUNT
.如果您运行的是更高版本的 SQL Server,则可以使用列存储索引来加速
COUNT
。
References:参考:
What happens when you do COUNT(Column)
, COUNT(1)
, COUNT(*)
without the where
clause:当您在没有
where
子句的情况下执行COUNT(Column)
、 COUNT(1)
、 COUNT(*)
时会发生什么:
SQL Server finds the smallest (containing the least amount of columns, the most rows in a single AK page) index that will do the job. SQL Server 查找将完成这项工作的最小(包含最少的列数,单个 AK 页中的最多行)索引。
If you only have a clustered index, this means it will use the clustered index.如果您只有一个聚集索引,这意味着它将使用聚集索引。 (The clustered index contains all columns of the table)
(聚集索引包含表的所有列)
What you can do to speed things up:你可以做些什么来加快速度:
WITH(TABLOCK)
or WITH(TABLOCKX)
to force a table lock or exclusive table lock.WITH(TABLOCK)
或WITH(TABLOCKX)
以强制表锁或排他表锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.