CONTEXT_INFO and efficient mass update on a large table with a trigger
Let`s assume we have following task – make changes to the data in a large table that has consistency checker in a trigger. Let`s look at the ways to do it in a most efficient way.
Use test database in a simple recovery mode. Create two tables and fill them by random data.
Product table:
Finally we have table with about 5 million product records
Create order table that references the product table
Orders may have close date (closed orders):
20 million orders
And orders may not have close date (not closed orders):
About 5 million records
Let`s say we have a rule that says:
“Record in a product table can’t be modified unless there are not closed orders related to its products.”
If we want to change names for products with id <= 1M we have to wait some time:
45 seconds duration on my test server
Can we improve it?
Split update for some small bunches of 100 thousands records:
This loop worked 10 times and took 15 seconds to run. So we got a 3 times improvement. Not that this is specific to the server.
Can we speed it up more? If we are sure that our changes don’t require trigger’s check we can switch off the trigger. But then the trigger will not work for any other users too.
Is there a possibility to disable a trigger in our current session only?
Yes! Add the following hint to the trigger:
Try it now:
Running time – 12 seconds
Because we set special flag context_info for our session, trigger doesn’t fire for our session. But it fires for all other users`s sessions that do not have this flag enabled.
Finally we have an option to run mass DML queries bypassing trigger restrictions.
by Alexey Tikhomirov
Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.
MoreSubject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
MoreIn our time of greatest prevalence of databases were relational databases, which are the main targets of the tables and the relationships between them. Tables can solve most problems for data storage and manipulation. But in the real world entity requiring storage is not always presented in a tabular form. One of these very common types of data structures other than the table is a tree structure, where each data element is the parent and the offspring. An example of such a structure may be the structure of state enterprises, which is headed by the director (the root of the tree), his deputies, heads of departments from which are subject to certain deputies, employees of departments, which are subject to the rulers.
MoreSubscribe to our blog
Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.
Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
Discuss your project with us