Heim  >  Artikel  >  Datenbank  >  数据库应用优化

数据库应用优化

WBOY
WBOYOriginal
2016-06-07 15:28:48924Durchsuche

基本语句优化10个原则: 原则1:尽量避免在列上进行运算,这样会导致索引失效。 例如: select * from t where year(d) =2011; 优化为: select * from t where d=2011-01-01; 原则2:使用JOIN时,应该用小结果集驱动大结果集。同时把复杂的JOIN查询拆分成多

基本语句优化10个原则:

原则1:尽量避免在列上进行运算,这样会导致索引失效。

例如:

select * from t where year(d) >=2011;

优化为:

select * from t where d>='2011-01-01';

原则2:使用JOIN时,应该用小结果集驱动大结果集。同时把复杂的JOIN查询拆分成多个Query。因为JION多个表时,可能导致更多的锁定和堵塞。

原则3:注意like模糊查询的使用,避免%%。

例如:

select * from t where name like '%de%';

优化为:

select * from t where name>='de' and name

原则4:仅列出需要查询的字段,这对速度不会有明显影响,主要考虑节省内存。

例如:

select * from t;

优化为:

select id,name from t;

原则5:使用批量插入语句节省交互。

优化后: insert into t(id,name) values (1,'a'),(2,'b'),(3,'c');

原则6:LIMIT的技术比较大的时候用between。

例如:

select * from t as t order by id limit 1000000,10;

优化后:

select * from t as t where id between 1000000 and 1000010 order by id;

原则7:不要使用rand函数获取多条随机记录。

例如:

select * from t order by rand() limit 20;

原则8:避免使用NULL。

原则9:不要使用count(id),而是应该 count(*);

原则10:不要做无谓的排序操作,而是近可能在索引中完成排序。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn