Prawn, please help me optimize this SQL statement

Source: Internet
Author: User
SQL optimized MySQL PHP

Prawn, please 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 "approval success";


Reply to discussion (solution)

Put Concat (' ". $value [' result ']." {HX} ', now (), ' {HX} '. $value [' name ']. " {HX} '. $value [' op ']. " {HX} '. $value [' suggestion ']. "') This is implemented in PHP, not with SQL Concat.

There is the database index, which is very helpful to improve the speed

Is there anything else you can do to optimize the next few words?

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 (SE Lect ' mu_id ' from ' $table ' WHERE ' id ' = $value [id] LIMIT 1) "; mysql_query ($sql, $conn);//The statement is simplified, but if the index is good, it should be promoted. , mostly indexed for member_user.id,mu_id,id fields}

Thanks, Baoxiaohua. Simplifies SQL statements

  • 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.