Conditionally Updating Columns When The use of ON DUPLICATE KEY UPDATE In MySQL

Conditionally Updating Columns When The use of ON DUPLICATE KEY UPDATE In MySQL

[ad_1]

A few weeks in the past, I talked concerning the classes I discovered whilst sending 7M emails the usage of ColdFusion. In that submit, I discussed that I had to retailer a cache of e mail validation codes so as to steer clear of sending an e mail to any deal with that used to be identified to be invalid. However, an invalid e mail deal with is not the one reason why to skip a given ship. If a person explicitly unsubscribes from a printed circulation, I want to put out of your mind the given person within the subsequent ship (or my SMTP supplier—Postmark—will mark the e-mail as bounced).

When a person explicitly unsubscribes from the published circulation, I replace their e mail validation report to retailer the worth, suppressed. However, after all, their e mail deal with is technically legitimate. Because of this, if we ever run their e mail during the validation procedure once more (corresponding to by means of NeverBounce or Electronic mail Checklist Check), it will come again as adequate or legitimate.

With a purpose to deal with the suppressed project as a one-way door, I had to ensure that as soon as an e mail validation used to be marked suppressed, it’s going to by no means be marked as the rest. To do that, I needed to replace my SQL commentary to conditionally retailer the brand new price if and provided that the present price used to be no longer suppressed.

To discover this common sense, let’s first have a look at the database desk construction:

CREATE TABLE `email_verification` (
	`e mail` varchar(255) NOT NULL,
	`textCode` varchar(50) NOT NULL,
	`updatedAt` datetime NOT NULL,
	PRIMARY KEY (`e mail`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Right here, we are the usage of the e mail as the principle key and the textCode as the e-mail verification price. Populating this desk used to be going to be an iterative procedure; and, the verification codes have been going to modify through the years. As such, my INSERT had to care for updates on e mail duplication (ie, on number one key collision).

The SQL for this sort of multi-pass INSERT looks as if this:

SET @e mail="ben@bennadel.com";
SET @textCode="adequate";

INSERT INTO
	email_verification
SET
	e mail = @e mail,
	textCode = @textCode,
	updatedAt = UTC_TIMESTAMP()
ON DUPLICATE KEY UPDATE
	textCode = VALUES( textCode ),
	updatedAt = VALUES( updatedAt )
;

On this SQL commentary, I am the usage of the ON DUPLICATE KEY UPDATE clause so as to replace any current row with an identical e mail deal with (which, keep in mind, is getting used as the principle key). Within the context of the ON DUPLICATE KEY UPDATE clause, the VALUES() serve as returns the worth that would had been used inside the INSERT had there been no key-collision.

With this SQL, I will run the INSERT...ON DUPLICATE KEY UPDATE as again and again as I really like; and the textCode column will stay getting up to date for the given e mail deal with:

Conditionally Updating Columns When The use of ON DUPLICATE KEY UPDATE In MySQL

In fact, as soon as the textCode column is designated as suppressed, I by no means need it to modify. With a purpose to do that, my UPDATE project has to have a look at the current price and conditionally override it. Fortunately, the UPDATE clause can reference any current column price inside the current row by means of title. Because of this, I will use the IF() serve as to go back the current price whether it is lately suppressed; or, to go back the VALUES() price whether it is the rest.

SET @e mail="ben@bennadel.com";
SET @textCode="adequate";

INSERT INTO
	email_verification
SET
	e mail = @e mail,
	textCode = @textCode,
	updatedAt = UTC_TIMESTAMP()
ON DUPLICATE KEY UPDATE
	textCode = IF(
		( textCode="suppressed" ),
		textCode,          -- Go back the prevailing price (suppressed)
		VALUES( textCode ) -- Go back the brand new price.
	),
	updatedAt = VALUES( updatedAt )
;

To be transparent, inside the ON DUPLICATE KEY UPDATE clause, the expression textCode refers back to the current price inside the current row. However, the expression VALUES(textCode) refers back to the new price that might had been inserted. And, as soon as the textCode column is about to suppressed, it may possibly by no means return to the rest:

Using INSERT..ON DUPLICATE KEY UPDATE with an IF() function to lock-down a column value.

By way of the usage of the IF() serve as inside the ON DUPLICATE KEY UPDATE clause, I will make sure that as soon as the textCode column is “suppressed”, any next execution of this question will grow to be a no-op. That mentioned, I am electing to at all times replace the updatedAt column on each and every execution as a method to offer inside comments to our crew.

I can have damaged this complete set of rules up into two separate SQL queries: a SELECT question adopted by means of a conditional INSERT or UPDATE question. On the other hand, this little bit of workflow operates over hundreds of thousands of e mail deal with. And, if I will reduce the selection of SQL queries in part (from 2 to one), this has an overly significant affect on how briskly the full workflow can execute.

Need to use code from this submit?
Take a look at the license.



[ad_2]

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back To Top
0
Would love your thoughts, please comment.x
()
x