zoukankan      html  css  js  c++  java
  • 进阶之路004 增删改查/数据导入导出之查询功能

    查询时注意:

    (模糊查询     a.device_id  LIKE CONCAT('%',#{deviceId},'%'))

    1,查询时尽量不要用select * ,查询到精确的字段

    2,查询结果只要一条或最大最小值时,添加 limit 1; //可以在查到数据后避免接下来不必要的查询

    3,where条件语句中尽量不要用 or  ,最好用 union all;     例如:select * from student where id=1 union all select * from student where name="张三";

    4, 分页查询时,当数据量特别大时,最好用:order by + 索引 : select id,name from employee order by id  limit 10000,10

    5,模糊查询时,查询带有%的数据时必须要将%放在后面不然会查询失败,正确例题:select * from user where name like "张%

    6,尽量避免在索引列上使用mysql的内置函数

    7,查询最近七天内登陆过的用户(假设 loginTime 加了索引)

    8,应该避免在where子句中对字段进行表达式操作,这将导致系统放弃使用索引二进行全表扫描

    9,inner join 、left join、right join 优先使用 inner join ,若使用lift join 左表尽量小

    10,查询时尽量少用distinct

    11,不要有超过5张表以上的表连接,同时索引不要太多5个以内;

    12,尽量用union all 来代替union

    13,索引尽量不要建立在大量重复的字段上 如性别

    14,连接多个表时,尽量使用表的别名;

    15,尽量用varchar/nvarchar 代替 char

    16,先过滤掉不需要的记录,再执行group by语句

    17, 日常尽量使用explain来分析SQL的计划,来判断走不走索引这一块 例如: explain select * from  id=13 or name="张三";

    //Controller

    @ResponseBody
    @RequestMapping("/lightSplitBox")
    @RequiresPermissions("zkgj:detail:list")
    public R lightSplitBox(@RequestParam Map<String, Object> params){

    PageUtils page = detailService.lightSplitBox(params);

    String s=(String) params.get("params001");
    return R.ok().put("page", page);
    }

    //Service接口

    PageUtils lightSplitBox(Map<String, Object> params);//查询所有的分光想

    //接口实现类

    @Override
    public PageUtils lightSplitBox(Map<String, Object> params) {
    long page=Long.parseLong((String) params.get("page"));
    long limit=Long.parseLong((String) params.get("limit"));
    String order=(String) params.get("order");//获取排序方式
    String sidx=(String) params.get("sidx");//获取排序的字段
    DetailEntity detailEntity=new DetailEntity();
    String opticalBoxName=(String)params.get("opticalBoxName");//获取查询参数
    String opticalBoxType=(String)params.get("opticalBoxType");//获取查询参数
    Page<DetailEntity> pagination = new Page<>(page,limit); //分页
    List<DetailEntity> list = detailDao.lightSplitBox(pagination,order,sidx,opticalBoxType,opticalBoxName);
    pagination.setRecords(list);
    return new PageUtils(pagination);
    }

    //Dao层

    List<DetailEntity> lightSplitBox(@Param("page") Page<DetailEntity> page,@Param("order") String order,@Param("sidx") String sidx,@Param("opticalBoxType") String opticalBoxType,@Param("opticalBoxName") String opticalBoxName);

    //sql

    <select id="getDetail" resultType="io.renren.modules.zkgj.entity.DetailEntity">
    select
    DATE_FORMAT(a.check_date,'%Y-%m-%d') AS checkDateString,
    a.id,
    a.operator,
    a.user_info,
    a.check_date as checkDate,
    COUNT(`operator`) AS sumPort
    FROM tb_detail a
    <where>
    <if test="operator !=null and operator != '' ">
    and a.operator like '%${operator}%'
    </if>
    <if test="checkDateString !=null and checkDateString != '' ">
    and DATE_FORMAT(a.check_date,'%Y-%m-%d')=#{checkDateString}
    </if>
    <if test="params.sql_filter!=null">
    and ${params.sql_filter}
    </if>
    </where>
    GROUP BY DATE_FORMAT(a.check_date,'%Y-%m-%d'),a.operator


    <choose>
    <when test="sidx !='' and sidx != null">
    ORDER BY a.${sidx} ${order}
    </when>
    <otherwise>
    ORDER BY a.check_date desc
    </otherwise>
    </choose>
    </select>

  • 相关阅读:
    随机产生16进制颜色值
    关于单元测试的思考--Asp.Net Core单元测试最佳实践
    使用xUnit为.net core程序进行单元测试
    SQLSERVER——查看阻塞信息(sp_who_lock优化无误版)
    SQLServer连接查询之Cross Apply和Outer Apply的区别及用法
    .netcore 写日志(使用NLog,log4net)
    概率与影响矩阵
    WebApi Helper帮助文档 swagger
    C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
    解决哈希(HASH)冲突的主要方法
  • 原文地址:https://www.cnblogs.com/pureray-hui/p/12371453.html
Copyright © 2011-2022 走看看