Home > Database > Mysql Tutorial > body text

MySQL语句相关经验总结_MySQL

WBOY
Release: 2016-06-01 13:18:36
Original
688 people have browsed it

MySQL语句

1. 字段自减

  UPDATE `table_name` SET `total`=IF(`total`

  一般在做字段减法的时候我们需要在外部判断一次数值

  这样可以省去一次外部数值判断的查询,也可以防止无符号字段 total 为0时 数值溢出,将变成最大值或者报错的情况

  2. UPDATE 的 SET 使用子查询数据

  UPDATE `bz_xs_chapter` AS c,

  (SELECT COUNT(*) AS total FROM `bz_xs_chapter` WHERE `bookId` = 22042) AS c1,

  (SELECT COUNT(*) AS total FROM `bz_xs_tome` WHERE `bookId` = 22372) AS c2

  SET

  `c`.`name`=`c1`.`total`,

  `c`.`words`=`c2`.`total`

  WHERE `c`.`id` = 2889820

  这样可以通过子查询一次更新多个字段值,效率要高很多了吧

  3. COUNT(*)效率高

  之前看过许多相关帖子,有的说使用 COUNT(id) 效率高,各说各的看起来都有理,但自己测试:

  270W数据,350MB大小数据表,COUNT(*) 比 COUNT(id) 速度快了至少 10 倍 甚至 100 倍

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!