MySQL查询优化
1.MySQL查询优化技术
-
查询重用 (把查询结果缓存到内存,下次查的时候,直接在内存用,但不建议。内存同步太频繁)
-
查询重写 (语句改写)
例如将:子查询 --> 联表查询 -
硬件层
-
mysql系统层
-
schema设计层
-
sql层
-
架构层
2.子查询优化
CREATE TABLE t1 (a1 INT auto_increment, b1 INT, PRIMARY KEY (a1));
CREATE TABLE t2 (a2 INT auto_increment, b2 INT, PRIMARY KEY (a2));
insert into t1 values(null,1);
insert into t2 values(null,1);
2.1 优化器自动优化
SELECT * FROM t1 WHERE t1.a1<100 AND a1 IN (SELECT a2 FROM t2 WHERE t2.a2 >10);
mysql> explain SELECT * FROM t1 WHERE t1.a1<100 AND