我有两个基本相同代码的版本。 (见下文)版本1运行时间为2秒,版本2运行时间为.5 - .6秒。目前有1000万行来自我选择的地方,但这个数字上升得非常快。如果可能的话,我想更低。问题是我使用的是版本2,我需要调用30次(不同的状态,不同的用户名等),最终的数字仍然太大而不能满足我的需要。我可以使用第三个版本吗?或者还有其他方法可以让它更快吗?或者我唯一能做的就是玩索引。
基本上所有这些计数都将显示在Web应用程序中访问量最大的屏幕上,当1000个用户同时使用系统时,30 * .5秒听起来有点太多了。
版本1
aerospike@2.0.3
第2版
declare @a1 datetime; set @a1 = GETDATE()
declare @int1 INT,@int2 INT,@int3 INT,@int4 INT,@int5 INT,@int6 INT,@int7 INT,@int8 INT
select @int1 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'a'
select @int2 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'b'
select @int3 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'c'
select @int4 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'd'
select @int5 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'e'
select @int6 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'f'
select @int7 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'g'
select @int8 = COUNT(Id) from ToDos where StatusId = 1 and stringUserId = 'h'
select @int1, @int2, @int3, @int4, @int5, @int6, @int7, @int8
SELECT DATEDIFF(MILLISECOND, @a1, GETDATE())
答案 0 :(得分:1)
尝试条件计数。
select
@int1 = COUNT(CASE WHEN stringUserId = 'a' THEN 1 END),
@int2 = COUNT(CASE WHEN stringUserId = 'b' THEN 1 END),
@int3 = COUNT(CASE WHEN stringUserId = 'c' THEN 1 END),
@int4 = COUNT(CASE WHEN stringUserId = 'd' THEN 1 END),
@int5 = COUNT(CASE WHEN stringUserId = 'e' THEN 1 END),
@int6 = COUNT(CASE WHEN stringUserId = 'f' THEN 1 END),
@int7 = COUNT(CASE WHEN stringUserId = 'g' THEN 1 END),
@int8 = COUNT(CASE WHEN stringUserId = 'h' THEN 1 END)
from ToDos
where StatusId = 1
仅供参考:我没有为ELSE
添加CASE
部分,因为默认情况下会返回NULL
而COUNT
不会计算空值
答案 1 :(得分:0)
你可以尝试:
select a.* from (select stringUserId, count(stringUserId) as IDCount
from ToDos
where StatusId = 1 and stringUserId in ('a','b','c','d','e','f','g','h')
group by stringUserId) a
order by a.IDCount desc
通过
从订单中删除计数功能