1. Optimize Your Queries For the Query Cache

使用query cache来优化查询

Most MySQL servers have query caching enabled. It’s one of the most effective methods of improving performance, that is quietly handled by the database engine. When the same query is executed multiple times, the result is fetched from the cache, which is quite fast.

大多数mysql服务器都开启了query caching。这是提高性能的最直接有效的方法,由数据库引擎直接实现。当同一个查询被多次执行时,结果集可以直接从缓存中获取,非常快速。

The main problem is, it is so easy and hidden from the programmer, most of us tend to ignore it. Some things we do can actually prevent the query cache from performing its task.

主要问题是,由于这个过于简单,直接对程序员透明,我们经常会忽略它。有一些操作会造成querycache功能无法生效。

// query cache does NOT work

$r= mysql_query("SELECT username FROM user WHERE signup_date >= CURDATE()");

// query cache works!

$today=date("Y-m-d");

$r= mysql_query("SELECT username FROM user WHERE signup_date >= '$today'");

The reason query cache does not work in the first line is the usage of the CURDATE() function. This applies to all non-deterministic functions like NOW() and RAND() etc… Since the return result of the function can change, MySQL decides to disable query caching for that query. All we needed to do is to add an extra line of PHP before the query to prevent this from happening.

第一行的query cache无法工作的原因时使用了CURDATE()函数。这个同样适用于所有的不确定性函数比如NOW()和RAND()等。。。由于返回的结果可能会变化,MYSQL决定对于查询不使用query cache。解决这个问题的办法就是在PHP中加入额外的一行。

2. EXPLAIN Your SELECT Queries

解析SELECT查询语句

Using the EXPLAIN keyword can give you insight on what MySQL is doing to execute your query. This can help you spot the bottlenecks and other problems with your query or table structures.

使用EXPLAIN关键字能够让你看到MYSQL在执行你的查询时,内部的实际工作流程。这能够帮助你定位到你的查询或者表结构的瓶颈和其他问题。

The results of an EXPLAIN query will show you which indexes are being utilized, how the table is being scanned and sorted etc…

EXPLAIN的结果将展示给你哪些索引会被用到,表是怎么被扫描,排序等等。

Take a SELECT query (preferably a complex one, with joins), and add the keyword EXPLAIN in front of it. You can just use phpmyadmin for this. It will show you the results in a nice table. For example, let’s say I forgot to add an index to a column, which I perform joins on:

拿一个SELECT查询(一个使用join的复杂查询最好),在语句前面加上EXPLAIN.你可以使用phpmyadmin。他将结果以表的形式返回给你。比如,我在某个字段上面缺失了索引,下面执行了一个join:

After adding the index to the group_id field:

在字段group_id上添加索引后:

Now instead of scanning 7883 rows, it will only scan 9 and 16 rows from the 2 tables. A good rule of thumb is to multiply all numbers under the “rows” column, and your query performance will be somewhat proportional to the resulting number.

他将只扫描两个表的9行和16行,而不是所有的7883行。一个很好的窍门就是将rows字段的所有数字相乘,你的查询性能将会和这个数字成比例(注:对于这类联合查询适用)

3. LIMIT 1 When Getting a Unique Row

查询一行时用limit 1

Sometimes when you are querying your tables, you already know you are looking for just one row. You might be fetching a unique record, or you might just be just checking the existence of any number of records that satisfy your WHERE clause.

有些时候当你查询表时,你已经知道你只会拿到一行。你可能是查看一个唯一记录,或者你可能只是检查满足你的where条件

In such cases, adding LIMIT 1 to your query can increase performance. This way the database engine will stop scanning for records after it finds just 1, instead of going thru the whole table or index.

这种情况下,加上limit 1将提高你的效率。数据库引擎在找到一条记录后就停止继续扫描,而不是扫完整张表或者索引。

// do I have any users from Alabama?

// what NOT to do:

$r= mysql_query("SELECT * FROM user WHERE state = 'Alabama'");

if(mysql_num_rows($r) > 0) {

// ...

}

// much better:

$r= mysql_query("SELECT 1 FROM user WHERE state = 'Alabama' LIMIT 1");

