1

从 SQL Server 2008 R2 获取大约 7Mb 的数据到客户端大约需要 5 秒。该机是比较强大的 AMD 12 Core,64Gb RAM,Windows Server 2008,2 10Gbit 卡。在服务器上运行 select 甚至更慢,然后从客户端运行。将 7Mb 文件从该服务器复制到本地工作站大约需要 500 毫秒。

这是一个小型复制器:

--create test table for reproducer
CREATE TABLE [dbo].[Test_Speed](
    [ED] [datetime] NULL
) ON [PRIMARY]

  --fill test table with data, insert took 3:51 mins
  declare @r int
  set @r = 1
  while (@r < 830000)
  begin 
  insert into [CDB_ODS].[dbo].[Test_Speed] select getdate()
  set @r = @r+1
  end

  --select all records, roughly 7Mb. 4 secs if run on the client, 5 secs on the server (1.4Mb sec)
  select ed from [dbo].[Test_Speed]


  /*
  SELECT on CLIENT
    SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.
Table 'Test_Speed'. Scan count 1, logical reads 1833, physical reads 0, read-ahead reads 0, 
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

 SQL Server Execution Times:
   CPU time = 281 ms,  elapsed time = 4020 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.


   --- SELECT on SERVER 
   SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

(829999 row(s) affected)
Table 'Test_Speed'. Scan count 1, logical reads 1833, physical reads 0, read-ahead reads 0,
 lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

 SQL Server Execution Times:
   CPU time = 328 ms,  elapsed time = 5369 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

   */
4

2 回答 2

0

是的,这对我来说似乎比较正常。7Mbytes 是相当数量的数据。

性能高度依赖于客户端。当然,诸如网络速度之类的东西会产生影响。更大的影响是沿途各个点的缓冲区大小。为了获得最快的性能,您希望 SQL Server 一次性发送大量结果,这些结果可以快速放入应用程序中。

其他选项也有很大的影响。在 Excel/VBA 中,有不同的数据导入方法。如果使用游标,则需要一个只读的向前读游标。其他任何事情都可以慢得多。例如,一种游标将一次读取一行数据——因此延迟成为性能而不是带宽的主要因素(我认为这是可更新的游标,但我并不肯定)。

您需要详细说明您的客户以及它如何访问数据。这很可能是客户端问题,而不是数据库问题。

于 2012-07-25T13:23:18.410 回答
0

主键、索引查找、索引扫描、执行计划,这些都是你的朋友。检查此链接以获取索引主题。这个论坛帖子也可以帮助理解主键和外键。关于执行计划的分析和理解查看这个链接

在我工作的数据库上,检索相似数量的信息,数千行,花费不到一秒钟的时间。考虑您的数据库设计并稍后通过测试不同的方法调整查询来提高性能非常重要,并不总是投入更多的 CPU/RAM 功率是最好的方法

已编辑

我对你的评论感到有些讽刺,希望不是你的目标。使用我刚刚完成的以下测试成为我的客人(以前不可能这样做,现在我下班了)。在我的情况下,插入大约是 20-30 秒,因为我使用了更好的方法 (CTE) 来插入近 100 万行。比较时间你会发现使用主键比不使用它快 1 秒多一点。同样在服务器上通常快 1 秒。

CREATE TABLE [dbo].[Test_Speed]([ED] [DATETIME2] NULL) ON [PRIMARY]
CREATE UNIQUE CLUSTERED INDEX Idx1 ON Test_Speed(ED);

DECLARE @firstdate DATETIME2='00010101 00:00:00', 
        @lastdate DATETIME2= '25001231 00:00:00'

;WITH CTE_DatesTable
  AS
  (
    SELECT @FirstDate AS auxDate
    UNION ALL
    SELECT DATEADD(dd, 1, auxDate)
    FROM CTE_DatesTable
    WHERE DATEADD(dd, 1, auxDate) <= @LastDate
  )
  INSERT INTO [dbo].[Test_Speed]
  SELECT auxDate FROM CTE_DatesTable
  OPTION (MAXRECURSION 0);

  SET STATISTICS TIME ON;
  SET STATISTICS IO ON;
  SELECT ED FROM [dbo].[Test_Speed]
  SET STATISTICS TIME OFF;
  SET STATISTICS IO OFF;



/*
    Select on Client with primary key , insertion 21s
    (913106 row(s) affected)
     SQL Server Execution Times:
       CPU time = 0 ms,  elapsed time = 0 ms.
    (913106 row(s) affected)
    Table 'Test_Speed'. Scan count 1, logical reads 1919, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
     SQL Server Execution Times:
       CPU time = 375 ms,  elapsed time = 7790 ms   

    Select on Client without primary key , insertion 31s   
    SQL Server Execution Times:
       CPU time = 0 ms,  elapsed time = 0 ms.
    (913106 row(s) affected)
    Table 'Test_Speed'. Scan count 1, logical reads 1931, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    (1 row(s) affected)
     SQL Server Execution Times:
       CPU time = 405 ms,  elapsed time = 8049 ms.

    ------------------  

    Select on Server with Primary key, insertion 19s
    (913106 row(s) affected)
     SQL Server Execution Times:
       CPU time = 0 ms,  elapsed time = 0 ms.
    (913106 row(s) affected)
    Table 'Test_Speed'. Scan count 1, logical reads 1931, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
     SQL Server Execution Times:
       CPU time = 187 ms,  elapsed time = 6164 ms.   

    Select on Server without Primary key, insertion 25s
     (913106 row(s) affected)
     SQL Server Execution Times:
       CPU time = 0 ms,  elapsed time = 0 ms.
    (913106 row(s) affected)
    Table 'Test_Speed'. Scan count 1, logical reads 1919, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
     SQL Server Execution Times:
       CPU time = 343 ms,  elapsed time = 7407 ms.
*/
于 2012-07-25T13:27:22.810 回答