Coder Social home page Coder Social logo

sql-standard's Introduction

sql-standard

背景: 由于开发人员水平参差不齐,即使订了一些口头sql规范、书面sql规范,还是避免不了不规范、以及不安全的情况 ,并且SQL是影响系统性能最重要的因素,所以拦截掉垃圾SQL语句还是有一定必要性 目标: 1、杜绝掉一些垃圾,不安全sql(前期已经定义一些标准,后期可以自定义更改) • 业务上具有唯一特性的字段,即使是多个字段的组合,也必须建成唯一索引。 • 超过三个表禁止 join。需要 join 的字段,数据类型必须绝对一致;多表关联查询时, 保证被关联的字段需要有索引。(原因:性能低,sql语句相对复杂不利于维护、理解,如果以后分库分表不利) • 说明:即使双表 join 也要注意表索引、SQL 性能。 • 在 varchar 字段上建立索引时,必须指定索引长度,没必要对全字段建立索引,根据 实际文本区分度决定索引长度即可。 • 不要使用 count(列名)或 count(常量)来替代 count(),count()是 SQL92 定义的 标准统计行数的语法,跟数据库无关,跟 NULL 和非 NULL 无关。说明:count(*)会统计值为 NULL 的行,而 count(列名)不会统计此列为 NULL 值的行。 • 不要使用 select * ,建议需要什么数据就拿什么数据,多余的数据字段需要网络带宽、内存,影响性能 • where后面必须要有条件,必须要用到索引,包含left join连接字段,符合索引最左原则(原因:1、如果因为动态SQL,bug导致update,delete的where条件没有带上,全表操作; 2、使用了索引,SQL性能基本不检查到会太差) • sql不等于尽量使用<>,!=有些版本、平台上不适用,而<>是sql标准不等于 • where后面尽量不适用数据库函数 .......未完待续 2、防止全表更新与删除 防止开发同学写sql代码的时候,全局的更新、删除、查询.当数据量比较大的时候,全局的查询就会导致线上系统卡顿(之前药店那边就有这一个线上事故) 例如: select * from A update A set A.name = “” delete from A 3、杜绝sql注入,以及修改建议 • 建议使用“#{}”形式进行预编译SQL处理,不推荐使用“${}”方式。 例如: update organization set organization_address = #{param} where deleted = 0 and id = #{id} 4、禁止sql执行时间过长(超过某个限定值) 待开发 使用方式: 如果本身有mybatis-plus.jar com.zhiyun.hospital sql-standard 1.0-SNAPSHOT com.baomidou mybatis-plus-boot-starter 2、启动类上增加注解 #属性value是规则适用的路径 @EnableSqlStandard(value = "com.test") public class Bootstrap { public static void main(String[] args) { SpringApplication.run(Bootstrap.class, args); } } 忽视拦截校验方式 如果针对具体XXX.mapper.java方法不进行相应sql规范校验,可以在相应的方法上加注解@InterceptorIgnore,具体方式如下(除非特殊场景下,一般不予使用): public interface ReportTemplateDAO extends BaseMapper { @InterceptorIgnore List getReportTemplatePage(Page page,@Param("params") Map param); }

sql-standard's People

Contributors

zhangfanghui8 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.