0

What I want to do is something that has the following logic:

IF EXISTS (SELECT * FROM people WHERE ID = 168)
THEN UPDATE people SET calculated_value = complex_queries_and_calculations
WHERE ID = 168

.., so to update a field of a given record if that record contains a given data, and else do nothing. To generate the data which would be used for the update, I need to query other tables for values and make some calculations. I want to avoid these queries + calculations, if there's actually nothing to update. And in this case, simply do nothing. Hence, I guess that putting for example an EXIST clause inside a WHERE clause of the UPDATE statement would end in many queries and calculations made in vain.

How can I only UPDATE conditionally and else do nothing, and make sure that all the queries + calculations needed to calculate the value used for the update are only made if the update is needed? And then, in the end, only do the update if complex_queries_and_calculations is not NULL?

My best solution so far uses a Common Table Expression (WITH clause), which makes it impossible to short-circuit. Anyway, such that you can understand the logic I'm trying to achieve, I'm showing what I've been trying so far (without success; code below is not working and I don't know why..):

-- complex queries and calculations; return result as t.result
WITH t AS(complex queries and calculations)
UPDATE target_table
SET
CASE
WHEN t.result IS NOT NULL
THEN target_table.target_column = t.result WHERE target_table.PK = 180
END;

UPDATES (Still saying syntax error, still not working)

WITH t AS(complex_queries_and_calculations AS stamp)
UPDATE target_table
SET target_column =
CASE
WHEN t.stamp IS NULL
THEN target_column
ELSE t.stamp
END
WHERE ID = 168;

Not even this is working (still reporting syntax error on UPDATE line):

WITH t AS(complex_queries_and_calculations AS stamp)
UPDATE target_table
SET target_column = target_column
WHERE ID = 168;

(eventual better approaches which avoid redundant target_column = target_column updates welcome)

With select it works, so I'm totally not understanding the syntax error #1064 it returns for my update query:

WITH t AS(complex_queries_and_calculations AS stamp)
SELECT
CASE
WHEN t.stamp IS NULL
THEN "Error!"
ELSE t.stamp
END
FROM t;

ADDITIONAL INFO

It seems like MariaDB actually does not support CTEs with UPDATE statements; correct me if I'm wrong... So I tried the following:

UPDATE people AS p
INNER JOIN (queries_and_calculations AS result) t
ON p.ID <> t.result -- just to join
SET p.target_column = t.result
WHERE p.ID = 168
AND t.result IS NOT NULL;

and now it's saying:

#4078 - Illegal parameter data types varchar and row for operation '='
DevelJoe
  • 856
  • 1
  • 10
  • 24
  • 1
    you have to provide the whole query with sample data in order for community to be able to help you. – eshirvana Nov 14 '20 at 20:51
  • From upon the update keyword the queries exactly as it is, please focus on that part / how to make it work – DevelJoe Nov 14 '20 at 20:56

3 Answers3

4

Simply do the UPDATE. If there is no row with that ID, it will do nothing. This will probably be no slower than testing first.

Ditto for DELETE when the row might not exist.

"Upsert"/"IODKU" -- INSERT ... ON DUPLICATE KEY UPDATE ... is useful when you want to modify some columns when the row exists (according to some unique column), or add a new row (when it does not exist). This is better than doing a SELECT first.

Think of it this way... A big part of the UPDATE is

  • opening the table,
  • locating the block in the table that needs to be modified
  • loading that block into the cache ("buffer_pool")

All of that is needed for both your SELECT and UPDATE (yeah, redundantly). The UPDATE continues with:

  • If the row does not exist, exit.
  • Modify the row, and flag the block as "dirty".
  • In the background, the block will eventually be flushed to disk.

(I left out details about transactional integrity ("ACID"), etc.)

