begin;
select id, new_rating into temp_ratings values (1,25),(2,94),(3,16);
update table place set rating=new_rating
from temp_ratings
where place.id=temp_ratings.id;
drop table temp_ratings;
commit;
Scales well, and almost all db engines know how to do that join. You can index the id, or use a temp table, or whatever depending on the shape and quantity of your data.(edit -- actually finished the post)
1) This is not really a temporary table in the traditional
sense; you're creating a real table and then dropping it.
This has problems with concurrency (you can't have two
"real" tables with the same name), among other issues.
2) Temporary tables in Postgres suck because they bloat
the catalogs. Creating temporary tables regularly is
better avoided.
3) There's no real need to create a table of any kind. A
regular VALUES would do. (Though you might have to encode
some type information into the query.)> I had an update loop go from taking 90 seconds to 3.
How is the performance with prepared statements? It seems like repeatedly executing the same statement with different values is what prepared statements are made for.
My point is that, in a vacuum, you can't really say anything about what the performance difference will be. Yes, you can certainly find instances where the performance is better, but it may not be much.
My point is that the complexity of writing the statement is non-trivial, and unless you have a good reason to you shouldn't use CASE. It shouldn't be your first choice, is all I'm saying. For example, the example you provided here is not even valid SQL. It should be:
UPDATE TABLE place
SET rating = (
CASE WHEN id = 1 THEN 25
WHEN id = 2 THEN 94
WHEN id = 3 THEN 16
END
)
WHERE id IN (1, 2, 3)
Keep in mind, I also said: "If performance becomes a problem on this query, you can fix it later when you can focus on just that one issue and understand the specific problem much better."I agree with others that usually it's best to write queries that are simple to understand and let the optimizer pick a good query. Under higher load and with higher data, the indexes matter more of course (and amount of ram).
I can't say the above case is more likely to be better or not, it depends on the database schema.
I made a postgres table with two columns, id and num. I filled it with 1000000 rows in sequential order (just a generate_series()), so the first row was (1,1), the second was (2,2) etc.
I ran two queries to update the first 1000 'num' columns with random values. The first was as described above:
UPDATE test SET num = (CASE WHEN id = 1 THEN 123 CASE WHEN id = 2 THEN 356 ...) WHERE id IN ...
This took about 1.5 seconds (on my completely un-optimized postgres instance)The second uses 1,000 individual queries in a transaction (BEGIN, COMMIT):
BEGIN
UPDATE test set num = 123 WHERE id = 1
....
COMMIT
This is still running, so it's taken over 3 minutes so far.I'm guessing it's because even though the statements are in a single transaction the database still issues 1,000 individual table scans for a single record, and does 1,000 individual updates. Doing 1,000 updates using a single CASE statement seems to take the same time as updating a single one with the traditional way.
Edit: Query returned successfully in 21 min 39 secs.
But like someone said above, it depends on the best way, it will be different. Also when dividing into 1000 separate queries, they have to wait for the previous one to run, and then they'll need to be optimized. A more interesting comparison might be (1) create index on the id column, (2) run the 1000 updates in parallel, maybe 30 at a time?
Edit: Oh no, there was not. My bad :/ I added an index and the query time dropped to 5 seconds or so. A lot, lot faster, but 4x as slow as the CASE version.
Parallel queries would probably perform better, but I'm looking at a more general case. Django supports a lot of backends and not all of them support parallel queries, and indeed there is no way to make parallel queries like this in Django.
If we are talking about Postgres specifics, there is some better syntax for this that lets you join on a list of Values you can construct, which I intend to add support for later.
But if using 1 case statement is generally faster than issuing N individual synchronous queries then it's a big win for applications that have to do this kind of thing:
for instance in queryset.all():
instance.field = some_function()
instance.save()
While this isn't a super common pattern it does crop up from time to time and it's annoying to work around (if you even can).4-5 seconds sort of makes sense, depending on hardware. 3 minutes made no sense.
Both methods will be faster than sending 100 separate update. postgre API calls.
The case statement is slightly different, more like this (usually with a couple of hundred ID's):
Members of the Django community, myself included, have seem huge performance benefits using this technique. I had an update loop go from taking 90 seconds to 3.As a seasoned DBA you see any huge downsides with this technique? I agree that if you're updating the whole table using a CASE statement is not a great idea but if you want to update 1000 rows with differing values and you have to issue 1000 individual queries then this can take some time (including whatever overhead Django has).
1. https://github.com/django/django/pull/9606