激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - Mysql數據庫之索引優化

Mysql數據庫之索引優化

2020-06-03 15:29鯤鵬之翅 Mysql

MySQL憑借著出色的性能、低廉的成本、豐富的資源,已經成為絕大多數互聯網公司的首選關系型數據庫。本文給大家介紹mysql數據庫之索引優化,感興趣的朋友一起學習吧

MySQL憑借著出色的性能、低廉的成本、豐富的資源,已經成為絕大多數互聯網公司的首選關系型數據庫。雖然性能出色,但所謂“好馬配好鞍”,如何能夠更好的使用它,已經成為開發工程師的必修課,我們經常會從職位描述上看到諸如“精通MySQL”、“SQL語句優化”、“了解數據庫原理”等要求。我們知道一般的應用系統,讀寫比例在10:1左右,而且插入操作和一般的更新操作很少出現性能問題,遇到最多的,也是最容易出問題的,還是一些復雜的查詢操作,所以查詢語句的優化顯然是重中之重。

問題:cpu負載過高,達到36。

Mysql數據庫之索引優化

現象:通過mysqladmin -uroot -p processlist 查看到大量如下信息:

?
1
Sending data select * from `rep_corp_vehicle_online_count` where corp_id = 48 and vehicle_id = 10017543

根據以上的可能是表rep_corp_vehicle_online_count的問題 做出如下測試:

查看表結構:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> desc rep_corp_vehicle_online_count;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| corp_id | int(11) | NO | | NULL | |
| vehicle_id | int(11) | NO | | NULL | |
| online_day | varchar(20) | NO | | NULL | |
| loc_total | int(11) | NO | | NULL | |
| create_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

查看索引,只有主鍵索引:

?
1
2
3
4
5
6
7
mysql> show index from rep_corp_vehicle_online_count;
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| rep_corp_vehicle_online_count | 0 | PRIMARY | 1 | id | A | 1247259 | NULL | NULL | | BTREE | | |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

代碼執行情況:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
mysql>explain select * from rep_corp_vehicle_online_count where corp_id = 79 and vehicle_id = 10016911 and online_day = '2016-03-29'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: rep_corp_vehicle_online_count
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1248495
Extra: Using where
1 row in set (0.00 sec)

表數據分析情況,重復數據很多:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mysql> select count(distinct corp_id) from rep_corp_vehicle_online_count;
+-------------------------+
| count(distinct corp_id) |
+-------------------------+
| 18 |
+-------------------------+
1 row in set (0.63 sec)
mysql> select count(corp_id) from rep_corp_vehicle_online_count;
+----------------+
| count(corp_id) |
+----------------+
| 1239573 |
+----------------+
1 row in set (0.00 sec)
mysql> select count(distinct vehicle_id) from rep_corp_vehicle_online_count;
+----------------------------+
| count(distinct vehicle_id) |
+----------------------------+
| 2580 |
+----------------------------+
1 row in set (1.03 sec)
mysql>explain select count(vehicle_id) from rep_corp_vehicle_online_count;
+-------------------+
| count(vehicle_id) |
+-------------------+
| 1239911 |
+-------------------+
1 row in set (0.00 sec)

最后處理,創建索引:

?
1
2
3
4
5
6
7
8
9
10
11
12
mysql> create index r_c_v on rep_corp_vehicle_online_count(corp_id,vehicle_id);
Query OK, 1487993 rows affected (6.09 sec)
Records: 1487993 Duplicates: 0 Warnings: 0
mysql> show index from rep_corp_vehicle_online_count;
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| rep_corp_vehicle_online_count | 0 | PRIMARY | 1 | id | A | 1490176 | NULL | NULL | | BTREE | | |
| rep_corp_vehicle_online_count | 1 | r_c_v | 1 | corp_id | A | 18 | NULL | NULL | | BTREE | | |
| rep_corp_vehicle_online_count | 1 | r_c_v | 2 | vehicle_id | A | 2596 | NULL | NULL | | BTREE | | |
+-------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

添加索引過后負載降低到了1.73:

Mysql數據庫之索引優化

以上內容是小編給大家介紹的Mysql數據庫之索引優化 ,希望對大家學習有所幫助!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产午夜精品在线 | 国产精品av久久久久久无 | 国产九九 | 日韩在线观看视频一区 | 国产精品久久久久久久久久10秀 | 女人解衣喂奶电影 | 精精国产xxxx视频在线野外 | 久草在线资源福利站 | www中文在线| 精品一区二区三区在线播放 | 亚洲国产精品久久久 | 午夜国产福利 | 午夜天堂在线 | 人人看人人舔 | 羞羞的视频在线 | 国产午夜精品一区二区三区四区 | 国产又白又嫩又紧又爽18p | 黄色av免费电影 | 成年人网站国产 | 国产一级做a爰片在线看 | 一级毛片免费高清视频 | 久久中文免费 | 亚洲成人在线免费 | 日日影视| 国产精品91久久久 | 国产91av视频 | 黄色特级毛片 | jizzzzxxxxx | 国产精品wwww | www.xxx视频 | 黄色av网站在线观看 | 久久精品一区二区三区国产主播 | 国产在线观看免费视频软件 | 国产乱淫av一区二区三区 | 日韩黄色片免费看 | 欧美黑人伦理 | 日本视频网| 成人做爰高潮片免费视频美国 | 免费黄色日韩电影 | h视频免费观看 | 中文字幕在线免费看 |