---
title: "SQL优化"
author: "ln1058"
date: "2012-08-29T11:16:43Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2012-08-29T11:16:43Z"
id: 1666437
url: "https://ln1058.iteye.com/blog/1666437"
---

这周，公司的DBA为我们做了SQL优化的简单培训，听了觉得受益匪浅，这里记录了主要内容。

 

1\. 不用select \*, 只select需要的字段

 

2\. &gt;,&lt;,&gt;=,&lt;=

   &gt;=3 的效率要高于&gt;2

 

3\. where email like 'A%' 的效率要高于 email like '%A%'

 

mysql&gt; explain select count(\*) from User where email like 'A%';  
+----+-------------+-------+-------+----------------+----------------+---------+------+------+--------------------------+  
| id | select\_type | table | type  | possible\_keys  | key            | key\_len | ref  | rows | Extra                    |  
+----+-------------+-------+-------+----------------+----------------+---------+------+------+--------------------------+  
|  1 | SIMPLE      | User  | range | IDX\_User\_email | IDX\_User\_email | 258     | NULL |  112 | Using where; Using index |  
+----+-------------+-------+-------+----------------+----------------+---------+------+------+--------------------------+  
1 row in set (0.01 sec)

mysql&gt; explain select count(\*) from User where email like '%A%';  
+----+-------------+-------+-------+---------------+----------------+---------+------+------+--------------------------+  
| id | select\_type | table | type  | possible\_keys | key            | key\_len | ref  | rows | Extra                    |  
+----+-------------+-------+-------+---------------+----------------+---------+------+------+--------------------------+  
|  1 | SIMPLE      | User  | index | NULL          | IDX\_User\_email | 258     | NULL | 3745 | Using where; Using index |  
+----+-------------+-------+-------+---------------+----------------+---------+------+------+--------------------------+  
1 row in set (0.00 sec)

 

4\. UNION 与 UNION ALL  
     UNION在进行表链接后会筛选掉重复的记录，所以在表链接后会对所产生的结果集进行排序运算，删除重复的记录再返回结果。  
     UNION ALL操作符替代UNION，因为UNION ALL操作只是简单的将两个结果合并后就返回。  
如果明确知道两个查询不会有交集，使用UNION All

 

5\. IS NULL和 IS NOT NULL  在查询中将不会使用index

 

mysql&gt;  select  sql\_no\_cache  count(\*) from Interview i where   TRIM(i.Email) != '' and i.Email IS NOT NULL;  
+----------+  
| count(\*) |  
+----------+  
|      409 |  
+----------+  
1 row in set (2.07 sec)

mysql&gt;  select  sql\_no\_cache  count(\*) from Interview i where  i.Email&gt;'';  
+----------+  
| count(\*) |  
+----------+  
|      409 |  
+----------+  
1 row in set (0.00 sec)

Other example ：and length(trim(c.Address1)) &gt; 0 and v.confid is not null and v.confid != 0

| ConfID             | int(11)      | YES  |     | NULL    |                |

and c.Address1 &gt; '' and v.confid&gt;0

 

6\. Expression

 mysql&gt; explain select * from User where id+1=59938552;

 mysql&gt; explain select * from User where id=59938551;

 where salary/2=5000

 

7\. Function

 where Year(v.VisitDate) = Year(now()) and Month(v.VisitDate) = Month(now())  
 VisitDate&gt;=DATE\_FORMAT(now(),'%Y-%m-01 00:00:00') and VisitDate&lt; DATE\_FORMAT(date\_add(now(), interval 1 month),'%Y-%m-01 00:00:00');

 

8\. IN，EXISTS和 Inner Join

 mysql&gt;   explain select id from Business where NodeGroup in (select value from NodeGroup where name='NodeGroup');

 

 mysql&gt; explain select id from Business where   exists (select 1 from  NodeGroup where Business.NodeGroup =value and name='NodeGroup');

 

Exists can use for subquery get large result set。

 

mysql&gt; explain  select b.id from Business b inner join NodeGroup n on b.NodeGroup =n.value  
            where  n.name='NodeGroup';