简体   繁体   中英

Optimizing MySQL update query

This is currently my MySQL UPDATE query, which is called from program written in Java:

String query = "UPDATE maxday SET DatePressureREL = (SELECT " + 
   "Date FROM ws3600 WHERE PressureREL = (SELECT MAX" +
   "(PressureREL) FROM ws3600 WHERE Date >= '" + Date + 
   "') AND Date >= '" + Date + "' ORDER BY Date DESC LIMIT 1), " +
   "PressureREL = (SELECT PressureREL FROM ws3600 WHERE " + 
   "PressureREL = (SELECT MAX(PressureREL) FROM ws3600 " +
   "WHERE Date >= '" + Date + "') AND Date >= '" + Date + 
   "' ORDER BY Date DESC LIMIT 1), ...";

try {
    s.execute(query);
} 
catch (SQLException e) {
    System.out.println("SQL error");
}
catch(Exception e) {
    e.printStackTrace();
}

Let me explain first, what does it do. I have two tables, first is ws3600, which holds columns (Date, PressureREL, TemperatureOUT, Dewpoint, ...). Then I have second table, called maxday, which holds columns like DatePressureREL, PressureREL, DateTemperatureOUT, TemperatureOUT,... Now as you can see from an example, I update each column, the question is, is there a faster way? I am asking this, because I am calling MAX twice, first to find the Date for that value and secondly to find the actual value. Now I know that I could write like that:

SELECT Date, PressureREL FROM ws3600 WHERE PressureREL = 
  (SELECT MAX(PressureREL) FROM ws3600 WHERE Date >= '" + 
  Date + "') AND Date >= '" + Date + "' 
ORDER BY Date DESC LIMIT 1

That way I get the Date of the max and the max value at the same time and then update with those values the data in maxday table. But the problem of this solution is, that I have to execute many queries, which as I understand takes alot more time compared to executing one long mysql query because of overhead in sending each query to the server.

If there is no better way, which solution beetwen this two should I choose. The first, which only takes one query but is very unoptimized or the second which is beter in terms of optimization, but needs alot more queries which probably means that the preformance gain is lost because of overhead in sending each query to the server?

Doing 2 queries isn't really a problem for me, but they should be in a transaction (the reads and the write), this way you'll be sure that your update values are not wrong. With one query you do not have this problem.

I think the time lost in reading some data is nothing regarding the time lost by performing a write operation. A write operation is not by definition a fast thing, you could have triggers, you're maybe emptying the query cache from all requests impacting this table, the database needs to sync your write on disk, etc.

The more important thing for you is to keep your process simple, readable, and logic.

1) I think the problem goes deeper than just SQL optimization. Do you think this could be modeled differently where you don't have to migrate data like this (this much, and this often too btw) in the first place? Perhaps just using a FK/cross table to link the two together instead of migrating every field?

2) One query is much better than using JDBC to constantly go back and forth over the connection with new statements. That is a very expensive operation (each time). You will always want to stick to condensing queries into one as opposed to using iteration to make execute many statements.

Working from the inside out, it looks like all your subqueries do the same thing.

What's the point of having a where clause that does Date >= '" + Date + "') AND Date >= '" + Date + "' ?

Without going into column names or technical details, what are the purposes of your two tables?

String query = @"UPDATE maxday SET DatePressureREL = (SELECT Date FROM ws3600 WHERE PressureREL = (SELECT MAX(PressureREL) FROM ws3600 WHERE Date >= @Date) AND Date >= @Date ORDER BY Date DESC LIMIT 1), PressureREL = (SELECT PressureREL FROM ws3600 WHERE PressureREL = (SELECT MAX(PressureREL) FROM ws3600 WHERE Date >= @Date) AND Date >= @Date ORDER BY Date DESC LIMIT 1), ...";

After this, ideally if you were using a SelectCommand of some type instead of a string, you would

query.Parameters.Add(new MySqlParameter("@Date", yourdate));

Alternatively, you can just do this, although it opens you up to sql injection

query = query.replace("@Date", "'" + Date "'");

Either way, it makes the query considerably more legible.

If you can get all the values in one select query, this might work. Use a stored procedure accepting one parameter (date) that does:

One select statement, storing the values in a cursor, and

One update statement, using the values in the cursor.

Cursor Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM