简体   繁体   English

应用中的大插入量-性能

[英]Large inserts from app - performance

I am having to create a custom tool in .NET to data migrate and cleanse an existing system. 我必须在.NET中创建一个自定义工具,以进行数据迁移和清理现有系统。 And it also does some other things. 而且它还做其他一些事情。

I have literally a table of around 2000 users and then for each of those users, they can have anything from 0 to 9,000 "customer accounts" 我实际上有一个大约2000个用户的表,然后对于这些用户中的每个用户,他们可以拥有0到9,000个“客户帐户”

then for each of these users and their accounts, they will need to insert a menu system into another table for that user and account. 然后,对于这些用户及其帐户中的每一个,他们将需要在该用户和帐户的另一个表格中插入菜单系统。

So I am having to go through all these objects created within the app, and execute an insert statement. 因此,我必须遍历在应用程序内创建的所有这些对象,并执行插入语句。 The problem is, the performance is horrible. 问题是,性能太差了。 It took 3.5 hours to complete the inserts and each insert took around 3 seconds to complete.... pretty slow! 完成插入需要3.5个小时,每个插入大约需要3秒钟才能完成。

With the inserts, I am making sure that I dont insert duplicate data/make sure there isnt any existing duplicate data thus my insert statment: 通过插入,我确保不插入重复数据/确保不存在任何现有重复数据,因此我要执行插入语句:

 IF NOT EXISTS (SELECT ID FROM UserWebAccessLevel WHERE UserID = '{0}' AND CustomerID = '{1}' AND MenuID = {2}) BEGIN INSERT INTO [WebAccessLevel] (UserID, CustomerID, MenuID) VALUES ('{0}', '{1}', {2}) END 

doing this for each user, for their customers and for each menu item found within the app logic.... yeh, takes a long time. 为每个用户,他们的顾客以及在应用程序逻辑中找到的每个菜单项执行此操作。。。是的,这需要很长时间。

im wondering how I can better enhance the performance of the insert statement? 我想知道如何才能更好地增强插入语句的性能?

Thank you 谢谢

Without an index on your table, the SELECT ID FROM... could be slowing things down, as SQL Server has to look at each record one by one and compare the IDs in the WHERE clause. 在表上没有索引的情况下, SELECT ID FROM...可能会减慢速度,因为SQL Server必须逐个查看每个记录并比较WHERE子句中的ID。 By adding an index containing the three IDs, SQL Server will be able to (more-or-less) instantly locate an existing record when a WHERE clause specifies the three IDs. 通过添加包含三个ID的索引,当WHERE子句指定三个ID时,SQL Server将能够(或多或少)立即定位现有记录。

Assuming you are using SQL Server Management Studio:- 假设您使用的是SQL Server Management Studio:

  • Right-click the table then click "Design". 右键单击该表,然后单击“设计”。
  • Right-click where the list of columns has appeared, then click "Indexes/Keys". 右键单击出现在列列表中的位置,然后单击“索引/键”。
  • in the next dialog that appears, click the Add button 在出现的下一个对话框中,单击添加按钮
  • In the list of properties on the rh side, find the one called "Columns" and click the box to the right of it. 在rh侧的属性列表中,找到一个名为“ Columns”的列,然后单击其右侧的框。 When you see the ellipses button ("...") click that. 当您看到省略号按钮(“ ...”)时,请单击该按钮。
  • In the next dialog that appears, add your three ID columns. 在出现的下一个对话框中,添加三个ID列。

An index doesn't have to be unique. 索引不必是唯一的。 In the "Indexes/Keys" dialog you get the option to specify whether it should be unique or not. 在“索引/键”对话框中,您可以选择指定其是否唯一。

The only other thing I can suggest checking is in your C#/VB code that executes the IF NOT EXISTS... SQL - make sure you aren't opening and closing the connection each time. 我唯一建议检查的另一件事是在执行IF NOT EXISTS... SQL的C#/ VB代码中-确保您没有每次都打开和关闭连接。 That will really slow things down! 真的会减慢速度!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM