网站中竖导航栏怎么做,seo营销是什么,建设银行网站注销,义务 网站建设优化很多是基于索引的#xff0c;结合上一篇中的性能分析。1、insert优化--批量插入
insert into tb_test values(1,tom),(2,cat),(3,jerry);
--手动事务提交
start transaction;
insert into...;
insert into...;
commit;
--主键顺序插入#xff08;性能高于乱序插入#x…优化很多是基于索引的结合上一篇中的性能分析。1、insert优化--批量插入 insert into tb_test values(1,tom),(2,cat),(3,jerry); --手动事务提交 start transaction; insert into...; insert into...; commit; --主键顺序插入性能高于乱序插入 --大批量数据插入loadinsert性能较低 mysql --local-infile -u root -p --连接mysql时加载本地文件的参数 set global local_infile1; --设置全局参数开启本地导入 --载入文件地址表字段间分隔行间分隔 load data local infile /root/sql1.log into table tb_user fields terminated by , lines terminated by \n;2、主键优化InnoDB存储引擎表数据根据主键顺序存放称为索引组织表。主键乱序插入id50页分裂页合并页中删除记录达到merge_threshold默认页的50%InnoDB会开始寻找前后页是否可以合并以优化空间。主键设计原则尽量降低主键长度节省二级索引空间插入数据时尽量选择顺序插入防止页分裂使用auto_increment自增主键尽量不要使用UUID做主键或者其他自然主键如身份证号这些都是无序的且长度长尽量避免对主键的修改代价大需要改索引结构3、order by优化Using filesort通过表的索引或全表扫描读取满足条件的数据行然后在排序缓冲区sort buffer中完成排序操作所有不是通过索引直接返回排序结果的排序都叫FileSort排序。Using index通过有序索引顺序扫描直接返回有序数据这种情况即为using index不需要额外排序操作效率高。写在extra中Using filesort尽量避免发生 / Using indexcreate index idx_user_age_phone on tb_user(age,phone); explain select id,age,phone from tb_user order by age,phone; --结果中extra会显示Using index性能高 order by age desc,phone desc; --desc倒序排序时extra会出现backward index scan反向扫描索引; Using index order by age asc, phone desc; --extra会出现Using index;Using filesort(性能低) --解决方法 create index idx_user_age_pho_ad on tb_user(age asc,phone desc); --extra只会出现Using index1、根据排序字段建立合适的索引多字段排序时遵循最左前缀法则2、尽量使用覆盖索引3、多字段排序一升序一降序需要注意联合索引在创建时的规则4、如果不可避免出现filesort大量数据排序时可以适当增大排序缓冲区大小sort_buffer_size(默认256K)4、group by优化explain select profession,count(*) from tb_user group by profession; --typeall, extrausing temporary临时表性能很低 --创建联合索引 create index idx_user_pro_age_sta on tb_user(profession,age,status); --extra变成using index性能优化 --第二种情况 explain select age,count(*) from tb_user group by age; --extra变成using index;using temporary explain select age,count(*) from tb_user where professionmath group by age; --extra只有using indexgroup by分组操作时建立索引提高效率尽量避免extra出现using temporarygroup by分组操作时索引的使用也满足最左前缀法则的5、limit优化limit x,n --表示从x1行开始返回n行 select * from tb_sku limit 2000000,10; --大数据量情况下x很大时性能很低全部回表 --优化方法子查询覆盖索引仅回表10条数据 select s.* from (select id from tb_sku order by id limit 2000000,10) a left join tb_sku s on a.ids.id;6、count优化select count(*) from tb_sku;大数据量情况下比较耗时因为InnoDB需要读取每一行优化思路自己计数插入数据时参数自加1性能比较count(*)count(1)count(主键)count(字段)count(主键)遍历整张表取值直接累加count(字段) 没有not null约束遍历整张表取值服务层判断是否null累加count(字段) 有not null约束遍历整张表取值直接累加count(*)专门优化不取值直接累加7、update优化更新数据时要根据索引主键id等此时事务是行锁update course set name’java’ where id1;若更新数据时根据name无索引会产生表锁update course set name’java’ where name’php’;