if(mysql_num_rows($r) > 0) {

// ...

}

4. Index the Search Fields

在查询的字段上加索引

Indexes are not just for the primary keys or the unique keys. If there are any columns in your table that you will search by, you should almost always index them.

索引并不只是为了主键或者唯一键。如果你的表中有任何字段需要被搜索,基本上你就需要增加索引。

As you can see, this rule also applies on a partial string search like “last_name LIKE ‘a%’”. When searching from the beginning of the string, MySQL is able to utilize the index on that column.

可以看到,这个规则也适用于partial string搜索比如"last_name LIKE ‘a%"。当从string的开头开始搜索时,MYSQL能够使用到这个字段上面的索引。

You should also understand which kinds of searches can not use the regular indexes. For instance, when searching for a word (e.g. “WHERE post_content LIKE ‘%apple%’”), you will not see a benefit from a normal index. You will be better off usingmysql fulltext searchor building your own indexing solution.

你也应该能够理解哪种类型的搜索不能够用到索引。比如,当检索一个字(比如, “WHERE post_content LIKE ‘%apple%’),正常的索引将不能够帮助到你。你最好使用MYSQL全文检索或者建立自己的索引解决方案。

5. Index and Use Same Column Types for Joins

Joins的字段要用同样的字段类型并建索引

If your application contains many JOIN queries, you need to make sure that the columns you join by are indexed on both tables. This affects how MySQL internally optimizes the join operation.

如果你的程序包含了多个JOIN 查询,你需要确认你join的字段在两个表中都被索引。这个会影响到MYSQL如何内部优化join操作。

Also, the columns that are joined, need to be the same type. For instance, if you join a DECIMAL column, to an INT column from another table, MySQL will be unable to use at least one of the indexes. Even the character encodings need to be the same type for string type columns.

另外,这些做了JOIN操作的字段,需要是同一种类型。比如,如果你将一个DECIMAL字段和一个INT字段做了关联,MYSQL将至少不能用到一个索引。对于string类型,甚至字符编码需要时同样的类型。

// looking for companies in my state

