Home Database Mysql Tutorial MySQL语句相关经验总结_MySQL

MySQL语句相关经验总结_MySQL

Jun 01, 2016 pm 01:18 PM
mysql Experience summary statement

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 倍

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles