sql查詢代碼邏輯優化

直接上代碼
		// 在for外查詢 list
		List<QueAttr> byIds = queAttrService.findByIds(Lists.newArrayList(attrIds));
		Map<Long, String> attrId_name = byIds.stream().filter(Objects::nonNull)
				.collect(Collectors.toMap(QueAttr::getAttrId, QueAttr::getName));

		for (QueAttrPath queAttrPath : queAttrPaths) {
			List<Long> pathIds = queAttrPath.getPathIds();
			List<Map<String, String>> names = Lists.newArrayList();
			for (Long pathId : pathIds) {
				String name = attrId_name.get(pathId);// attrId
//				String name = queAttrService.getCacheAttr(pathId).getString("name"); for內查詢 即使查詢redis也比for耗時長
				if (StringUtils.isNotBlank(name)) {
					Map<String, String> _temp = Maps.newHashMap();
					_temp.put("attrId", pathId.toString());
					_temp.put("name", name);
					names.add(_temp);
				}
			}
			queAttrPath.setName(names);
		}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章