$r= mysql_query("SELECT company_name FROM users

LEFT JOIN companies ON (users.state = companies.state)

WHERE users.id = $user_id");

// both state columns should be indexed

// and they both should be the same type and character encoding

// or MySQL might do full table scans

6. Do Not ORDER BY RAND()

切忌ORDER BY RAND()

This is one of those tricks that sound cool at first, and many rookie programmers fall for this trap. You may not realize what kind of terrible bottleneck you can create once you start using this in your queries.

这个听起来有点意思,很多新的程序员会掉进这个陷阱。你可能根本没有意识到这是多么严重的瓶颈。

If you really need random rows out of your results, there are much better ways of doing it. Granted it takes additional code, but you will prevent a bottleneck that gets exponentially worse as your data grows. The problem is, MySQL will have to perform RAND() operation (which takes processing power) for every single row in the table before sorting it and giving you just 1 row.

如果你确实需要随机的返回行,我们有更为有效的方法。虽然它多了一些代码量,但是你能够根本上解决掉这个随着数据量增长而出现的瓶颈。问题就是,MYSQL将对于表中的所有行做RAND操作(耗cpu)然后才返回给你一行。

// what NOT to do:

$r= mysql_query("S

mysql 运维 最佳实践_Mysql 开发最佳实践相关推荐

  1. mysql开发与实践_MySQL开发与实践

    原标题:MySQL开发与实践 本书作为MySQL课程的教材,系统全面地介绍了有关MySQL数据库应用开发所涉及的各类知识.全书共分16章,内容包括数据库基础.MySQL概述.MySQL语言基础.数据库 ...

  2. mysql运用与实践_MySQL开发与实践 PDF 下载

    相关截图: 资料简介: 本书作为MySQL课程的教材,系统全面地介绍了有关MySQL数据库应用开发所涉及的各类知识.全书共分16章,内容包括数据库基础.MySQL概述.MySQL语言基础.数据库和表的 ...

  3. 《DevOps实战:VMware管理员运维方法、工具及最佳实践》——2.3 配置管理

    本节书摘来自华章计算机<DevOps实战:VMware管理员运维方法.工具及最佳实践>一书中的第2章,第2.3节,作者:小特雷弗 A. 罗伯茨(Trevor A. Roberts Jr.) ...

  4. 【MySQL】MySQL运维及开发规范

    1.概述 MySQL运维及开发规范一.基础规范 (1) 使用INNODB存储引擎 (2) 表字符集使用UTF8 (3) 所有表都需要添加注释 (4) 单表数据量建议控制在5000W以内 (5

  5. mysql运维备份_MySQL运维经验

    原标题:MySQL运维经验 1. 概要 每台机器都使用多实例的模型. 每个机器放多个实例,每个实例放多个DB. 多实例之间没有进行资源隔离,这么做是让每个实例都能发挥最大性能. 目前大部分核心业务已切 ...

  6. 20个MySQL运维案例,请查收!

    墨墨导读:日常MySQL运维中,会遇到各种各样的问题,下面分享二十个MySQL运维案例,附有问题的分析和解决办法,希望你遇到同样的问题的时候,可以淡定地处理. 数据技术嘉年华,十周年盛大开启,点我立即 ...

  7. MySQL运维高级课程 - 多位大牛轮番上阵倾力讲解 超多经典PDF 28天MySQL运维课程

    ===============课程目录=============== ├<01 - 掀起MySQL的头盖来> │  ├掀起MySQL的头盖来 - 叶金荣.mp3 │  └掀起MySQL的头 ...

  8. 最新MySQL运维高级课程 - 多位大牛轮番上阵倾力讲解 超多经典PDF 28天MySQL运维课程

    ===============课程目录=============== ├<01 - 掀起MySQL的头盖来> │  ├掀起MySQL的头盖来 - 叶金荣.mp3 │  └掀起MySQL的头 ...

  9. MySQL运维案例分析:Binlog中的时间戳

    引言:本文从一个典型的案例入手来讲述Binlog中时间戳的原理和实践,通过本文你可以了解时间戳在Binlog中的作用及产生方法,以便在出现一些这方面怪异的问题时,做到心中有数,胸有成竹. 本文选自&l ...

最新文章

  1. 又偷懒了4个月,督促自己
  2. python爬虫软件-一些Python爬虫工具
  3. 【C++】38.类成员变量声明为 对象与指针的区别
  4. GVA gin-vue-admin前后端部署教程
  5. 无法将成员变量添加到ID为 的控件中
  6. nosuiteable Oracle,快给你的Kubernetes集群建一个只读账户(防止高管。。。后)
  7. WebStorm生成Vue的组件关系图
  8. tkinter事件机制
  9. 用html做工资查询登陆页面,薪资筛选页面.html
  10. php pdo mysql类源码_完整示例php+pdo实现的购物车类
  11. 【模板篇】树状数组们(三)
  12. 【XSY2470】lcm 数学
  13. 算法设计思维导图(算法设计与分析第二版)
  14. IAR for STM8介绍、下载、安装与注册
  15. linux系统浏览器没有图片不显示,网页不显示图片怎么回事【解决方法】
  16. windows调整jadx的使用内存大小
  17. 野火 步进电机 视频笔记
  18. 安装出错:Command line option syntax error.Type Command /? for help.解决方案
  19. Unity安卓Android平台StreamingAssets下文件的读取
  20. android+mdm+解决方案,Android平台下的MDM (Mobile Device Management)解决方案

热门文章

  1. 测试—自定义消息处理
  2. mysql order by 运算_在MySQL中具有ORDER BY的多个LIKE运算符?
  3. whea uncorrectable error蓝屏_Windows 10再出“不可选”更新:蓝屏、死机比较烦
  4. win配置环境变量以及一些变量换行类的小技巧
  5. memcached 与 mysql_memcached 和 MySQL 的 query ?
  6. python数据分析准备_使用Python进行数据分析I 环境准备
  7. c#string倒数第二位插入字符_【转载】C#中string类使用Substring方法截取字符串
  8. 用vhdl语言设计一个小游戏_用最直白的设计语言营造梦想的办公空间
  9. Android开发之解决ListView和ScrollView滑动冲突的方法
  10. Android开发之ListView子布局item高度的问题