2011-10-11 61 views
0

我的经理给了我一个项目,我一直在使用jQuery Calendar插件来显示存储在我们的SQL数据库的各种表中的日历数据。使用.NET构建JSON?

这只是一个jQuery插件,它接收静态json数据并在日历上呈现它。我必须将它与.net和我们的SQL数据库集成,以便日历可以从SQL数据库(Microsoft SQL)呈现数据。

最初,我们将这一切放在一起,以便我们将所有来自SQL服务器的数据,然后使用.Net构建json,然后将其传递到jQuery日历插件。

虽然原则上这个效果很好,但是速度非常慢,IIS经常超时。更何况,每次我们任何人想查看日历,我们必须等待3分钟左右,因为入口数量已接近3000.

查询相当复杂,它们使用的是动态的Dateadd和DateDiff函数和一系列的行动方式。对于查询,单独SQL服务器上的执行时间大约为90秒。总查询大小约为160kb。

然后,我们将查询分为3部分(针对不同的部门),但是我们必须等待日历绘制的时间仍然超过一分钟。

这里是查询的只是其中的一个例子,但有超过这100每个部门

CREATE TABLE #AnnualLastMonImportantCustomDate(
    Title varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    AllocatedDate varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    EndDateTime varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    url varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    width varchar(10) COLLATE Latin1_General_CI_AS NULL, 
    height varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    AllDay varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    description varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    color varchar(550) COLLATE Latin1_General_CI_AS NULL, 
    textColor varchar(550) COLLATE Latin1_General_CI_AS NULL 
) 

DECLARE db_cursor CURSOR FOR SELECT AlertDate FROM xsCRMAlerts 
WHERE AlertType='InternalImportantDate' 
-- cursor is the results row when table goes through fetch process 

SET @MyTableName='xsCRMAlerts' 

OPEN db_cursor -- opens the table and stores id, which is the primary key in the table 
FETCH NEXT FROM db_cursor INTO @MyTableName -- @MyTableName in this case is the result row. 

WHILE @@FETCH_STATUS = 0 -- 0 is for success -1 is for too many results -2 is for the row fetched is missing 
BEGIN 

-- Below between begin and end the statement is linked to a function, which gives the dates tabled based on a start date. This table is then cross joined to produce desired result. 
SET @startDate = @MyTableName -- we can set the start date to all the data we recieved because we have only asked for one field in our @MyTableName query when db_cursor was being drawn 
INSERT INTO #AnnualLastMonImportantCustomDate 

     SELECT 
     'Important Date : ' + [Title] as 'Title', 
     dr.date as 'AllocatedDate', 
     dr.date as 'EndDateTime' , 

     'xsCRM_Dates_Edit.aspx?id=' + cast(id as varchar) as 'url' , 
     '515px' as 'width', 
     '410px' as 'height',  
     'true' as 'allDay', 
     'Important date' as 'description', /* This is a static entry and will not show on the calendar. Used when redering object*/ 
     'yellow' as 'color', 
     'black' as 'textColor' 
     FROM [DelphiDude].[dbo].[xsCRMAlerts] 
     cross JOIN 

     dateTable(
     DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) 
     , 
     DateAdd(yy,1,DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)))) 
     ) dr -- You can specify intervals by calling DateTable_Month, DateTable_Quarter, DateTable_BiAnnual and DateTable_Annual 

     WHERE 
     (AlertType='InternalImportantDate') and 
     (occurring='765') and 
     (Datepart(m,date) = 12) and 
     (Datepart(day,date) > 24) and 
     (Datepart(dw,date) = 2) and 

     (Datepart(year,date) = (Datepart(year,getDate()) + 1)) 

    FETCH NEXT FROM db_cursor INTO @MyTableName -- gets the next record from the table 
END 

CLOSE db_cursor 
DEALLOCATE db_cursor 

我们真的需要这些查询。

我们现在考虑将结果集限制在prev和未来30天。但是,每次我们优化查询时,我(即使它只是使用查找和替换)必须在每个模块的100个查询中复制该更改。

有没有一种方法可以优化这些查询,并加速执行和日历渲染时间,这是明确的,并通过长时间改善它?是否有一种方法可以将这些更改应用到每个查询中进行复制?

我建议使用缓存,数据库缓存和对象缓存给我的老板,但他说数据会经常变化,并且来自这里的数据需要传递到其他模块,因此如果它被缓存可能是不准确的。我没有足够的经验来竞争他所说的话。

有任何建议吗?

回答

1

在您发布的查询中,游标无用,因为您从不在插入查询中使用@startDate或@MyTableName变量。

因此,很多重复行可能会插入到您的临时表中。

同时,尽量使用一个CTE表变量,而不是“#Temporary表”,因为“#Temporary表”中的数据是在文件系统中存储phisically并耗费了大量的I/O增加执行时间。

最后的建议:不要忘记在你的xsCRMAlerts表上创建聚簇/非聚簇索引。如果您使用的是SQL Server Management Studio,则执行计划或数据库引擎优化顾问工具可以帮助您找到缺失的索引。

希望这会有所帮助:)