MySQL中使用order by … limit 1替换Max函数
测试环境:

测试Sql:
| 1 | select max(sscode) from sm_sku_show where del_flag = "0"; | 
测试耗时:
| 1 | [SQL] | 
测试结果:
| 1 | +---------------------+ | 
结论:
max() 函数的需扫描where条件过滤后的所有行。
请慎用max()函数,特别是频繁执行的sql,若需用到可转化为测试中的 order by id desc limit 1
SQL Server中Max函数查询优化与查看执行计划
SQL语句:
| 1 | SELECT COUNT(1) FROM northwind.dbo.OrderDetails | 
执行结果:
查看执行计划:
预估的执行计划: