索引相关的面试题
说出以下语法使用索引的情况
假设建立复合索引(a、b、c),说下以下索引的使用情况:
sql条件语句 | 索引的使用情况 |
---|---|
where a=4 | a |
where a=4 and b=6 | ab |
where a=4 and c=5 and b=6 | abc |
where b=4 or b=5 | 无 |
where a=4 and c=6 | a |
where a=4 and b>5 and c=6 | ab |
where a=4 and b like ‘test%’ and c=4 | ab(test%相当于范围) |
where a=4 order by b,c | a,不会有filesort |
where b=5 order by a | 无,会有filesort |
where b=5 order by c | 无,会有filesort |
where a=5 group by c,b | a,有useing temporary |
大批量数据时分页操作如何优化
1、批量插入数据 :
①建立表
②随机生成字符串
③批量插入数据
2、传统分析查询:
使用limit随着offset增大,查询速度会越来越慢,会把前面的数据都取出来,找到相应的位置
3、优化后分页查询:
①使用子查询优化分页
②使用id限定优化
记录上一页最大的id号,使用范围查询;
限制是只能使用明确知道id的情况,不过一般建立表的时候,都会添加基本的id字段,这为分页带来了便利;
SELECT * FROM testemloyee WHERE id BETWEEN 990000 AND 990010 LIMIT 10;