I have this query that runs in 7 seconds producing 12,094 records on my local instance of SQL Server 2008 R2 (non express). However, it takes 38 seconds on an instance of Sql Server Express. It might just be the difference between a local 8gb Hyper-V machine and a remote GoGrid machine with half the memory.
My question is am I doing anything obviously inefficient here?
Query:
declare @FromTimestamp datetime = '2012-02-01'
declare @ToTimestamp datetime = '2012-02-22'
DECLARE @to datetime = DATEADD(dd, 1, @ToTimestamp)
SELECT
Data.value('(/LendingTreeAffiliateRequest/Request/@AppID)[1]', 'varchar(50)') AS AppID,
Data.value('(/LendingTreeAffiliateRequest/Request/@type)[1]', 'varchar(50)') AS [Type],
[Timestamp],
Data.value('(/LendingTreeAffiliateRequest/@affid)[1]', 'varchar(50)') AS CDNumber,
Data.value('(/LendingTreeAffiliateRequest/Request/SourceOfRequest/VisitorIPAddress)[1]', 'varchar(50)') AS IP,
Data.value('(/LendingTreeAffiliateRequest/Request/SourceOfRequest/LendingTreeAffiliateEsourceID)[1]', 'varchar(50)') AS ESourceID,
Data.value('(/LendingTreeAffiliateRequest/Request/Applicant/State)[1]', 'char(2)') AS [State],
Data.value('(/LendingTreeAffiliateRequest/Request/Applicant/DateOfBirth)[1]', 'varchar(20)') AS DateOfBirth,
Data.value('(/LendingTreeAffiliateRequest/Request/Applicant/EmailAddress)[1]', 'varchar(255)') AS Email,
Data.value('(/LendingTreeAffiliateRequest/Request/Applicant/IsVeteran)[1]', 'char(1)') AS IsVeteran,
Data.value('(/LendingTreeAffiliateRequest/Request/Applicant/CreditHistory/CreditSelfRating)[1]', 'varchar(50)') AS Credit
FROM
WebLogEntry wle
INNER JOIN EventType et on wle.EventTypeId=et.Id
INNER JOIN [EventData] ed on wle.EventDataId=ed.Id
WHERE
(et.Id=2)
AND (Data.exist('/LendingTreeAffiliateRequest/Request/@AppID')=1)
AND ([Timestamp] BETWEEN @FromTimestamp AND @to)
I don’t see anything obviously wrong other than the fact that the SQL server is going to have to churn on an awful lot of xquery. You might want to try returning the data field in its entirety and then parse it in code to see if that is faster with your particular implementation. Measure the time of both methods and go with the faster one.
If you have a predictable data structure, you are better off storing that in a table rather than XML blobs as that will speed things up significantly.