Even in the worst case, the whole task (for a single row) takes under 10 milliseconds. In the best case, it takes under 1ms and can be done somewhat in parallel with certain other activities.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Yeh no need to iodku, cause I don't wanna insert anything, but do nothing if the record does not exist. I'm just afraid of doing unnecessary queries (two queries and some calculations to calculate the update value) in all the cases where the targeted record does not exist.. Isn't there a way to do the queries, claculations and updates ONLY if the record does not exist? Or are where queries processed and short circuited first anyway? U get what I mean? – DevelJoe Nov 14 '20 at 21:11
  • @DevelJoe - True. Just giving you a tip that you might need tomorrow. – Rick James Nov 14 '20 at 21:13
  • And I added more discussion. – Rick James Nov 14 '20 at 21:20
  • thanks a lot, but I still can't get it working, it still says that there's a syntax error on the UPDATE line.. – DevelJoe Nov 15 '20 at 01:19
  • Let's see the syntax error; it points at the offending token. – Rick James Nov 15 '20 at 06:38
  • I don't understand what you mean by this comment? And also, thanks a lot for your very interesting answer; I'm just at the moment of starting to understand how queries / commands are being processed, how / what is short-circuitted in SQL, etc. I wanted to ask you if you have some supportive videos / tutorials which explain these things well / tools which help troubleshoot / precisely analyse query executions etc. Not that I didn't find a lot already online, but it would be great to know which tools experienced devs use for this purpose! Also; is ```UPDATE ... FROM SELECT``` n/a in mariadb?? – DevelJoe Nov 15 '20 at 11:13
  • https://www.eversql.com/sql-syntax-check-validator/ is also saying that they syntax is valid, wth... – DevelJoe Nov 15 '20 at 12:24
  • Thanks dude, see my answer below, like that it indeed worked, but wow was that a pain. It looks like you were right and ```WHERE``` clauses indeed do some sort of short-circuiting, if the queried clause returns no matching records. Thank you, and still waiting for the tools / tutorials / vids / pages you recommend to use for query optimizations when it comes to performance testings etc. :) – DevelJoe Nov 15 '20 at 14:14
0

There is no IF in SQL, since it is not needed:


UPDATE people p
SET calculated_value = c.val
FROM (
        SELECT ID, val
        FROM
        ... complex_queries_and_calculations
        ) c
WHERE c.ID = p.ID
AND ID = 168
AND v.val <> i.val -- maybe add this to avoid idempotent updates. Beware of NULLs, though!
        ;

~

wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • cheers, but does this work for mariadb? one sec I'll check.. – DevelJoe Nov 15 '20 at 12:51
  • I don't know. The `update ... from ...` syntax may differ a bit for mysql. – wildplasser Nov 15 '20 at 12:57
  • nope, not working; it's saying syntax error on 'FROM (SELECT...)'; this is so frustrating, seems like mariadb lacks quite some features. Which is the best RDBMS you recommend? And, do you may have another solution, compatible with mariadb? (also, please note that my RDBMS is mariadb, not mysql, they're indeed different in terms of available functions..) – DevelJoe Nov 15 '20 at 12:59
0

GOT IT, The following query works to do exactly what I wanted in Mariadb :

UPDATE target_table

LEFT JOIN (complex_queries_and_calculations_to_get_update_value AS update_value) t

ON target_table.ID <> t.update_value -- serves just to have update value in memory, 
-- because it needs to be accessed twice to create the updated column value 
-- on update, sort of a workaround for CTE + UPDATE in MariaDB

SET target_column = JSON_ARRAY( FORMAT_UPDATE_VALUE(t.update_value),
                    FORMAT_2_UPDATE_VALUE(t.update_value) )

WHERE ID = 128 AND t.update_value IS NOT NULL;

If the record does not exist, the query takes about 0.0006 secs to execute, without doing anything to the table. If it does exits, it takes 0.0014 secs to execute, while updating the targeted record accordingly. So, it indeed seems to work and resources are saved if the targeted record is not found in target_table. Great thanks to all who helped!

DevelJoe
  • 856
  • 1
  • 10
  • 24