/*
SET NOCOUNT ON
CREATE TABLE #tmp(userId INT NOT NULL, productId INT NOT NULL, CONSTRAINT pk_Temp PRIMARY KEY(userid, productId))
DECLARE @u INT = 0
DECLARE @p INT = 0
WHILE @u < 100000
BEGIN
SET @u = @u + 1
WHILE @p < 29
BEGIN
SET @p = @p + 1
IF RAND() * 100 < 99
BEGIN
INSERT INTO #tmp
VALUES (@u, @p)
END
END
SET @p = 0
END
SET NOCOUNT OFF
DROP TABLE #tmp
DROP INDEX ix_TempOne ON #tmp
CREATE NONCLUSTERED INDEX ix_TempOne ON #tmp (userId)
DROP INDEX ix_TempTwo ON #tmp
CREATE NONCLUSTERED INDEX ix_TempTwo ON #tmp (userId) INCLUDE(productId)
DROP INDEX ix_TempThree ON #tmp
CREATE NONCLUSTERED INDEX ix_TempThree ON #tmp (productId)
DROP INDEX ix_TempFour ON #tmp
CREATE NONCLUSTERED INDEX ix_TempFour ON #tmp (productId) INCLUDE (userId)
*/
begin transaction t1
IF OBJECT_ID('tempdb..#tmp2') IS NOT NULL
DROP TABLE #tmp2
DECLARE @date DATETIME = GETDATE()
SELECT userId INTO #tmp2 FROM #tmp WHERE userId BETWEEN (SELECT FLOOR(RAND() * (100+1)+1)) AND (SELECT FLOOR(RAND() * (100000+1)+1))
SELECT DISTINCT productId FROM #tmp WHERE UserId IN (SELECT UserId FROM #tmp2)
SELECT CAST(DATEDIFF(millisecond, @date, GETDATE()) AS VARCHAR) + ' miliseconds' + CHAR(10)
这是一个示例,类似于我们用来过滤的一个缓存表。
基于结果集(在#tmp2创建的结果集),我们想知道哪个ProductId仍然适合过滤后的选项。 (只是添加了随机部分以创建动态结果集)
当前,我已经在userId和productId上设置了一个主键,并在数据库上具有索引“ TempTwo”和“ TempFour”,但是当我希望该查询小于100毫秒时,该查询通常仍会导致400-1000毫秒的调用
我是否可以更改表的概念或更改收集信息的方式以在100毫秒内获得结果?
该表是一个缓存表,现在仅更新一次。