mysql综合查询索引优化_MySQL数据库SQL优化之确定问题使用索引提高查询效率
在EXPLAIN分析SQL中,已經可以確定是對客戶表customet的全表掃描導致效率的不理想,那么對客戶表customer的email字段創建索引,具體如下:
mysql> create index idx_email on customer(email);
創建索引后,再看一下這條語句的執行計劃,具體如下:
mysql> explain select sum(amount) from customer a,payment b where 1=1 and a.customer_id = b.customer_id and email ='JANE.BENNETT@sakiacustomer.org';
+----+-------------+-------+------------+------+--------------------+--------------------+---------+----------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+----------------------+------+----------+-------------+
| 1 | SIMPLE | a | NULL | ref | PRIMARY,idx_email | idx_email | 153 | const | 1 | 100.00 | Using index |
| 1 | SIMPLE | b | NULL | ref | idx_fk_customer_id | idx_fk_customer_id | 2 | sakila.a.customer_id | 26 | 100.00 | NULL |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+----------------------+------+----------+-------------+
2 rows in set, 1 warning (0.02 sec)
通過分析SQL的rows的結果可以看出,建立索引后對customer 需要掃的行數明顯減少(從583行減少到1行),可見索引的使用大大提高數據庫的訪問速度,尤其再表很龐大的時候,這種優勢更為明顯。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的mysql综合查询索引优化_MySQL数据库SQL优化之确定问题使用索引提高查询效率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【如何选择到合适的天线-天线参数讲解 】
- 下一篇: 地图采集商家,附近商家,最新企业信息采集