Ladies and gentlemen, help me optimize this SQL statement

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.