20+ Best Practices for MySQL

20+ Best Practices for MySQL

 

1. Query caching for optimized queries

 

      Most MySQL servers have query caching turned on. This is one of the most efficient ways to improve performance, and this is handled by MySQL's database engine. When many identical queries are executed multiple times, the results of these queries will be placed in a cache, so that subsequent identical queries will not need to operate the table and directly access the cached results.

 

MySQL's query cache does not work with this function. Therefore, SQL functions like NOW() and RAND() or other such functions will not enable query caching, because the return of these functions will be volatile and volatile.

 

 

2. Explain your select query

Using the EXPLAIN keyword lets you know how MySQL handles your SQL statement. This can help you analyze the performance bottleneck of your query statement or table structure.

 

EXPLAIN query results will also tell you how your index primary keys are used, how your data tables are searched and sorted...etc, etc.

 

Adding explain in front of a select query (complex query with connections) will display relevant information

EXPLAIN
SELECT c.id, cl.question, cl.lang, cl.answer FROM cs_faq c
LEFT JOIN cs_faq_lang cl ON cl.cs_faq_id = c.id
WHERE c.id < 10;



 

 

3. Use LIMIT 1 when getting the only row 

After determining that there is only one piece of data, the advantage of using LIMIT 1 is that when the data is scanned, the scan will be stopped, and the subsequent tables and indexes will not be traversed.

 

 

4. Properly indexed

The general case to add an index is as follows:

1) Conditions behind where

2) Search fields, such as like 'aa%', can be indexed, but like '%aa%', it has no effect. Full-text indexing can be used instead.

3) To add an index to the field with low repetition

 

 

5. When using the index, use the field connection, to the same data type

If you use a decimal field and concatenate another int field, MySQL cannot use the index of these fields to index the merged field.

// find company in state
$r = mysql_query("SELECT company_name FROM users
    LEFT JOIN companies ON (users.state = companies.state)
    WHERE users.id = $user_id");
 
// 两个 state 字段应该是被建过索引的,而且应该是相当的类型,相同的字符集。

 

 

6.不要用order by RAND()

 

7. 尽量不用select * ,查询更少的字段会更快

 

8. 所有表都要有一个id主键,并且一般是用int 并且AUTO_INCREMENT

 

9. 相比varchar,优先使用enum

enum所占用空间更少,如果像status列,存放的是状态字符串,“active”,“inactive”,“pending”等,这类用enum会更省空间

 

10. 使用procedure analyse(),获取表字段的建议

SELECT * FROM faq_lang procedure analyse();

 

 前面的列都是表信息,最后一列就是建议。不过这只是建议,我们还是要根据实际情况选择是否修改表字段。

 

 

11. 如果可以的话,尽量使用 not null

1)除非有非常重要的理由使用null,否则将列设置未not null

2)要清楚空字符串和null的区别

3)null需要额外的空间,会增加比较语句的复杂度

 

12. 预处理语句

使用PrepareStatement,预编译的方式,可以防止SQL注入的问题。

 

13. 无缓冲查询

 

14. 使用unsigned int 存储IP地址

一般我们会用varchar(15)存IP地址,如果用unsigned int,占用空间会更小

 

15. 固定长度(静态)的表会更快

不固定长度列的类型包括:varchar、text、blob,如果用固定长度,检索的时候会更快,但是会浪费一部分空间。

 

16. 垂直分表

例如:一张用户表,包含家庭住址,而这个不是常用的数据。这样就可以将地址拆分出来,保存到另一个表中,用户表越小查询就越快。

 

17. 拆分大型delete和insert语句

 这种大型SQL语句会锁表,并使得Web应用程序停止。这样对导致其他用户无法使用,为了避免这种情况,可以执行一部分SQL后sleep()一段时间。

 

18. 越小的列,越少的列,越快

 

19. 正确的选择存储引擎

1)MyISAM,表锁,适合大量的查询

2)InnoDB,行锁,适合跟多的更新数据,支持事务,支持崩溃回滚

 

20. 使用对象关系映射器,ORM

1)方便数据迁移

2)可以将多个查询批处理到事务中,比向数据库发起单个查询更快

 

21. 小心持久链接

1)减少重新连接到MySQL的成本

2)但可能会遇到连接数限制问题,内存问题

 

 

参考:

http://coolshell.cn/articles/1846.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326543978&siteId=291194637