SQL:从表2中插入表1,然后更新表2-性能提高

发布于 2025-01-18 20:17:09 字数 1497 浏览 1 评论 0原文

我正在努力提高我继承的数据库过程的速度和性能。在此过程之前的基本步骤是公用事业将大约一百万或更多记录上传到上传表中。该过程很快,但是一旦我们开始将/更新/移动项目从上传表添加到数据库中的其他表中,事情就开始放慢速度。

我已经阅读了一些文章,说明如果不存在的话,可能比选择不同的时间更快,所以我正在考虑重构以下代码这样做,但我也想知道是否有一种方法可以结合这两个查询以增加速度。

上传表包含许多列,我只是在显示产品部分,但也有存储列的列数与产品的数量相同,而其他许多详细信息则不是表之间的一对一关系。

第一个查询将产品插入产品表(如果尚不存在)中,则下一步将更新上传表的上传表,其中包括上传表中的所有记录。

INSERT INTO Product (p.ProductCode, p.ProductDescription, p.ProductCodeQualifier, p.UnitOfMeasure)
    SELECT DISTINCT 
        ut.ProductCode, ut.ProductDescription, ut.ProductCodeQualifier, ut.UnitOfMeasure
    FROM 
        Upload_Table ut 
    LEFT JOIN
        Product p ON (ut.ProductCode = p.ProductCode) 
                  AND (ut.ProductDescription = p.ProductDescription) 
                  AND (ut.ProductCodeQualifier = p.ProductCodeQualifier)
                  AND (ut.UnitOfMeasure = p.UnitOfMeasure)
    WHERE 
        p.Id IS NULL
        AND ut.UploadId = 123456;
UPDATE Upload_Table
SET ProductId = Product.Id 
FROM Upload_Table 
INNER JOIN Product ON Upload_Table.ProductCode = Product.ProductCode 
                   AND Upload_Table.ProductDescription = Product.ProductDescription 
                   AND Upload_Table.ProductCodeQualifier = Product.ProductCodeQualifier 
                   AND Upload_Table.UnitOfMeasure = Product.UnitOfMeasure
WHERE (Upload_Table.UploadId = 123456)

任何帮助或建议将不胜感激。我对SQL的理解很不错,但我不是专家。

谢谢!

目前,由于我试图找到速度提高的最佳结果,并更好地了解如何改善此过程,因此目前尚未尝试对此部分进行任何更改。

I am working to increase the speed and performance for a database process that I have inherited. The basic steps, prior to this process, is a utility uploads about a million or more records into an Upload Table. That process is pretty quick, but things start to slow down once we start adding/updating/moving items from the Upload Table into other tables in the database.

I have read a few articles stating that using IF NOT EXIST may be quicker than SELECT DISTINCT so I was thinking about refactoring the following code to do so but I was also wondering if there is a way to combine these two queries in order to increase the speed.

The Upload Table contains many columns, I am just showing the Product portion but there is also Store Columns which has the same number of columns as the Product and many other details that are not a one-to-one relationship between tables.

The first query inserts the product into the Product table if it does not already exist, then the next step updates the Upload Table with Product IDs for all the records in the Upload Table.

INSERT INTO Product (p.ProductCode, p.ProductDescription, p.ProductCodeQualifier, p.UnitOfMeasure)
    SELECT DISTINCT 
        ut.ProductCode, ut.ProductDescription, ut.ProductCodeQualifier, ut.UnitOfMeasure
    FROM 
        Upload_Table ut 
    LEFT JOIN
        Product p ON (ut.ProductCode = p.ProductCode) 
                  AND (ut.ProductDescription = p.ProductDescription) 
                  AND (ut.ProductCodeQualifier = p.ProductCodeQualifier)
                  AND (ut.UnitOfMeasure = p.UnitOfMeasure)
    WHERE 
        p.Id IS NULL
        AND ut.UploadId = 123456;
UPDATE Upload_Table
SET ProductId = Product.Id 
FROM Upload_Table 
INNER JOIN Product ON Upload_Table.ProductCode = Product.ProductCode 
                   AND Upload_Table.ProductDescription = Product.ProductDescription 
                   AND Upload_Table.ProductCodeQualifier = Product.ProductCodeQualifier 
                   AND Upload_Table.UnitOfMeasure = Product.UnitOfMeasure
WHERE (Upload_Table.UploadId = 123456)

Any help or suggestions would be greatly appreciated. I am decent with my understanding of SQL but I am not an expert.

Thanks!

Currently have not tried to make any changes for this part as I am trying to find the best result for speed increases and a better understanding of how this process can be improved.

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

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

发布评论

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

评论(1

执笏见 2025-01-25 20:17:09

建议:

  1. 您可以在插入和更新之前禁用触发器,外键,约束和索引,在这些过程之后,您可以再次启用所有这些。因为索引,触发器和外国钥匙接受插入(更新)性能非常糟糕。

  2. 不建议在更新或插入过程中使用自动命令事务模式。这将获得非常糟糕的性能,每次插入记录后,每次都会在自动命令模式下自动进行交易。但是,为了获得最佳性能,我建议您仅在插入1000个记录(或10000之后)之后使用commit

  3. 如果可以的话,那么您可以每天定期多次执行此过程(插入或更新),也可以使用触发器进行此操作。我不知道您的业务逻辑,也许这种变体不会满足您的需求。

  4. ,不要忘记分析为您的查询执行计划

Recommendations:

  1. You can disable triggers, foreign keys, constraints, and indexes before inserting and updating, after these processes you can enable all these again. Because indexes, triggers, and foreign keys accept the inserting (updating) performance very badly.

  2. Is not recommended to use auto-commit transaction mode during the update or insert process. This is getting a very bad performance, on auto-commit mode every time for each after inserting records the transactions automatically will be committed. But, for best performance, I recommended you use commit only after inserting 1000 records (or after 10000).

  3. If you can, then you can do this process (insert or update) periodically multiple times a day, you can also do this using triggers. I don't know your business logic, maybe this variant will not satisfy you.

  4. And don't forget to analyze executing plan for your queries.

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