代码如下:
效率高的代码:
SELECT a.id, concat( DATE_FORMAT( a.sys_created_time, '%Y%m%d' ), lpad(count(1), 4, 0) ) CODE FROM sprc_opportunity a LEFT JOIN sprc_opportunity b ON date_format( a.sys_created_time, '%Y%m%d' ) = date_format( b.sys_created_time, '%Y%m%d' ) AND a.sys_created_time >= b.sys_created_time AND a.tenant_id = b.tenant_id WHERE a.tenant_id = '1095' GROUP BY date_format( a.sys_created_time, '%Y%m%d' ), a.id ORDER BY date_format( a.sys_created_time, '%Y%m%d' ), a.id
效率低的代码:
SELECT a.id, concat( DATE_FORMAT( a.sys_created_time, '%Y%m%d' ), lpad(count(1), 4, 0) ) CODE FROM sprc_opportunity a LEFT JOIN sprc_opportunity b ON date_format( a.sys_created_time, '%Y%m%d' ) = date_format( b.sys_created_time, '%Y%m%d' ) AND a.id>= b.id AND a.tenant_id = b.tenant_id WHERE a.tenant_id = '1095' GROUP BY date_format( a.sys_created_time, '%Y%m%d' ), a.id ORDER BY date_format( a.sys_created_time, '%Y%m%d' ), a.id
去查看mysql的explain计划,自己要去通过看mysql的执行计划来进行优化的。