[TOC]
# 原始SQL
```
SELECT `a`.`province_id`,count(province_id) as vote_num,`b`.`name`,`b`.`people_num`,`b`.`img`,count(a.uid) as known FROM `cmf_region_relevance` `a` LEFT JOIN `cmf_region` `b` ON `b`.`id`=`a`.`province_id` WHERE `b`.`level` = 1 GROUP BY `a`.`province_id` ORDER BY `vote_num` DESC
```
返回行数:31 更新行数:0 执行耗时:138ms
>注:高版本的这个group by 可能不解析,sql server肯定会报错,mysql8.0可能会报错!
# 建议优化后:
```
SELECT `a`.`province_id`, COUNT(province_id) AS vote_num, `b`.`name`, `b`.`people_num`, `b`.`img`
, COUNT(a.uid) AS known
FROM `cmf_region_relevance` `a`
LEFT JOIN `cmf_region` `b` ON `b`.`id` = `a`.`province_id`
WHERE `b`.`level` = 1
GROUP BY `a`.`province_id`,`b`.`name`, `b`.`people_num`, `b`.`img`
ORDER BY `vote_num` DESC
```
用时: 返回行数:31 更新行数:0 执行耗时:2179ms
>很明显,这里不问题,耗时更长!
# 重写建议:
```
SELECT
`cmf_region_relevance`.`province_id`,
COUNT(*) AS `vote_num`,
CAST(`t`.`name` AS CHAR(32)) AS `name`,
CAST(`t`.`people_num` AS UNSIGNED) AS `people_num`,
CAST(`t`.`img` AS CHAR(255)) AS `img`,
COUNT(*) AS `known`
FROM (SELECT *
FROM `public_health`.`cmf_region`
WHERE CAST(`level` AS UNSIGNED) = 1) AS `t`
INNER JOIN `public_health`.`cmf_region_relevance` ON `t`.`id` = `cmf_region_relevance`.`province_id`
GROUP BY
`cmf_region_relevance`.`province_id`,
CAST(`t`.`name` AS CHAR(32)),
CAST(`t`.`people_num` AS UNSIGNED),
CAST(`t`.`img` AS CHAR(255))
ORDER BY
ISNULL(COUNT(*)),
COUNT(*) DESC
```
返回行数:31 更新行数:0 执行耗时:128ms
>很明显更清晰
# 建议增加索引
索引1:
DDL语句:
ALTER TABLE `public_health`.`cmf_region` ADD INDEX rds_idx_0 (`level`);
索引2:
DDL语句:
ALTER TABLE `public_health`.`cmf_region_relevance` ADD INDEX rds_idx_1 (`province_id`);
现存索引:
public_health.cmf_region_relevance: `idx_provinceid_uid` (`province_id`, `uid`)
==============
增加索引后
返回行数:31 更新行数:0 执行耗时:123ms
- centos
- 安装工具
- 安装nginx
- 安装PHP5
- 安装PHP7.0.1
- 安装PHP7.0.8
- 安装redis
- redis允许远程访问
- 安装php之redis扩展
- 安装mysql
- 配置nginx虚拟机绑定域名
- xdebug下载配置
- phpStorm远程调试配置
- 报错解决:安装PHP7解决问题方法
- 报错解决:xdebug-解决问题
- yum丢失的解决办法
- CentOS下安装JDK的四种方法
- workman
- 某服务器配置情况
- 安装PHP7.2.17
- 安装PHP7.1.27
- 安装MongoDB
- ubuntu
- mac
- window
- phpstrom+wamp+xdebug
- mysql
- MySql创建本地用户和远程用户 并赋予权限
- 自建主从复制-mycat
- 数据库优化
- 阿里云mysql主从复制
- 报错解决
- SQL分析
- group by
- Mysql定时备份数据脚本
- MySQL数据库远程连接开启方法
- 启动报错systemctl status
- 日志导出
- mysq进程
- mysql查询正在执行的进程
- 命令
- nginx
- 安装GIT
- access.log
- error.log分析
- 500 Internal Server Error错误
- 502解决方案
- 405 Not Allowed,nginx静态文件响应post请求
- Linux基本操作
- 创建用户
- chmod命令详细用法设置文件的权限
- chown命令
- chgrp命令:改变文件的群组
- Linux 设置定时任务crontab命令
- 其他问题
- Win10 Subsystem Linux : Ubuntu 的root密码
- 安全问题
- PHP安全设置
- redis
- 安装
- 安装2.8.17
- 问题
- 日志分析
- an upstream response is buffered to a temporary file
- too many open files
- worker_connections are not enough
- recv() failed
- 日志
- 系统日志
- apache访问日志与错误日志
- nginx访问日志与错误日志
- php错误日志
- php-fpm慢日志
- mysql慢日志
- 服务器优化
- php-fpm进程数优化
- 服务器安全
- RHSA-2018:2748: kernel security and bug fix update
- RHSA-2018:3408: git security update
- RHSA-2018:2570: bind security update
- RHSA-2018:3052: wget security and bug fix update
- RHSA-2018:3221: openssl security, bug fix, and enhancement update
- RHSA-2018:2384: kernel security and bug fix update
- RHSA-2018:3032: binutils security, bug fix, and enhancement update
- RHSA-2018:3157: curl and nss-pem security and bug fix update
- RHSA-2018:2285: yum-utils security update
- RHSA-2018:3092: glibc security, bug fix, and enhancement update
- CVE-2018-17182 on Ubuntu 14.04 LTS (trusty)
- CVE-2018-9415 on Ubuntu 14.04 LTS (trusty)
- CVE-2018-8043 on Ubuntu 14.04 LTS (trusty)
- CVE-2018-3620 on Ubuntu 14.04 LTS (trusty)
- CVE-2018-14634 on Ubuntu 14.04 LTS (trusty)
- CVE-2018-14609 on Ubuntu 14.04 LTS (trusty)
- CentOS Linux 7安全基线检查
- Redis安全基线检查
- RHSA-2019:1168-重要: 内核 安全更新
- RHSA-2019:1481-重要: 内核 安全更新
- RHSA-2019:0512-重要: 内核 安全和BUG修复更新
- ThinkPHP漏洞
- ThinkPHP 5.1.X <= 5.1.30 远程代码执行漏洞
- ThinkPHP 5 <=5.0.22 远程代码执行高危漏洞
- ThinkPHP <5.0.24 Request.php 远程代码执行漏洞
- PHP
- 怎样获取PHP各种版本
- 攻击
- SSH暴力破解
- RDP暴力破解
- SQLSERVER暴力破解
- MYSQL暴力破解
- FTP暴力破解
- SQL注入
- 代码执行
- XSS攻击
- 本地文件包含
- 远程文件包含
- 脚本木马
- 上传漏洞
- 路径遍历
- 越权访问
- CSRF
- CRLF
- 其他