Ladies and gentlemen, help me optimize this SQL statement SQL optimization mysql PHP
Ladies and gentlemen, help me optimize this SQL statement
$ SQL = "UPDATE "'. $ table. "'set $ field = concat ('". $ value ['result']. "{hx} ', now (),' {hx }". $ value ['name']. "{hx }". $ value ['OP']. "{hx }". $ value ['suggestion ']. "') $ auditing WHERE 'id' = ". $ value ['id'];
Global $ conn;
Mysql_query ($ SQL, $ conn );
If ($ auditing = ", 'check' = 'yes '"){
$ SQL = "SELECT 'Mu _ id' FROM '$ table' WHERE 'id' = $ value [id] LIMIT 1 ";
$ SQL = mysql_query ($ SQL, $ conn );
$ Mu_id = mysql_fetch_array ($ SQL );
$ SQL = "UPDATE 'Member _ user' SET 'check' = 'yes', 'updatetime' = now () WHERE 'id' = $ mu_id [0]";
Mysql_query ($ SQL, $ conn );
}
Unset ($ conn );
Return "approved ";
Reply to discussion (solution)
Set concat ('". $ value ['result']. "{hx} ', now (),' {hx }". $ value ['name']. "{hx }". $ value ['OP']. "{hx }". $ value ['suggestion ']. "') this is implemented using a php program. do not use SQL concat.
The other is to create a database index, which is very helpful for improving the speed.
Are you sure you want to optimize the following statements?
If ($ auditing = ", 'check' = 'yes '") {$ SQL = "SELECT 'Mu _ id' FROM '$ table' WHERE 'id' = $ value [id] LIMIT 1"; $ SQL = mysql_query ($ SQL, $ conn); $ mu_id = mysql_fetch_array ($ SQL); $ SQL = "UPDATE 'Member _ user' SET 'check' = 'yes', 'updatetime '= now () WHERE 'id' = $ mu_id [0] "; mysql_query ($ SQL, $ conn);} can be changed to if ($ auditing = ", 'check' = 'yes' ") {$ SQL =" UPDATE 'Member _ user' SET 'check' = 'yes', 'updatetime' = now () WHERE 'id' in (SELECT 'Mu _ id' FROM '$ table' WHERE 'id' = $ value [id] LIMIT 1) "; mysql_query ($ SQL, $ conn); // The statement is simplified. it is hard to say whether the index is improved. However, if the index is to be completed, it should be improved. it is mainly used to index the member_user.id, mu_id, and id fields}
Thank you! baoxiaohua simplified the SQL statement.