日期:2014-05-18  浏览次数:20564 次

Insert select需要87S,Select出来才只要10S,求解.
越来越对数据库这块感兴趣了.求解.

顺便再问一句,建立索引之后是不是数据的插入速度会慢许多....

SQL code

    SELECT
        DISTINCT
        C.[Firm Name],
        C.[Address Line 1] + ' ' + C.[Address Line 2],
        C.[City],
        C.[State],
        C.[Zip Code],
        C.[Bus# Phone Number],
        C.[Bus# FaxNumber],
        C.[Email],
        C.[Contact First Name],
        C.[Contact Last Name],
        @source,
        GETDATE(),
        @agencyID
        FROM dbo.CADBEImportData AS C LEFT JOIN AgencyCertifiedCompanies AS A
        ON C.[Firm Name] = A.[CompanyName] AND A.[Source] = @source
        WHERE A.[CompanyName] IS NULL AND C.[Firm Name] <> ''


SQL code

    INSERT INTO AgencyCertifiedCompanies(
        [CompanyName],
        [Address],
        [City],
        [State],
        [Zip],
        [Phone],
        [Fax],
        [Email],
        [ContactFirstName],
        [ContactLastName],
        [Source],
        [AddedDate],
        [CertificationIssuerID]
    ) 
    SELECT
        DISTINCT
        C.[Firm Name],
        C.[Address Line 1] + ' ' + C.[Address Line 2],
        C.[City],
        C.[State],
        C.[Zip Code],
        C.[Bus# Phone Number],
        C.[Bus# FaxNumber],
        C.[Email],
        C.[Contact First Name],
        C.[Contact Last Name],
        @source,
        GETDATE(),
        @agencyID
        FROM dbo.CADBEImportData AS C LEFT JOIN AgencyCertifiedCompanies AS A
        ON C.[Firm Name] = A.[CompanyName] AND A.[Source] = @source
        WHERE A.[CompanyName] IS NULL AND C.[Firm Name] <> ''




------解决方案--------------------
数据量不大的情况不要用索引!
------解决方案--------------------
弄个简单的例子

SQL code
CREATE TABLE tablename(id int)

insert into tablenameselect 1

select * from tablename