SQL Server创建的表运行缓慢

时间:2016-01-15 19:54:50

标签: sql sql-server

我正在创建一个名为#PILOTTERR的临时表。我需要在此表中插入超过40,000条记录。执行大约需要10分钟。有没有办法让我更快?

CREATE TABLE #PILOTTERR 
(
     Zip TEXT, 
     Office CHAR(4), 
     Branch NVARCHAR(33), 
     District NVARCHAR(37), 
     Region NVARCHAR(26), 
     SE_Territory NVARCHAR(42), 
     ISE_Territory NVARCHAR(43)
);

INSERT INTO #PILOTTERR 
VALUES ('00544','NY04','Long Island','New York-Long Island','US Northeast','SE-New York/Long Island-A','ISE-New York/Long Island-D')

INSERT INTO #PILOTTERR 
VALUES ('01001','MA01','North Boston','North Boston','US Northeast','SE-North Boston-C','ISE-North Boston-B')
    etc....

1 个答案:

答案 0 :(得分:2)

尝试这种方法:

CREATE TABLE #PILOTTERR (
                    Zip NVARCHAR(10), 
                    Office CHAR(4), 
                    Branch NVARCHAR(33), 
                    District NVARCHAR(37), 
                    Region NVARCHAR(26), 
                    SE_Territory NVARCHAR(42), 
                    ISE_Territory NVARCHAR(43)
                    );

而不是多次插入语句只有1:

    INSERT INTO #PILOTTERR 
    VALUES (N'00544','NY04',N'Long Island',N'New York-Long Island',N'US Northeast',N'SE-New York/Long Island-A',N'ISE-New York/Long Island-D'),
           (N'01001','MA01',N'North Boston',N'North Boston',N'US Northeast',N'SE-North Boston-C',N'ISE-North Boston-B'),
         (etc..)

如果您的数据类型是NVARCHAR,那么以N''方式开始引号,服务器不必从VARCHAR转换为NVARCHAR。

用一个小得多的敏感数据类型替换TEXT,比如NVARCHAR(10),我觉得它应该足够ZIPCODE

更好的是,正如我在评论中提到的,如果您从excel或csv获取数据,则可以使用SQL Server的导入数据向导。您可以找到有关它的更多信息HERE