Oracle/MySQL/PostgreSQL 簡單查詢的效能對比
maohaiqing0304發表於2016-04-02
註釋:
30w+6w+2個100條表 left join where 多對多關係...求count(1) ,mysql資料庫 執行4514s,posgresql執行206s,oracle執行45s.
mysql 只有nest loop;
pg、oracle 有hash join 哦....
詳情如下:
mysql 只有nest loop;
pg、oracle 有hash join 哦....
詳情如下:
錶行數
SQL> SELECT table_name,num_rows FROM USER_TABLES WHERE upper(TABLE_NAME) IN ('BRAND','COLOR_INFO','SHOP_BRAND','ITEM');
TABLE_NAME NUM_ROWS
------------------------------ ----------
shop_brand 62524
color_info 183
brand 143
item 309674
SQL>
TABLE_NAME NUM_ROWS
------------------------------ ----------
shop_brand 62524
color_info 183
brand 143
item 309674
SQL>
Oracle 部分
執行資訊
SQL> set timing on
SELECT count(1) AS s
FROM "item"
LEFT JOIN "color_info"
ON "color_info"."color_no" = "item"."color_no"
LEFT JOIN "brand"
ON "brand"."brand_no" = "item"."brand_no"
LEFT JOIN "shop_brand"
ON "shop_brand"."brand_no" = "item"SQL> 2 3 4 5 6 7 8 ."brand_no"
WHERE 1 = 1; 9
SELECT count(1) AS s
FROM "item"
LEFT JOIN "color_info"
ON "color_info"."color_no" = "item"."color_no"
LEFT JOIN "brand"
ON "brand"."brand_no" = "item"."brand_no"
LEFT JOIN "shop_brand"
ON "shop_brand"."brand_no" = "item"SQL> 2 3 4 5 6 7 8 ."brand_no"
WHERE 1 = 1; 9
S
----------
900302146
Elapsed: 00:00:45.94
SQL>
----------
900302146
Elapsed: 00:00:45.94
SQL>
執行計劃
Plan hash value: 3456201856------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 185 | | 10888 (66)| 00:02:11 |
| 1 | SORT AGGREGATE | | 1 | 185 | | | |
|* 2 | HASH JOIN RIGHT OUTER | | 915M| 157G| 2992K| 10888 (66)| 00:02:11 |
| 3 | INDEX FAST FULL SCAN | A | 62524 | 2259K| | 295 (1)| 00:00:04 |
|* 4 | HASH JOIN RIGHT OUTER | | 309K| 43M| | 1013 (1)| 00:00:13 |
| 5 | INDEX FAST FULL SCAN | UK_COLOR_NO | 183 | 6771 | | 2 (0)| 00:00:01 |
|* 6 | HASH JOIN RIGHT OUTER| | 309K| 32M| | 1008 (1)| 00:00:13 |
| 7 | INDEX FULL SCAN | UK_BRAND_NO | 143 | 5291 | | 1 (0)| 00:00:01 |
| 8 | INDEX FAST FULL SCAN| IX_BRAND_COLOR_NO | 309K| 21M| | 1005 (1)| 00:00:13 |
------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("shop_brand"."brand_no"(+)="item"."brand_no")
4 - access("color_info"."color_no"(+)="item"."color_no")
6 - access("brand"."brand_no"(+)="item"."brand_no")
PostgreSQL 部分
執行資訊:
postgres=# SELECT count(1) AS s
postgres-# FROM item
postgres-# LEFT JOIN color_info ON color_info.color_no = item.color_no
postgres-# LEFT JOIN brand ON brand.brand_no = item.brand_no
postgres-# LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
postgres-# WHERE 1 = 1;
s
-----------
postgres=#
postgres-# FROM item
postgres-# LEFT JOIN color_info ON color_info.color_no = item.color_no
postgres-# LEFT JOIN brand ON brand.brand_no = item.brand_no
postgres-# LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
postgres-# WHERE 1 = 1;
s
-----------
900302146
Time: 206415.142 mspostgres=#
執行計劃:
postgres=# EXPLAIN
SELECT count(1) AS s
FROM item
LEFT JOIN color_info ON color_info.color_no = item.color_no
LEFT JOIN brand ON brand.brand_no = item.brand_no
LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
WHERE 1 = 1;
+-------------------------------------------------------------------------------------------+
| QUERY PLAN |
+-------------------------------------------------------------------------------------------+
| Aggregate (cost=13514243.57..13514243.58 rows=1 width=0) |
| -> Hash Left Join (cost=2602.12..11234175.66 rows=912027167 width=0) |
| Hash Cond: (item.brand_no = shop_brand.brand_no) |
| -> Hash Left Join (cost=13.33..33128.01 rows=328178 width=19) |
| Hash Cond: (item.brand_no = brand.brand_no) |
| -> Hash Left Join (cost=7.12..28609.34 rows=328178 width=19) |
| Hash Cond: (item.color_no = color_info.color_no) |
| -> Seq Scan on item (cost=0.00..24089.78 rows=328178 width=38) |
| -> Hash (cost=4.83..4.83 rows=183 width=19) |
| -> Seq Scan on color_info (cost=0.00..4.83 rows=183 width=19) |
| -> Hash (cost=4.43..4.43 rows=143 width=19) |
| -> Seq Scan on brand (cost=0.00..4.43 rows=143 width=19) |
| -> Hash (cost=1807.24..1807.24 rows=62524 width=19) |
| -> Seq Scan on shop_brand (cost=0.00..1807.24 rows=62524 width=19) |
+-------------------------------------------------------------------------------------------+
14 rows in set
postgres=#
SELECT count(1) AS s
FROM item
LEFT JOIN color_info ON color_info.color_no = item.color_no
LEFT JOIN brand ON brand.brand_no = item.brand_no
LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
WHERE 1 = 1;
+-------------------------------------------------------------------------------------------+
| QUERY PLAN |
+-------------------------------------------------------------------------------------------+
| Aggregate (cost=13514243.57..13514243.58 rows=1 width=0) |
| -> Hash Left Join (cost=2602.12..11234175.66 rows=912027167 width=0) |
| Hash Cond: (item.brand_no = shop_brand.brand_no) |
| -> Hash Left Join (cost=13.33..33128.01 rows=328178 width=19) |
| Hash Cond: (item.brand_no = brand.brand_no) |
| -> Hash Left Join (cost=7.12..28609.34 rows=328178 width=19) |
| Hash Cond: (item.color_no = color_info.color_no) |
| -> Seq Scan on item (cost=0.00..24089.78 rows=328178 width=38) |
| -> Hash (cost=4.83..4.83 rows=183 width=19) |
| -> Seq Scan on color_info (cost=0.00..4.83 rows=183 width=19) |
| -> Hash (cost=4.43..4.43 rows=143 width=19) |
| -> Seq Scan on brand (cost=0.00..4.43 rows=143 width=19) |
| -> Hash (cost=1807.24..1807.24 rows=62524 width=19) |
| -> Seq Scan on shop_brand (cost=0.00..1807.24 rows=62524 width=19) |
+-------------------------------------------------------------------------------------------+
14 rows in set
postgres=#
MySQL 部分
執行資訊
mysql> set profiling=1;
mysql> show profiles; #檢視sql執行時間
---------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-------------+----------------------------------------------------------- |
| 6 | 4514.459468 | SELECT count(1) AS s
FROM item
LEFT JOIN color_info ON color_info.color_no = item.color_no
LEFT JOIN brand ON brand.brand_no = item.brand_no
LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
WHERE 1 = 1 |
+----------+-------------+----------------------------------------------------------------
---------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-------------+----------------------------------------------------------- |
| 6 | 4514.459468 | SELECT count(1) AS s
FROM item
LEFT JOIN color_info ON color_info.color_no = item.color_no
LEFT JOIN brand ON brand.brand_no = item.brand_no
LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
WHERE 1 = 1 |
+----------+-------------+----------------------------------------------------------------
mysql>
執行計劃:
mysql> EXPLAIN
SELECT count(1) AS s
FROM item
LEFT JOIN color_info ON color_info.color_no = item.color_no
LEFT JOIN brand ON brand.brand_no = item.brand_no
LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
WHERE 1 = 1;
+----+-------------+------------+--------+---------------+---------------------+---------+-----------------------+--------+-----------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------------------+---------+-----------------------+--------+-----------------------------------------------------------------+
| 1 | SIMPLE | item | index | NULL | ix_brand_color_no_1 | 109 | NULL | 322260 | Using index |
| 1 | SIMPLE | color_info | eq_ref | uk_color_no | uk_color_no | 54 | lottery.item.color_no | 1 | Using index |
| 1 | SIMPLE | brand | eq_ref | uk_brand_no | uk_brand_no | 54 | lottery.item.brand_no | 1 | Using index |
| 1 | SIMPLE | shop_brand | index | NULL | uk_store_brand | 165 | NULL | 61992 | Using where; Using index; Using join buffer (Block Nested Loop) |
+----+-------------+------------+--------+---------------+---------------------+---------+-----------------------+--------+-----------------------------------------------------------------+
4 rows in set
mysql>
SELECT count(1) AS s
FROM item
LEFT JOIN color_info ON color_info.color_no = item.color_no
LEFT JOIN brand ON brand.brand_no = item.brand_no
LEFT JOIN shop_brand ON shop_brand.brand_no = item.brand_no
WHERE 1 = 1;
+----+-------------+------------+--------+---------------+---------------------+---------+-----------------------+--------+-----------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------------------+---------+-----------------------+--------+-----------------------------------------------------------------+
| 1 | SIMPLE | item | index | NULL | ix_brand_color_no_1 | 109 | NULL | 322260 | Using index |
| 1 | SIMPLE | color_info | eq_ref | uk_color_no | uk_color_no | 54 | lottery.item.color_no | 1 | Using index |
| 1 | SIMPLE | brand | eq_ref | uk_brand_no | uk_brand_no | 54 | lottery.item.brand_no | 1 | Using index |
| 1 | SIMPLE | shop_brand | index | NULL | uk_store_brand | 165 | NULL | 61992 | Using where; Using index; Using join buffer (Block Nested Loop) |
+----+-------------+------------+--------+---------------+---------------------+---------+-----------------------+--------+-----------------------------------------------------------------+
4 rows in set
mysql>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28602568/viewspace-2074090/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 簡單的mysql查詢2016-04-13MySql
- 對 MySQL 慢查詢日誌的簡單分析2020-07-08MySql
- MySQL - 資料查詢 - 簡單查詢2020-12-27MySql
- 對比SQL中簡單巢狀查詢與非巢狀查詢CF2022-03-21SQL巢狀
- MySQL的簡單查詢語句2019-01-15MySql
- MySQL之資料的簡單查詢2017-05-11MySql
- mysql查詢快取簡單使用2021-03-06MySql快取
- 簡單的查詢2020-11-08
- 簡單對比MySQL和Oracle中的一個sql解析細節2015-05-18MySqlOracle
- Oracle 和 mysql 的一些簡單命令對比參照(轉)2007-08-16OracleMySql
- 【.NET 6】使用EF Core 訪問Oracle+Mysql+PostgreSQL並進行簡單增改操作與效能比較2022-04-03OracleMySql
- nginx與lighttpd效能簡單對比薦2011-04-27Nginxhttpd
- 我對Postgresql遞迴查詢的理解2016-08-02SQL遞迴
- HTTPS SPDY和 HTTP/2效能的簡單對比2015-01-21HTTP
- SQL查詢月、天、周、年(MySql的例項對比)2009-02-19MySql
- 查詢oracle效能SQL2014-04-01OracleSQL
- MySQL 查詢結果以百分比顯示簡單實現2021-09-09MySql
- php mysql 一個查詢優化的簡單例子2016-04-03PHPMySql優化單例
- TDengine 和 InfluxDB 查詢效能對比測試報告2022-03-29UX測試報告
- MySQL單表查詢2019-07-08MySql
- MySQL 單表查詢2020-09-01MySql
- Mybatis簡單查詢2021-09-15MyBatis
- 最新+最簡單Oracle分頁查詢法2009-10-14Oracle
- PostgreSQL初體驗及其與MySQL的對比2024-03-02MySql
- MySQL簡單實現多欄位模糊查詢2019-03-15MySql
- 《MySQL 入門教程》第 07 篇 簡單查詢2022-01-29MySql
- 簡單的查詢語法2020-12-11
- ElasticSearch中的簡單查詢2016-11-22Elasticsearch
- Laravel 關聯查詢 ——一對一 簡單例子2018-11-06Laravel單例
- mysql timestamp比較查詢2021-12-13MySql
- 五個容易錯過的 PostgreSQL 查詢效能瓶頸2022-01-21SQL
- MySQL 查詢效能分析之 Explain2020-01-06MySqlAI
- MySQL中使用or、in與union all在查詢命令下的效率對比2021-09-09MySql
- PostgreSQL和oracle表分割槽對比2013-05-02SQLOracle
- Mysql-基本練習(09-刪除單表記錄、查詢指定列資料、列的別名、簡單單表條件查詢、簡單分組查詢)2018-05-16MySql
- 【效能】Oracle表並行對邏輯遷移後查詢效能的影響2020-04-02Oracle並行
- PostgreSQL TPROC-C基準測試:PostgreSQL 12與PostgreSQL 13效能對比2020-12-08SQL
- Laravel 5 關聯查詢 —— N 對 N 簡單例子2018-11-06Laravel單例