MS SQL Server - 通过网络批量插入

发布于 2024-07-11 13:31:18 字数 332 浏览 10 评论 0原文

我有一个使用 MS SQL Server 的应用程序,我需要从文件中进行批量插入。 症结在于数据库和我的应用程序将托管在不同的服务器上。 通过网络进行批量插入的最佳方法是什么? 到目前为止,我提出了两个想法:

  1. 从应用程序服务器,共享数据库服务器可以找到的目录,并使用来自远程文件的批量插入语句进行导入

  2. 从数据库服务器运行 FTP 服务器 - 执行导入时,只需将文件 ftp 到数据库服务器并使用本地文件中的批量插入进行导入(我倾向于此选项)。

其他人可以告诉我是否有更好的方法来做到这一点,或者如果没有,哪一种最有意义,为什么?

I have an application that uses MS SQL Server for which I'll need to do a bulk insert from a file. The sticking point is that the database and my application will be hosted on separate servers. What is the best way to do a bulk insert across a network? Two ideas I'd come up with so far:

  1. From the app server, share a directory that the db server can find, and do the import using a bulk insert statement from the remote file

  2. Run an FTP server from the db server - when the import is performed, simply ftp the file to the db server and do the import using a bulk insert from the local file (I am leaning towards this option).

Can anyone else tell me if there is a better way to do this, or if not, which one makes the most sense, and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

反目相谮 2024-07-18 13:31:18

我以前做过,并且尝试过这两种选择。

最后,我做了与选择 1 相反的操作。在数据库服务器上共享应用程序可以找到的目录。 您不必在批量插入期间处理带宽问题。

如果您特别关心安全性或可传输性,则可以使用 FTP 服务器选项。

最后一个选项(要非常小心)是将 DTS 与本地化 SQL 服务器一起使用。 它可能更安全。 如果做错了,效率就会大大降低。

I've done it before, and tried both options.

In the end, I did the opposite of choice 1. Share a directory on the DB server that the app can find. You don't have to deal with bandwidth issues during the bulk insert.

The FTP server option works if you're particularly concerned with security or transferability.

A final option (be very careful) is to use DTS with a localized SQL server. It might be more secure. If you do it wrong, it'll be much less efficient.

落在眉间の轻吻 2024-07-18 13:31:18

我仍在寻找一种在 MS SQL 中执行此操作的方法,但我在 MySQL 中所做的是将 CSV 作为 BLOB 保存在临时表中,在数据库服务器本地目录中运行 SELECT ... INTO DUMPFILE,然后在该本地文件上运行常规 LOAD DATA 语句(编辑:我觉得我应该指出,这是在 LOAD DATA LOCAL 可用之前)。

我认为 spWriteStringToFile 会做到这一点。

编辑:我正在做某事。

comm.CommandText = @"EXEC spWriteStringToFile @data, 'c:\datadumps', 'data.csv';
    BULK INSERT my_table FROM 'c:\datadumps\data.csv';";
comm.Parameters.AddWithValue("data", File.ReadAllText(path));
comm.ExecuteNonQuery();

I'm still looking for a way to do this in MS SQL, but what I did in MySQL was to save the CSV as a BLOB in a temporary table, run SELECT ... INTO DUMPFILE in a directory local to the DB server, then run the regular LOAD DATA statement on that local file (EDIT: I feel I should point out, this was before LOAD DATA LOCAL was available).

I think spWriteStringToFile will do it.

EDIT: I'm on to something.

comm.CommandText = @"EXEC spWriteStringToFile @data, 'c:\datadumps', 'data.csv';
    BULK INSERT my_table FROM 'c:\datadumps\data.csv';";
comm.Parameters.AddWithValue("data", File.ReadAllText(path));
comm.ExecuteNonQuery();
白昼 2024-07-18 13:31:18

如果文件足够小,那么 ftp 选项可能会起作用(您的数据库框中将有一个副本)。 不过,如果两者之间有一个单跳的千兆网络,我认为执行选项 1) 不会有太大问题。

If the file is small enough then the ftp option may work (you will have a duplicate in your db box). However I don't see much of a problem to do option 1) if you have a gigabit network between the two with a single hop.

蓝色星空 2024-07-18 13:31:18

我不确定 2k08,因为有一些额外的实用程序可以在服务器之间复制文件。 FTP 将是两者中更快的一个,因为它不使用 Windows 的文件系统来传输文件。 (存在开销,但除非文件很大,否则可能可以忽略不计)。 不使用共享还有其他优点,例如远程服务器在访问过程中文件崩溃。

我不同意 cmartin 关于向数据库服务器添加开放共享的观点。 通常,您不想打开数据库服务器的文件共享,因为它通常被认为存在安全风险,而且很多地方都不允许这样做。 话虽这么说,它会否定您将文件传输到另一个位置以使用批量导入。

I am not sure about 2k08 as there are some additional utilities to copy files between servers. FTP will be the faster of the two, because it doesn't use the window's file system to transfer the file. (there is overhead, but unless the file is large it might be negligible). There are other advantages to not using a share, such as the remote server with the file crashing mid-access.

I disagree with cmartin about adding an open share to the db server. Generally you don't want to open file shares to the db server as it is generally considered a security risk, and a lot of places won't allow it. That being said it would negate you having transfer the file to another location to use the bulk import.

甜扑 2024-07-18 13:31:18

实际上,您可以将该文件放置在应用程序服务器和数据库服务器上。

两个文件必须是相同的路径。 从应用程序服务器来看,目的仅用于选择。 数据库服务器用于批量插入。

这就是我为客户所做的,他们也同意这个简单的技巧。

Actually you can place the file at both the Application and Database server.

Both files must be the same path. From the Application server, the purpose is only for selection. The Database server is for the bulk insert.

This is what I did for my customer and they also agreed with this simple trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文