8個SQL講解優化
8個SQL講解優化
注意:dept表和emp表需要自己建立,插入資料
1、列出自己的掌門比自己年齡小的人員
SELECT a.`name`,a.`age`,c.`name` ceoname,c.`age` ceoage FROM
t_emp a
LEFT JOIN t_dept b ON a.`deptId`= b.`id`
LEFT JOIN t_emp c ON b.`CEO`= c.`id`
WHERE c.`age`<a.`age`
優化:
EXPLAIN SELECT SQL_NO_CACHE a.`name`,a.`age`,c.`name` ceoname,c.`age` ceoage FROM
emp a
LEFT JOIN dept b ON a.`deptId`= b.`id`
LEFT JOIN emp c ON b.`CEO`= c.`id`
WHERE c.`age`<a.`age`
CREATE INDEX idx_age ON emp(age)
2、列出所有年齡低於自己門派平均年齡的人員
SELECT c.`name`,c.`age`,aa.age FROM t_emp c INNER JOIN
(
SELECT a.`deptId`,AVG(a.`age`)age FROM t_emp a
WHERE a.`deptId` IS NOT NULL
GROUP BY a.`deptId`
)aa ON c.`deptId`=aa.deptid
WHERE c.`age`< aa.age
優化
EXPLAIN SELECT SQL_NO_CACHE c.`name`,c.`age`,aa.age FROM emp c INNER JOIN
(
SELECT a.`deptId`,AVG(a.`age`)age FROM emp a
WHERE a.`deptId` IS NOT NULL
GROUP BY a.`deptId`
)aa ON c.`deptId`=aa.deptid
WHERE c.`age`< aa.age
CREATE INDEX idx_deptid ON emp(deptid)
CREATE INDEX idx_deptid_age ON emp(deptid,age)
3、列出至少有2個年齡大於40歲的成員的門派
SELECT b.`deptName`,COUNT(*) FROM t_emp a
INNER JOIN t_dept b ON b.`id` = a.`deptId`
WHERE a.age >40
GROUP BY b.`deptName`,b.`id`
HAVING COUNT(*)>=2
優化
EXPLAIN SELECT SQL_NO_CACHE b.`deptName`,COUNT(*) FROM
dept b STRAIGHT_JOIN emp a ON b.`id` = a.`deptId`
WHERE a.age >40
GROUP BY b.`deptName`,b.`id`
HAVING COUNT(*)>=2
CREATE INDEX idx_deptid_age ON emp(deptid,age)
CREATE INDEX idx_deptname ON dept(deptname)
STRAIGHT_JOIN 強制確定驅動表和被驅動表
1、概念非常明確
2、對資料量的比例非常明確
4、至少有2位非掌門人成員的門派
SELECT * FROM t_emp a WHERE a.id NOT IN
{
SELECT b.`ceo` FROM t_dept b WHERE b.`ceo`IS NOT NULL
}
NOT IN -->LEFT JOIN xxx ON xx WHERE xx IS NULL
SELECT c.deptname, c.id,COUNT(*) FROM t_emp a
INNER JOIN t_dept c ON a.`deptId` =c.`id`
LEFT JOIN t_dept b ON a.`id`=b.`ceo`
WHERE b.`id` IS NULL
GROUP BY c.`id` ,c.deptname
HAVING COUNT(*)>=2
優化
EXPLAIN SELECT SQL_NO_CACHE c.deptname, c.id,COUNT(*)
FROM dept c STRAIGHT_JOIN emp a
ON a.`deptId` =c.`id`
LEFT JOIN dept b ON a.`id`=b.`ceo`
WHERE b.`id` IS NULL
GROUP BY c.deptname,c.`id`
HAVING COUNT(*)>=2
CREATE INDEX idx_ceo_deptnam ON dept(ceo,deptname)
CREATE INDEX idx_deptnam ON dept(deptname)
CREATE INDEX idx_deptid ON emp(deptid)
SELECT b.`id`,b.`deptName` ,COUNT(*) FROM t_emp a INNER JOIN t_dept b ON a.`deptId`= b.`id`
GROUP BY b.`deptName`,b.`id`
SELECT b.`id`,b.`deptName`, COUNT(*) FROM emp a INNER JOIN dept b ON a.`deptId`= b.`id`
GROUP BY b.`deptName`,b.`id`
UPDATE t_dept SET deptname='明教' WHERE id=5
5、列出全部人員,並增加一列備註“是否為掌門”,如果是掌門人顯示是,不是掌門人顯示否
CASE WHEN
IF
SELECT a.`name`, CASE WHEN b.`id` IS NULL THEN '否' ELSE '是' END '是否為掌門'
FROM t_emp a
LEFT JOIN t_dept b ON a.`id`=b.`ceo`
6、列出全部門派,並增加一列備註“老鳥or菜鳥”,若門派的平均值年齡>50顯示“老鳥”,否則顯示“菜鳥”
SELECT b.`deptName`,
IF (AVG(a.age)>50,'老鳥','菜鳥')'老鳥or菜鳥'
FROM t_emp a
INNER JOIN t_dept b ON a.`deptId`= b.`id`
GROUP BY b.`id` ,b.`deptName`
7、顯示每個門派年齡最大的人
SELECT NAME,age FROM t_emp a
INNER JOIN
(
SELECT deptid,MAX(age) maxage
FROM t_emp
WHERE deptid IS NOT NULL
GROUP BY deptid
) aa ON a.`age`= aa.maxage AND a.`deptId`=aa.deptid
優化
EXPLAIN SELECT SQL_NO_CACHE NAME,age FROM emp a
INNER JOIN
(
SELECT deptid,MAX(age) maxage
FROM emp
WHERE deptid IS NOT NULL
GROUP BY deptid
) aa ON a.`age`= aa.maxage AND a.`deptId`=aa.deptid
CREATE INDEX idx_deptid_age ON emp(deptid,age)
錯例
SELECT b.`deptName`,a.`name`,MAX(a.`age`)FROM t_dept b
LEFT JOIN t_emp a ON b.`id`=a.`deptId`
WHERE a.name IS NOT NULL
GROUP BY b.`deptName`
UPDATE t_emp SET age=100 WHERE id =2
8、顯示每個門派年齡第二大的人
SET @rank=0;
SET @last_deptid=0;
SELECT a.deptid,a.name,a.age
FROM(
SELECT t.*,
IF(@last_deptid=deptid,@rank:=@rank+1,@rank:=1) AS rk,
@last_deptid:=deptid AS last_deptid
FROM t_emp t
ORDER BY deptid,age DESC
)a WHERE a.rk=2;
分組排序
SET @rank=0;
SET @last_deptid=0;
SELECT * FROM
(
SELECT t.*,
IF(@last_deptid=deptid,@rank:=@rank+1,@rank:=1) AS rk,
@last_deptid:=deptid AS last_deptid
FROM t_emp t
ORDER BY deptid,age DESC
) a WHERE a.rk <=1
#oracle rank() over()
UPDATE t_emp SET age=100 WHERE id =1
SET @rank=0;
SET @last_deptid=0;
SET @last_age=0;
SELECT t.*,
IF(@last_deptid=deptid,
IF(@last_age = age,@rank,@rank:=@rank+1)
,@rank:=1) AS rk,
@last_deptid:=deptid AS last_deptid,
@last_age :=age AS last_age
FROM t_emp t
ORDER BY deptid,age DESC
相關文章
- SQL優化常用方法8SQL優化
- 一個SQL優化SQL優化
- 一個sql的優化SQL優化
- 幫朋友優化個sql優化SQL
- 如何優化這個sql?優化SQL
- [記錄]30個Oracle SQL優化規則詳解OracleSQL優化
- 記一個SQL優化案例SQL優化
- Bitmap的有關講解與優化優化
- 使用with as優化sql解決filter優化SQLFilter
- sql優化講課中引出的各種問題!SQL優化
- CSS效能優化的8個技巧CSS優化
- 一個SQL語句的優化SQL優化
- Python線性優化基礎講解~Python優化
- MySQL之SQL優化詳解(一)MySql優化
- MySQL之SQL優化詳解(二)MySql優化
- MySQL之SQL優化詳解(三)MySql優化
- 【SQL優化】SQL優化工具SQL優化
- SQL Server優化之SQL語句優化SQLServer優化
- SQL優化SQL優化
- with as優化sql優化SQL
- 效能優化案例-SQL優化優化SQL
- MySQL幾個簡單SQL的優化MySql優化
- 【SQL Server 優化效能的幾個方面】SQLServer優化
- 深入瞭解 TiDB SQL 優化器TiDBSQL優化
- SQL優化----dbms_sqltune詳解(1)SQL優化
- oracle sql tuning 8--優化全表掃描OracleSQL優化
- 資料庫優化 - SQL優化資料庫優化SQL
- sql優化之邏輯優化SQL優化
- SQL優化:limit分頁優化SQL優化MIT
- MySQL 效能優化之SQL優化MySql優化
- Mysql優化_ORDER BY和GROUP BY 的優化講解(單路排序和雙路排序)MySql優化排序
- SQL優化--用各種hints優化一條SQLSQL優化
- 【SQL優化】SQL優化的10點注意事項SQL優化
- SQL SERVER中SQL優化SQLServer優化
- Oracle SQL精妙SQL語句講解OracleSQL
- MySQL 52個SQL效能優化策略SQL語句彙總MySql優化
- 矩陣相乘優化演算法實現講解矩陣優化演算法
- 一個SQL效能問題的優化探索SQL優化