SQL优化的一些实用方法
1、任何在where子句中使用is null或is not null的语句优化器是不允许使用索引的。因为只有该字段中有null值,即使创建了索引其实也 是没有用的,所以创建索引应该在有值的字段上创建; 2、使用该sql语句将不会使用索引:select * from employee where last_name like ‘%cliton%'; 这样的话会使用索引: select * from employee where last_name like ‘cliton%'; 3、Order by语句 ORDER BY语句决定了Oracle如何将返回的查询结果排序。Order by语句对要排序的列没有什么特别的限制,也可以将函数加入列中(象联接或 者附加等)。任何在Order by语句的非索引项或者有计算表达式都将降低查询速度。仔细检查order by语句以找出非索引项或者表达式,它们 会降低性能。解决这个问题的办法就是重写order by语句以使用索引,也可以为所使用的列建立另外一个索引,同时应绝对避免在order by 子句中使用表达式。 4、 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理,在FROM子句 中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以上的表连接查询, 那就需要选择交叉表(intersection table)作为基础表, 交叉表是指那个被其他表所引用的表. 5、 SELECT子句中避免使用 ‘ * ‘: ORACLE在解析的过程中, 会将’*’ 依次转换成所有的列名, 这个工作是通过查询数据字典完成的, 这意味着将耗费更多的时间 6、 减少访问数据库的次数,能使用一条sql语句查询出来的最好使用一条语句直接查询出来。 整合简单,无关联的数据库访问: 如果你有几个简单的数据库查询语句,你可以把它们整合到一个查询中(即使它们之间没有关系) 7、通过内部函数提高SQL效率.: 复杂的SQL往往牺牲了执行效率. 能够掌握上面的运用函数解决问题的方法在实际工作中是非常有意义的 8、 使用表的别名(Alias): 当在SQL语句中连接多个表时, 请使用表的别名并把别名前缀于每个Column上.这样一来,就可以减少解析的时间并减少那些由Column歧义引起 的语法错误. 9、 用EXISTS替代IN、用NOT EXISTS替代NOT IN: 在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用EXISTS(或NOT EXISTS)通常将提高查询的 效率. 在子查询中,NOT IN子句将执行一个内部的排序和合并. 无论在哪种情况下,NOT IN都是最低效的 (因为它对子查询中的表执行了一个 全表遍历). 为了避免使用NOT IN ,我们可以把它改写成外连接(Outer Joins)或NOT EXISTS. 例: […]
View DetailsShell入门看我吧
背景 之前写了系列的shell实战的文章,得到了挺多小伙伴的关注,遇到有些对shell不太熟悉小伙伴,所以有个想法写一篇shell入门的文章。 时间流逝,再回头去看看过去的东西,才会发现哪些东西比较重要,故撰此文,记录我在过去学习和使用shell语言过程中我个人觉得比较重要的部分,做一个小总结和分享。 文章中使用到的代码位置:https://gitee.com/dhar/YTTInjectedContentKit/tree/master/DevPods/InjectedContentKit/Example/injectContentShell 和 https://gitee.com/dhar/ShellLearning/tree/master/helloshell 文章内容结构如下: 语法 变量 打印 运算 控制 循环 容器 方法 文件 文件读取 文件写入 sed流编辑 模块 工具模块 流程模块 输入和菜单 获取输入 菜单 语法 变量 变量的定义 定义一个变量和其他语言的类似,shell是弱类型语言所以不需要使用类型限定,并且变量可以修改类型,下面的例子定义了一个字符串类型的str变量,之后修改为数值类型 注意点:变量等号两边不能有空格出现
1 2 3 4 5 |
str="string" echo $str echo ${str} str=123 echo $str |
变量使用 变量可以赋值给另一个变量和打印,当变量被使用的时候需要在变量名称前面加上$符号,还有另一种方式是把变量名放在${}括号中使用,可以把命令执行结果作为右值赋值给变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
str2=$str echo $str2; str3=${str} echo ${str3} curDir=$(pwd) echo "curDir = ${curDir}" curDirCon=`ls` echo "curDirCon = ${curDir2}" # 输出: =======变量======= string string 123 123 123 curDir = /Users/aron/git-repo/ShellLearning/helloshell curDirCon = data syntax.sh |
打印 因为shell没有单步调试和其他功能强大的IDE,所以打印功能就经常使用到,此外打印功能还可以当做函数的返回值,比return作为函数的返回值功能更强大,shell 使用echo打印,内容默认输出到控制台中,echo可以打印字符串、变量、以及字符串中嵌入变量的混个内容,echo有几重要的参数选项 -e 转义字符处理,比如\t显示为制表符而不是显示输出\t -n 把文本字符串和命令输出显示在同一行中
1 2 3 4 5 6 7 8 9 10 |
str4="string4" echo $str4 echo "str4=$str4" echo "str4=${str4}str3=${str3}" # 输出: =======打印======= string4 str4=string4 str4=string4str3=123 |
运算 使用expr执行算术运算 注意点:*乘法运算符号需要转义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
echo "=======运算=======" result=$(expr 5 + 5) echo ${result} result=$(expr 16 - 5) echo ${result} result=$(expr 5 \* 5) echo ${result} result=$(expr 28 / 5) echo ${result} # 输出: =======expr运算======= 10 11 25 5 |
使用[]执行算术 []执行算术比expr简单多了,并且*乘法运算符号不需要转义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
echo "=======[]运算=======" result=$[5 + 5] echo ${result} result=$[16 - 5] echo ${result} result=$[5 * 5] echo ${result} result=$[28 / 5] echo ${result} # 输出: =======[]运算======= 10 11 25 5 |
控制 数值比较 控制使用if/else/fi语法,典型的数值比较如下
1 2 3 4 5 6 7 8 |
if [[ 3 > 7 ]]; then echo "hehe" else echo "yes" fi # 输出: yes |
还可以使用下面的比较符号: 比较符 描述 n1 -eq n2 检查n1是否与n2相等 n1 -ge n2 检查n1是否大于或等于n2 n1 -gt n2 检查n1是否大于n2 n1 -le n2 检查n1是否小于或等于n2 n1 -lt n2 检查n1是否小于n2 n1 -ne n2 检查n1是否不等于n2 一个简单的9*9乘法口诀表的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
echo "9*9=======" i=1 j=1 line="" while [[ i -lt 10 ]]; do j=1 line="" until [[ j -eq 10 ]]; do if [[ j -le i ]]; then result=$(expr $i \* $j) resultStr="$j X $i = $result" line=${line}${resultStr}"\t" fi j=$(expr $j + 1) done echo -e ${line} i=$(expr $i + 1) done # 输出: 9*9======= 1 X 1 = 1 1 X 2 = 2 2 X 2 = 4 1 X 3 = 3 2 X 3 = 6 3 X 3 = 9 1 X 4 = 4 2 X 4 = 8 3 X 4 = 12 4 X 4 = 16 1 X 5 = 5 2 X 5 = 10 3 X 5 = 15 4 X 5 = 20 5 X 5 = 25 1 X 6 = 6 2 X 6 = 12 3 X 6 = 18 4 X 6 = 24 5 X 6 = 30 6 X 6 = 36 1 X 7 = 7 2 X 7 = 14 3 X 7 = 21 4 X 7 = 28 5 X 7 = 35 6 X 7 = 42 7 X 7 = 49 1 X 8 = 8 2 X 8 = 16 3 X 8 = 24 4 X 8 = 32 5 X 8 = 40 6 X 8 = 48 7 X 8 = 56 8 X 8 = 64 1 X 9 = 9 2 X 9 = 18 3 X 9 = 27 4 X 9 = 36 5 X 9 = 45 6 X 9 = 54 7 X 9 = 63 8 X 9 = 72 9 X 9 = 81 ======= ======= |
[…]
View DetailsLua
Lua 是一个小巧的脚本语言。是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发。 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。 Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。Lua 有一个同时进行的JIT项目,提供在特定平台上的即时编译功能。 Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,ini等文件格式,并且更容易理解和维护。 Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。 一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
View DetailsOpenResty
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。 OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。 OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。 from:http://openresty.org/cn/
View DetailsTensorFlow
TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。 TensorFlow可被用于语音识别或图像识别等多项机器学习和深度学习领域,对2011年开发的深度学习基础架构DistBelief进行了各方面的改进,它可在小到一部智能手机、大到数千台数据中心服务器的各种设备上运行。TensorFlow将完全开源,任何人都可以用。
View DetailsSpring Framework 多个安全漏洞预警
4月5日,Pivotal 发布了 Spring Framework 存在多个安全漏洞的公告: (1)spring-messaging 模块远程代码执行漏洞 对应CVE编号:CVE-2018-1270 漏洞公告链接:https://pivotal.io/security/cve-2018-1270 (2)运行于 Windows 系统的 Spring MVC 存在目录遍历漏洞 对应CVE编号:CVE-2018-1271 漏洞公告链接:https://pivotal.io/security/cve-2018-1271 (3)Spring MVC 或 Spring WebFlux 服务器存在 Multipart 类型污染漏洞 对应CVE编号:CVE-2018-1272 漏洞公告链接:https://pivotal.io/security/cve-2018-1272 漏洞描述 CVE-2018-1270漏洞:Spring Framework的5.*版本、4.3.*版本以及不再支持的旧版本,通过spring-messaging和spring-websocket模块提供的基于WebSocket的STOMP,存在被攻击者建立WebSocket连接并发送恶意攻击代码的可能,从而实现远程代码执行攻击,建议尽快更新到新的版本。 CVE-2018-1271漏洞:Spring Framework的5.*版本、4.3.*版本以及不再支持的旧版本,Spring MVC允许应用程序对其配置提供静态资源,在Windows系统上实现该功能时,攻击者通过请求构造的特定资源URL,可能导致目录遍历的效果产生,建议尽快更新到新的版本。 CVE-2018-1272漏洞:Spring Framework的5.*版本、4.3.*版本以及不再支持的旧版本,当Spring MVC或Spring WebFlux服务器接受把客户端请求再转向另一台服务器的场景下,攻击者通过构造和污染Multipart类型请求,可能对另一台服务器实现权限提升攻击,建议尽快更新到新的版本。 漏洞影响范围 Spring spring-messaging远程代码执行漏洞(CVE-2018-1270)、(CVE-2018-1271)、(CVE-2018-1272)等影响版本如下: (1)Spring Framework 5.*(5.0到5.0.4)版本,建议更新到5.0.5版本 (2)Spring Framework 4.3.*(4.3到4.3.14)版本,建议更新到4.3.15版本 (3)以及不再受支持的旧版本,建议更新到4.3.15版本或5.0.5版本 官方历史安全公告列表,请参考: https://pivotal.io/security/ https://spring.io/blog/2018/04/05/multiple-cve-reports-published-for-the-spring-framework from:https://www.oschina.net/news/94947/multiple-cve-reports-published-for-the-spring-framework
View DetailsC# 获取文件名及扩展名
string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastIndexOf(".") – aFile.LastIndexOf("\\") – 1)); //文件名 string aLastName = aFile.Substring(aFile.LastIndexOf(".") + 1, (aFile.Length – aFile.LastIndexOf(".") – 1)); //扩展名 string strFilePaht="文件路径"; Path.GetFileNameWithoutExtension(strFilePath);这个就是获取文件名的 还有的就是用Substring截取 strFilePaht.Substring(path.LastIndexOf("\\") + 1, path.Length – 1 – path.LastIndexOf("\\")); strFilePaht.Substring(path.LastIndexOf("."), path.Length – path.LastIndexOf(".")); 或者用openFileDialog1.SafeFileName 这样就能取到该文件的所在目录路径 string path1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + @"\"; string path = Path.GetFileName("C:\My Document\path\image.jpg"); //只获取文件名image.jpg ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// string fullPath = @"\WebSite1\Default.aspx"; string filename = System.IO.Path.GetFileName(fullPath);//文件名 “Default.aspx” string extension = System.IO.Path.GetExtension(fullPath);//扩展名 “.aspx” string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullPath);// 没有扩展名的文件名 “Default” ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// System.IO.Path.GetFileNam(filePath) //返回带扩展名的文件名 System.IO.Path.GetFileNameWithoutExtension(filePath) //返回不带扩展名的文件名 System.IO.Path.GetDirectoryName(filePath) //返回文件所在目录 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //获取当前进程的完整路径,包含文件名(进程名)。 string str = this.GetType().Assembly.Location; […]
View Details如何查看centos系统cpu/内存使用情况
1.查看硬盘 [mushme@investide ~]$ df -ah 文件系统 容量 已用 可用 已用% 挂载点 /dev/cciss/c0d0p1 123G 63G 55G 54% / proc 0 0 0 – /proc sysfs 0 0 0 – /sys devpts 0 0 0 – […]
View DetailsCentOS 7服务器安全配置(未完待续)
1.问题背景 本打算买一个Linux服务器玩玩,系统为CentOS 7,供学习Linux和线上部署网站学习用。没想到买了没几天,啥都没做呢,登录之后发现了近2万条登录失败记录(输入lastb命令即可查看登入系统失败的用户相关信息)。什么?有人想通过暴力破解密码来登录我的服务器?!并且大量的攻击来自同一个ip地址。作为一个Linux小白,差点被吓尿了,网络真危险啊。 不过虽然是小白,也不甘示弱,有攻击就要有防御,现状开始逼迫我去学习关于Linux服务器安全防护的知识,要守住自己在网上的一块领地。 2.安装Fail2ban 在Q群上和小伙伴讨论过程中,发现了fail2ban这个利器。fail2ban能阻止暴力破解,如果fail2ban发现一个ip在暴力攻击,攻击次数达到一定次数时,就会禁止改ip连接服务器,以达到阻止暴力破解密码的目的。之前看登录失败日志,一个ip攻击了我上万次,现在有了fail2ban,它可做不了了,赶紧开始我们的安装。 安装fail2ban 输入以下两条命令即可安装
1 2 |
yum install epel-release yum install fail2ban |
说明: – yum install epel-release:安装EPEL仓库(Extra Packages for Enterprise Linux) – yum install fail2ban:从EPEL仓库安装fail2ban fail2ban配置文件 打开配置文件
1 |
nano /etc/fail2ban/jail.conf |
开头会见到如下说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# # WARNING: heavily refactored in 0.9.0 release. Please review and # customize settings for your setup. # # Changes: in most of the cases you should not modify this # file, but provide customizations in jail.local file, # or separate .conf files under jail.d/ directory, e.g.: # # HOW TO ACTIVATE JAILS: # # YOU SHOULD NOT MODIFY THIS FILE. # # It will probably be overwritten or improved in a distribution update. # # Provide customizations in a jail.local file or a jail.d/customisation.local. # For example to change the default bantime for all jails and to enable the # ssh-iptables jail the following (uncommented) would appear in the .local file. # See man 5 jail.conf for details. # # [DEFAULT] # bantime = 3600 # # [sshd] # enabled = true # # See jail.conf(5) man page for more information |
大意是不要修改这个配置文件,而应该新建一个jail.conf来写用户配置。 先查看配置文件内默认的配置(仅列出前面5项):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[DEFAULT] # # MISCELLANEOUS OPTIONS # # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space (and/or comma) separator. ignoreip = 127.0.0.1/8 # External command that will take an tagged arguments to ignore, e.g. <ip>, # and return true if the IP is to be ignored. False otherwise. # # ignorecommand = /path/to/command <ip> ignorecommand = # "bantime" is the number of seconds that a host is banned. bantime = 600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 5 |
ignoreip 不会被ban的ip bantime 每秒内访问的最大次数,超过就被ban maxretry 最大失败次数 这里只是简单举例,其他一些配置不多做赘述,有需要的可以自己去看英文注释。 于是新建/打开一个jail.conf,写入用户自己的配置
1 |
nano /etc/fail2ban/jail.local |
把下列代码拷进去
1 2 3 4 5 6 7 8 |
[ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] # sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com] logpath = /var/log/secure maxretry = 5 |
说明: – enabled: 激活fail2ban。 – filter: 是sshd默认参考这个文件/etc/fail2ban/filter.d/sshd.conf。 – action: 符合/etc/fail2ban/action.d/iptables.conf这个文件的ip将会被fail2ban给ban掉. 如果你之前改过ssh端口,把 port=ssh改成新端口, 比如 port=2222. 如果还在用22,就不用改这里。 – logpath: Fail2Ban的日志文件路径. – maxretry: 最大尝试登陆失败次数. 启动fail2ban服务 运行这两个命令即可启动
1 2 |
chkconfig --level 23 fail2ban on service fail2ban start |
查看一下iptables是否添加了fail2ban的规则
1 |
iptables -L |
会看到如下字样f2b-SSH
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Chain INPUT (policy ACCEPT) target prot opt source destination f2b-SSH tcp -- anywhere anywhere tcp dpt:EtherNet/IP-1 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain f2b-SSH (1 references) target prot opt source destination RETURN all -- anywhere anywhere |
查看登录失败日志 输入这条命令
1 |
cat /var/log/secure | grep 'Failed password' |
会看到类似这样的结果
1 2 3 4 5 6 7 8 9 10 11 12 |
Dec 6 22:47:12 vultr sshd[7942]: Failed password for root from 43.229.53.67 port 23021 ssh2 Dec 6 22:47:15 vultr sshd[7944]: Failed password for root from 43.229.53.67 port 40996 ssh2 Dec 6 22:47:16 vultr sshd[7944]: Failed password for root from 43.229.53.67 port 40996 ssh2 Dec 6 22:47:18 vultr sshd[7944]: Failed password for root from 43.229.53.67 port 40996 ssh2 Dec 6 22:47:31 vultr sshd[7948]: Failed password for root from 43.229.53.67 port 29907 ssh2 Dec 6 22:47:34 vultr sshd[7948]: Failed password for root from 43.229.53.67 port 29907 ssh2 Dec 6 22:47:36 vultr sshd[7948]: Failed password for root from 43.229.53.67 port 29907 ssh2 Dec 6 22:47:39 vultr sshd[7950]: Failed password for root from 43.229.53.67 port 48386 ssh2 Dec 6 22:47:41 vultr sshd[7950]: Failed password for root from 43.229.53.67 port 48386 ssh2 Dec 6 22:47:43 vultr sshd[7950]: Failed password for root from 43.229.53.67 port 48386 ssh2 Dec 6 22:47:47 vultr sshd[7952]: Failed password for root from 43.229.53.67 port 62846 ssh2 Dec 6 22:47:49 vultr sshd[7952]: Failed password for root from 43.229.53.67 port 62846 ssh2 |
3.修改22端口并启用 先在sshd_config里面添加新端口 查看sshd_confifg
1 |
/etc/ssh/sshd_config |
可以看到如下片段:
1 2 3 4 5 6 7 8 |
# If you want to change the port on a SELinux system, you have to tell # SELinux about this change. # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER Port 22 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress :: |
添加一个新端口,如22222,在Port 22下面添加一行Port 22222,Port 22先不要注释掉,以防新端口登录不上。 再在防火墙里添加新端口 查看防火墙运行状态
1 |
systemctl status firewalld.service |
查看所有打开的端口
1 |
firewall-cmd --zone=public --list-ports |
添加端口
1 |
firewall-cmd --zone=public --add-port=22222/tcp --permanent |
[…]
View Details