S
S
suhuxa12017-07-08 12:05:42
SQL
suhuxa1, 2017-07-08 12:05:42

How to form 1 UPDATE query with a thousand values?

Good afternoon!
Once a day, my script receives information (about a thousand lines in the database), which needs to be changed in this very database. Simply put, these are goods whose prices change every day. An array comes with prices and product IDs. It is necessary to form one UPDATE request, in which there will be something like: Item A: price = 500, shipping = 100. Item B: price = 400, shipping = 50. And so on. About 1000 items so. Sending one request is not an option.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eugene Wolf, 2017-07-08
@Wolfnsex

How to form 1 UPDATE query with a thousand values?

Like this, and so on up to 1000 (or as many as you need):
UPDATE table1
  SET val = CASE
    WHEN id = 1 THEN 10
    WHEN id = 2 THEN 20
    WHEN id = 3 THEN 30
  END
  -- тут ещё можно дописать условие, типа WHERE id IN (1,2,3)

Sending one request is not an option.
In my opinion, you are trying to turn the poor dolphin inside out through ... You can send many requests at once, separating them with ";" (semicolon). Moreover, they can also be wrapped in a transaction (both many requests one at a time, and many requests through ";").
PS An example of a table on which experiments were carried out: 078858d3885a4c2399df731018ed03e3.png(just in case)

B
Boris Korobkov, 2017-07-08
@BorisKorobkov

CREATE TEMPORARY TABLE ...;
INSERT INTO ... (...), (...), (...);
UPDATE ...;
DROP TEMPORARY TABLE ...;

Total 4 requests.

D
Dimonchik, 2017-07-08
@dimonchik2013

see here , but the Update request itself is slow, and cramming everything into one expression will not make it faster, rather, on the contrary,
you need to change the application logic itself - for example, a column with a date and current prices, another storage, etc.

D
d-stream, 2017-07-08
@d-stream

A more or less optimal option in a similar solution (updating prices for 0.5-20 million positions):
1. data is drawn from an external source into a temporary table
2. missing (new) positions are added from the temporary table to the nomenclature
3. the temporary table is thinned for duplicates 4. the temporary table is thinned
out according to a number of interactive conditions (which fields from it to use for updating) - matching ones are removed
.
experimentally - they came somewhere to the size of a "pack" of 50,000 ... 200,000 lines
____________________________
* we are talking about a fairly universal tool for importing different price lists of different brands = in one case there are purchase and sale prices + classification by discount groups, somewhere only the base price, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question