沒想到mysql的子查詢這麼弱
select t.id_1, t.id_2
from aaa t
where 1 = 1
and t.id_1 in ( ...)
and t.id_2 not in (...)
and t.xxx in
(select MAX(xxx) xxx
from aaa t
where 1 = 1
and t.id_1 in (...)
and t.id_2 not in (...)
GROUP BY t.ID_2)
order by t.xxx desc LIMIT 10
from aaa t
where 1 = 1
and t.id_1 in ( ...)
and t.id_2 not in (...)
and t.xxx in
(select MAX(xxx) xxx
from aaa t
where 1 = 1
and t.id_1 in (...)
and t.id_2 not in (...)
GROUP BY t.ID_2)
order by t.xxx desc LIMIT 10
mysql> show profile cpu,block io for query 4;
+--------------------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000021 | 0.000000 | 0.000000 | 0 | 0 |
| checking query cache for query | 0.000151 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000110 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| Table lock | 0.000077 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000076 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.001712 | 0.002000 | 0.000000 | 0 | 0 |
| preparing | 0.001671 | 0.001999 | 0.000000 | 0 | 0 |
| executing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| Sorting result | 0.000653 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000014 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.001727 | 0.002000 | 0.000000 | 0 | 0 |
| preparing | 0.001676 | 0.002000 | 0.000000 | 0 | 0 |
| Creating tmp table | 0.000030 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| Copying to tmp table | 0.001329 | 0.001000 | 0.000000 | 0 | 0 |
....................................................................................................
| Sorting result | 0.000018 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.000085 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.000030 | 0.000000 | 0.000000 | 0 | 0 |
| end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| removing tmp table | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |
| end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000039 | 0.000000 | 0.000000 | 0 | 0 |
| storing result in query cache | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000002 | 0.000000 | 0.000000 | 0 | 0 |
| cleaning up | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
+--------------------------------+----------+----------+------------+--------------+---------------+
468 rows in set (0.00 sec)
+--------------------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000021 | 0.000000 | 0.000000 | 0 | 0 |
| checking query cache for query | 0.000151 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000110 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| Table lock | 0.000077 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000076 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.001712 | 0.002000 | 0.000000 | 0 | 0 |
| preparing | 0.001671 | 0.001999 | 0.000000 | 0 | 0 |
| executing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| Sorting result | 0.000653 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000014 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.001727 | 0.002000 | 0.000000 | 0 | 0 |
| preparing | 0.001676 | 0.002000 | 0.000000 | 0 | 0 |
| Creating tmp table | 0.000030 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| Copying to tmp table | 0.001329 | 0.001000 | 0.000000 | 0 | 0 |
....................................................................................................
| Sorting result | 0.000018 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.000085 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.000030 | 0.000000 | 0.000000 | 0 | 0 |
| end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| removing tmp table | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |
| end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000039 | 0.000000 | 0.000000 | 0 | 0 |
| storing result in query cache | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000002 | 0.000000 | 0.000000 | 0 | 0 |
| cleaning up | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
+--------------------------------+----------+----------+------------+--------------+---------------+
468 rows in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/758322/viewspace-680170/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql的子查詢MySql
- MySQL子查詢MySql
- MYsql 子查詢MySql
- MySQL聯結查詢和子查詢MySql
- MySQL查詢為什麼沒走索引?這篇文章帶你全面解析MySql索引
- 沒想到,歷史漏洞造成的安全事件這麼多?事件
- MySQL之連線查詢和子查詢MySql
- 【MySQL】檢視&子查詢MySql
- MySQL 相關子查詢MySql
- [MYSQL -14]使用子查詢MySql
- 【MySQL】子查詢之一MySql
- 沒想到,這麼簡單的執行緒池用法,深藏這麼多坑!執行緒
- MySQL子查詢的優化薦MySql優化
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- 子查詢-表子查詢
- MySQL全面瓦解11:子查詢和組合查詢MySql
- MySQL資料庫基礎——多表查詢:子查詢MySql資料庫
- MYSQL學習筆記26: 多表查詢|子查詢MySql筆記
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- AI在用| 萬萬沒想到,科技論文還能這麼讀AI
- SQL查詢的:子查詢和多表查詢SQL
- 生產庫中遇到mysql的子查詢MySql
- mysql子查詢的缺陷以及5.6的優化MySql優化
- MySQL索引憑什麼能讓查詢效率提高這麼多?MySql索引
- 告訴你MySQL主鍵查詢為什麼這麼快MySql
- 萬萬沒想到——flutter這樣外接紋理Flutter
- 關於mysql 子查詢中 使用 limitMySqlMIT
- Mysql中的巢狀子查詢問題QSBSMySql巢狀
- MySql中limit子查詢的解決辦法MySqlMIT
- EXCEL你不知道的三個秘密:沒想到隱藏的這麼深Excel
- 複雜查詢—子查詢
- 沒想到吧!Google 排名第一的程式語言,為什麼會這麼火?Go
- 06 萬萬沒想到,Github還有這些功能Github
- MySQL查詢資料庫中沒有主鍵的表MySql資料庫
- 什麼是SQL 語句中相關子查詢與非相關子查詢SQL
- mysql求交集:UNION ALL合併查詢,inner join內連線查詢,IN/EXISTS子查詢MySql
- mysql sql_safe_updates 不支援子查詢的更新。MySql
- MySQL中in(獨立子查詢)的執行計劃MySql