c# - SqlGeography 空间操作缓慢 - SQL Server 2016

标签 c# .net sql-server performance sql-server-2016

我已经在 SQL Server 2016 中的新空间库 SqlGeography 上运行了一些测试,根据 Microsoft 的说法,它应该比以前的版本快很多:

SQL Server 2016 – It Just Runs Faster: Native Spatial Implementation(s). Apply SQL Server 2016 and a breadth of methods and spatial activities are faster and scale better. There are no application or database changes just the SQL Server binary updates showing dramatic improvement.

但是,测试表明新库比旧库慢。 我已经使用 Microsoft 发布的 Nugets Microsoft.SqlServer.Types 在 C# 中对其进行了测试。我已经针对版本 14 (SQL Server 2016) 测试了版本 11。

我应该怎么做才能让新的空间库表现得更好?

小测试的源码是:

var line1 = CreateLine(56, -4, 58, 16);
var line2 = CreateLine(58, -4, 56, 16);

for (int i = 0; i < 50000; i++)
{
     var intersection = line1.STIntersects(line2);
     var contains = line1.STBuffer(1000).STContains(line1);
}

public static SqlGeography CreateLine(double fromLat, double fromLon, double toLat, double toLon)
{
     SqlGeographyBuilder constructed = new SqlGeographyBuilder();
     constructed.SetSrid(4326);
     constructed.BeginGeography(OpenGisGeographyType.LineString);
     constructed.BeginFigure(fromLat, fromLon);
     constructed.AddLine(toLat, toLon);
     constructed.EndFigure();
     constructed.EndGeography();
     var line = constructed.ConstructedGeography;
     return line;
 }

最佳答案

Microsoft 在这篇文章中写道,Microsoft.SqlServer.Types 在 SQL Server 2016 的 T-SQL 代码中不再使用。 https://blogs.msdn.microsoft.com/psssql/2016/03/03/sql-2016-it-just-runs-faster-native-spatial-implementations/

它在 SQL 2014 中的工作原理:

As the SQL Server Spatial data types have matured we uncovered the Unmanaged (SQL Server) to Managed (Microsoft.SqlServer.Types) to SqlServerSpatial###.dll (unmanaged) transitions (PInvoke and PUnInvoke) may become a scalability bottleneck

在 SQL 2016 中:

(T-SQL) SQL Server 2016 invokes the native implementation of the methods, avoiding the Unmanaged to Managed to Unmanaged transitions, improving performance.

SQL 2016 似乎直接使用了 SqlServerSpatial###.dll 并且仅在 T-SQL 代码中提高了性能

关于c# - SqlGeography 空间操作缓慢 - SQL Server 2016,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818613/

相关文章:

sql-server - 在 SQL Server 中存储具有标准结构的 XML 是对 XML 数据类型的错误使用吗?

c# - 使用 C# 将数据 append 到现有的 Excel 文件

c# - 将一个 int 添加到它的现有数组中

c# - 如何使用PCM 8kHZ和每个样本16位的Naudio loopbackcapture独占模式

.net - XSL 排序问题

c# - Directory.CreateDirectory 是异步的还是什么?

c# - 无法打开登录请求的数据库 ""。登录失败。用户 'sa' 登录失败

asp.net - 对于一个存储过程调用,SQL 登录从 Web 应用程序失败

c# - 将 Properties.Settings.Default 项目保存到列表中,更改值,然后将其保存到文件中

c# - 非通用 TaskCompletionSource 或替代方案