以前的项目,今天让我数据优化,花了一天多时间把业务重写下
不要再循环中进行数据库查询语句
可以选用直接在外面把全部数据查询出来
然后用代码去遍历查询数据
用内存换时间
目前我优化后效率从7s到2s左右
MyBatis Plus 通过实体获取表名 TableName,还是挺方便的,使用SqlHelper工具类即可,
代码如下:
SqlHelper.table(SysUser.class).getTableName()
/**
* 使用java.text.DecimalFormat实现
*
* @param x
* @param y
* @return
*/
public static String getPercent(int x, int y) {
if (x == 0) return "0.00%";
double d1 = x * 1.0;
double d2 = y * 1.0;
// 设置保留几位小数, “.”后面几个零就保留几位小数,这里设置保留两位小数
DecimalFormat decimalFormat = new DecimalFormat("##.00%");
return decimalFormat.format(d1 / d2);
}
/**
* 方式一:使用java.text.NumberFormat实现
* @param x
* @param y
* @return
*/
public static String getPercent(int x, int y) {
double d1 = x * 1.0;
double d2 = y * 1.0;
NumberFormat percentInstance = NumberFormat.getPercentInstance();
// 设置保留几位小数,这里设置的是保留两位小数
percentInstance.setMinimumFractionDigits(2);
return percentInstance.format(d1 / d2);
}