MySQL优化

一、查询优化

对于查询优化,尽量要避免全表扫描,考虑在where,order by涉及的列上建立索引

1、尽量避免在where子句中对字段进行null值判断:

select id from t where num is null

优化后:

select id from t where num=0

2、尽量避免在where子句中使用!=或<>

3、尽量避免在where子句中使用or来连接条件:

select id from t where num=0

优化后:

select id from t where num=10

union all

select id from t where num=20

4、in和not in 也要慎用,能用between就不要用in了:

select id from t where num in (1,2,3)

优化后:

select id from t where num between 1 and 3

5、模糊查询,字母打头

SELECT id FROM t WHERE NAME LIKE '李%';

6、避免索引字段在where后面运算

SELECT id FROM t WHERE num/2=100;

SELECT id FROM t WHERE num=200;

7、避免索引字段进行函数操作

SELECT id FROM t WHERE SUBSTRING(NAME,1,3)='abc';

复合索引,必须使用第一个字段

CREATE TABLE IF NOT EXISTS student

(

id INT,

NAME VARCHAR(20),

age INT,

tel VARCHAR(15)

);

创建索引

CREATE INDEX ix_id_name ON student(id,NAME);

SELECT * FROM student WHERE id = 10;

SELECT * FROM student WHERE id = 10 AND NAME = 'li';

二、MySQL性能优化之参数配置

1、目的:

通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值