4

I am trying to do a query to retreive the number of active calls for a given time,

I have a table CALLS with all the calls record with an AnsweredTime and ReleaseTime and I need to retrieve the number of calls active for every second during the day.

So I've done the following :

    SELECT dt,
(SELECT COUNT(Id) FROM Calls WHERE
    AnsweredTimestamp <= dt AND
    ReleasedTimestamp >= dt)
 FROM CALView
WHERE dt >='2011-10-05'
and dt <'2011-10-06'

CalView is a table containing every second between two dates

The query return this (partialy) :

2011-10-05 09:40:00.000 20
2011-10-05 09:40:01.000 20
2011-10-05 09:40:02.000 20
2011-10-05 09:40:03.000 21
2011-10-05 09:40:04.000 21
2011-10-05 09:40:05.000 21
2011-10-05 09:40:06.000 21
2011-10-05 09:40:07.000 21
2011-10-05 09:40:08.000 21
2011-10-05 09:40:09.000 21
2011-10-05 09:40:10.000 20
2011-10-05 09:40:11.000 20
2011-10-05 09:40:12.000 19
2011-10-05 09:40:13.000 19
2011-10-05 09:40:14.000 19
2011-10-05 09:40:15.000 19
2011-10-05 09:40:16.000 19
2011-10-05 09:40:17.000 19
2011-10-05 09:40:18.000 19
2011-10-05 09:40:19.000 19
2011-10-05 09:40:20.000 18
2011-10-05 09:40:21.000 18
2011-10-05 09:40:22.000 18
2011-10-05 09:40:23.000 18
2011-10-05 09:40:24.000 18
2011-10-05 09:40:25.000 18
2011-10-05 09:40:26.000 18
2011-10-05 09:40:27.000 18
2011-10-05 09:40:28.000 18
2011-10-05 09:40:29.000 18
2011-10-05 09:40:30.000 18
2011-10-05 09:40:31.000 18
2011-10-05 09:40:32.000 18
2011-10-05 09:40:33.000 18
2011-10-05 09:40:34.000 18
2011-10-05 09:40:35.000 19
2011-10-05 09:40:36.000 18
2011-10-05 09:40:37.000 18
2011-10-05 09:40:38.000 18
2011-10-05 09:40:39.000 18
2011-10-05 09:40:40.000 18
2011-10-05 09:40:41.000 18
2011-10-05 09:40:42.000 18
2011-10-05 09:40:43.000 18
2011-10-05 09:40:44.000 19
2011-10-05 09:40:45.000 19
2011-10-05 09:40:46.000 19
2011-10-05 09:40:47.000 19
2011-10-05 09:40:48.000 19
2011-10-05 09:40:49.000 19
2011-10-05 09:40:50.000 19
2011-10-05 09:40:51.000 19
2011-10-05 09:40:52.000 19
2011-10-05 09:40:53.000 19
2011-10-05 09:40:54.000 19
2011-10-05 09:40:55.000 19
2011-10-05 09:40:56.000 19
2011-10-05 09:40:57.000 19
2011-10-05 09:40:58.000 20
2011-10-05 09:40:59.000 20
2011-10-05 09:41:00.000 20
2011-10-05 09:41:01.000 20
2011-10-05 09:41:02.000 20
2011-10-05 09:41:03.000 20
2011-10-05 09:41:04.000 20
2011-10-05 09:41:05.000 20
2011-10-05 09:41:06.000 20
2011-10-05 09:41:07.000 20
2011-10-05 09:41:08.000 20
2011-10-05 09:41:09.000 19
2011-10-05 09:41:10.000 19
2011-10-05 09:41:11.000 19
2011-10-05 09:41:12.000 19
2011-10-05 09:41:13.000 19
2011-10-05 09:41:14.000 19
2011-10-05 09:41:15.000 19
2011-10-05 09:41:16.000 20
2011-10-05 09:41:17.000 20
2011-10-05 09:41:18.000 20
2011-10-05 09:41:19.000 20
2011-10-05 09:41:20.000 20
2011-10-05 09:41:21.000 20
2011-10-05 09:41:22.000 20
2011-10-05 09:41:23.000 20
2011-10-05 09:41:24.000 20
2011-10-05 09:41:25.000 20
2011-10-05 09:41:26.000 20
2011-10-05 09:41:27.000 20
2011-10-05 09:41:28.000 20
2011-10-05 09:41:29.000 20
2011-10-05 09:41:30.000 19
2011-10-05 09:41:31.000 19
2011-10-05 09:41:32.000 19
2011-10-05 09:41:33.000 19
2011-10-05 09:41:34.000 20
2011-10-05 09:41:35.000 20
2011-10-05 09:41:36.000 20
2011-10-05 09:41:37.000 19
2011-10-05 09:41:38.000 19
2011-10-05 09:41:39.000 19
2011-10-05 09:41:40.000 19
2011-10-05 09:41:41.000 19
2011-10-05 09:41:42.000 19
2011-10-05 09:41:43.000 19
2011-10-05 09:41:44.000 19
2011-10-05 09:41:45.000 19
2011-10-05 09:41:46.000 18
2011-10-05 09:41:47.000 18
2011-10-05 09:41:48.000 19
2011-10-05 09:41:49.000 20
2011-10-05 09:41:50.000 20
2011-10-05 09:41:51.000 20
2011-10-05 09:41:52.000 20
2011-10-05 09:41:53.000 20

So I got the good result but the query is extremly slow, I've done the indexing, I am just wondering if there is something more I can do..

4

1 回答 1

6

您将在此处创建一个有趣的笛卡尔坐标。

Select dt,count(1)
from calview
left join calls on calls.AnsweredTimestamp <= calview.dt AND
                    calls.ReleasedTimestamp >= calview.dt
WHERE dt >='2011-10-05'
and dt <'2011-10-06'
group by dt

我相信这应该给你一排每一秒一个呼叫是活跃的......然后这是一个简单的计数。希望我的语法是正确的,我没有要确认的 ms sql 服务器环境。

ETA:使用了左连接,因此任何有 0 次呼叫的秒数都会出现在此处,计数为空。如果您切换到内部连接,则该行将在 1 秒的时间间隔内消失。

于 2011-11-24T20:02:23.660 回答