gpt4 book ai didi

sql - 使用数据透视表加快计数 (*)

转载 作者:行者123 更新时间:2023-12-05 01:15:48 29 4
gpt4 key购买 nike

我的问题是,在我们的应用程序中,我们在 MS SQL Server (2008) 数据库上执行了复杂的 SELECT,该数据库由要在表之间使用的多个连接(3 个或更多)组成使用 PIVOT 创建(每个数据透视表大约有 10000 行)。

只有 SELECT 相当快(select 只返回总数中的几行,如 50 from 10, 000)。但是使用 Count(*) 或过滤查找所有记录的数量要慢得多(10 000 条记录大约需要 2 秒)

有没有办法在查询总数和过滤上加快查询速度?例如 SQL Server 中的一些缓存或优化查询?

注意:我们的 ASMX 服务对数据库的查询。注2:每个包含数据透视表的表都有主键类型:uniqueidentifier

基本选择如下所示:

SELECT
Table1.[Id] AS [Id],
Table1.[Status] AS [Status],
Table2.[Id] AS [Id],
Table2.[Status] AS [Status],

FROM
(
-- Do PIVOT
) AS Table1
LEFT JOIN (
-- Do PIVOT
) AS Table2 ON Table2.xxx = Table1.yyy
)

--Catch only first X records..

最佳答案

您可能应该为每个数据透视表使用一个 View

来自 MS 文档

After a unique clustered index is created on the view, the view's result set is materialized immediately and persisted in physical storage in the database, saving the overhead of performing this costly operation at execution time.

关于sql - 使用数据透视表加快计数 (*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18141732/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com