#每天一个知识点# 最近在优化一批复杂 SQL,从原来 8 秒优化到 100ms 内,总结几个 MySQL 实战心得:
小表驱动大表,关联顺序直接影响执行计划
联合索引遵循:等值条件在前,排序字段在后
小数据量下优化器可能不走索引,属于正常现象
想要 ORDER BY 不 filesort,直接把排序字段放进索引
多表关联尽量用覆盖索引,避免回表
日常 CRUD 看不出差距,一到大报表、分页查询,索引好坏直接决定系统卡不卡。
#每天一个知识点#
js 实现防抖和节流函数
1. 防抖函数(Debounce)
function debounce(func, delay) {
let timer;
return function(...args) {
const context = this;
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(context, args);
}, delay);
};
}
// 使用示例
window.addEventListener('resize', debounce(() => {
console.log('Window resized');
}, 500));
2. 节流函数(Throttle)
function throttle(func, interval) {
let lastTime = 0;
return function(...args) {
const now = Date.now();
if (now - lastTime >= interval) {
func.apply(this, args);
lastTime = now;
}
};
}
// 使用示例
window.addEventListener('scroll', throttle(() => {
console.log('Window scrolled');
}, 500));
js 实现防抖和节流函数
1. 防抖函数(Debounce)
function debounce(func, delay) {
let timer;
return function(...args) {
const context = this;
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(context, args);
}, delay);
};
}
// 使用示例
window.addEventListener('resize', debounce(() => {
console.log('Window resized');
}, 500));
2. 节流函数(Throttle)
function throttle(func, interval) {
let lastTime = 0;
return function(...args) {
const now = Date.now();
if (now - lastTime >= interval) {
func.apply(this, args);
lastTime = now;
}
};
}
// 使用示例
window.addEventListener('scroll', throttle(() => {
console.log('Window scrolled');
}, 500));
展开
评论
4