将查询与分隔数据相交。花费太长时间来检索数据。需要专家建议

时间:2013-12-13 17:18:50

标签: sql sql-server-2008

面对挑战编写SQL select语句以检索分隔数据。有人可以帮我建议如何优化此查询吗?

SELECT 
        [ID] as 'itemID', [ID] as 'noteLink', [ID] as 'editLink', [Firstname] as 'Firstname',[Lastname] as 'Lastname',[Company] as 'Company',
        [Address line 1] as 'Address1', [Address line 2] as 'Address2',[Address line 3] as Address3,[EmailAddress] as 'EmailAddress',
        [City] as 'City',[Phone] as 'Telephone', [Skills], [Career Level] as Careerlevel, [Newsletter Preferences] as NewsletterPreferences, 
        [Industries] 
    FROM [reportUserTable] 
        WHERE (ISNULL([company], '') = '') 
        AND SUBSTRING(added,1,8) > '20130716' 
        AND SUBSTRING(added,1,8) < '20131213' 
INTERSECT 
SELECT 
        [ID] as 'itemID', [ID] as 'noteLink', [ID] as 'editLink', [Firstname] as 'Firstname',[Lastname] as 'Lastname',[Company] as 'Company',
        [Address line 1] as 'Address1', [Address line 2] as 'Address2',[Address line 3] as Address3,[EmailAddress] as 'EmailAddress',[City] as 'City',
        [Phone] as 'Telephone', [Skills], [Career Level] as Careerlevel, [Newsletter Preferences] as NewsletterPreferences, [Industries] 
    FROM [reportUserTable] 
        WHERE (ISNULL([company], '') = '') 
        AND [Industries] LIKE '%{04F1BDA1-B693-4505-A816-6123B2CAEB04}%' 
        OR [Industries] LIKE '%{0A8A4015-1591-4D86-B8E7-E913FB500CA4}%'

enter image description here

1 个答案:

答案 0 :(得分:0)

我相信你最好将你的行业列表标准化为许多表格,但这应该更快一点,因为它不会投射两次然后相交:

SELECT 
    [ID] as 'itemID', [ID] as 'noteLink', [ID] as 'editLink', [Firstname] as 'Firstname',[Lastname] as 'Lastname',[Company] as 'Company',
    [Address line 1] as 'Address1', [Address line 2] as 'Address2',[Address line 3] as Address3,[EmailAddress] as 'EmailAddress',
    [City] as 'City',[Phone] as 'Telephone', [Skills], [Career Level] as Careerlevel, [Newsletter Preferences] as NewsletterPreferences, 
    [Industries] 
FROM [reportUserTable] 
    WHERE (ISNULL([company], '') = '') 
    AND SUBSTRING(added,1,8) > '20130716' 
    AND SUBSTRING(added,1,8) < '20131213' 
    AND [Industries] LIKE '%{04F1BDA1-B693-4505-A816-6123B2CAEB04}%' 
    OR [Industries] LIKE '%{0A8A4015-1591-4D86-B8E7-E913FB500CA4}%'