成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

盤點開發中那些常用的MySQL優化

數據庫 MySQL
很多時候數據庫的性能是由于不合適(是指效率不高,可能會導致鎖表等)的SQL語句造成,本篇博文只是介紹簡單的SQL優化

 1、大批量插入數據優化

(1)對于MyISAM存儲引擎的表,可以使用:DISABLE KEYS 和 ENABLE KEYS 用來打開或者關閉 MyISAM 表非唯一索引的更新。 

  1. ALTER TABLE tbl_name DISABLE KEYS;  
  2. loading the data  
  3. ALTER TABLE tbl_name ENABLE KEYS; 

(2)對于InnoDB引擎,有以下幾種優化措施:

① 導入的數據按照主鍵的順序保存:這是因為InnoDB引擎表示按照主鍵順序保存的,如果能將插入的數據提前按照排序好自然能省去很多時間。

比如bulk_insert.txt文件是以表user主鍵的順序存儲的,導入的時間為15.23秒 

  1. mysql> load data infile 'mysql/bulk_insert.txt' into table user;  
  2. Query OK, 126732 rows affected (15.23 sec)  
  3. Records: 126732 Deleted: 0 Skipped: 0 Warnings: 0 

沒有按照主鍵排序的話,時間為:26.54秒 

  1. mysql> load data infile 'mysql/bulk_insert.txt' into table user;  
  2. Query OK, 126732 rows affected (26.54 sec)  
  3. Records: 126732 Deleted: 0 Skipped: 0 Warnings: 0 

② 導入數據前執行SET UNIQUE_CHECKS=0,關閉唯一性校驗,帶導入之后再打開設置為1:校驗會消耗時間,在數據量大的情況下需要考慮。

③ 導入前設置SET AUTOCOMMIT=0,關閉自動提交,導入后結束再設置為1:這是因為自動提交會消耗部分時間與資源,雖然消耗不是很大,但是在數據量大的情況下還是得考慮。

2、INSERT的優化

(1)盡量使用多個值表的 INSERT 語句,這種方式將大大縮減客戶端與數據庫之間的連接、關閉等消耗。(同一客戶的情況下),即:

  1. INSERT INTO tablename values(1,2),(1,3),(1,4) 

實驗:插入8條數據到user表中(使用navicat客戶端工具) 

  1. insert into user values(1,'test',replace(uuid(),'-',''));  
  2. insert into user values(2,'test',replace(uuid(),'-',''));  
  3. insert into user values(3,'test',replace(uuid(),'-',''));  
  4. insert into user values(4,'test',replace(uuid(),'-',''));  
  5. insert into user values(5,'test',replace(uuid(),'-',''));  
  6. insert into user values(6,'test',replace(uuid(),'-',''));  
  7. insert into user values(7,'test',replace(uuid(),'-','')); 
  8. insert into user values(8,'test',replace(uuid(),'-','')); 

得到反饋: 

  1. [SQL] insert into user values(1,'test',replace(uuid(),'-',''));  
  2. 受影響的行: 1  
  3. 時間: 0.033s  
  4. [SQL]   
  5. insert into user values(2,'test',replace(uuid(),'-',''));  
  6. 受影響的行: 1  
  7. 時間: 0.034s  
  8. [SQL]   
  9. insert into user values(3,'test',replace(uuid(),'-',''));  
  10. 受影響的行: 1  
  11. 時間: 0.056s  
  12. [SQL]   
  13. insert into user values(4,'test',replace(uuid(),'-',''));  
  14. 受影響的行: 1  
  15. 時間: 0.008s  
  16. [SQL]   
  17. insert into user values(5,'test',replace(uuid(),'-',''));  
  18. 受影響的行: 1  
  19. 時間: 0.008s  
  20. [SQL]   
  21. insert into user values(6,'test',replace(uuid(),'-',''));  
  22. 受影響的行: 1  
  23. 時間: 0.024s  
  24. [SQL]   
  25. insert into user values(7,'test',replace(uuid(),'-',''));  
  26. 受影響的行: 1  
  27. 時間: 0.004s  
  28. [SQL]   
  29. insert into user values(8,'test',replace(uuid(),'-',''));  
  30. 受影響的行: 1  
  31. 時間: 0.004s 

總共的時間為0.171秒,接下來使用多值表形式: 

  1. insert into user values  
  2. (9,'test',replace(uuid(),'-','')),  
  3. (10,'test',replace(uuid(),'-','')),  
  4. (11,'test',replace(uuid(),'-','')),  
  5. (12,'test',replace(uuid(),'-','')),  
  6. (13,'test',replace(uuid(),'-','')),  
  7. (14,'test',replace(uuid(),'-','')),  
  8. (15,'test',replace(uuid(),'-','')),  
  9. (16,'test',replace(uuid(),'-','')); 

得到反饋: 

  1. [SQL] insert into user values  
  2. (9,'test',replace(uuid(),'-','')),  
  3. (10,'test',replace(uuid(),'-','')),  
  4. (11,'test',replace(uuid(),'-','')),  
  5. (12,'test',replace(uuid(),'-','')),  
  6. (13,'test',replace(uuid(),'-','')),  
  7. (14,'test',replace(uuid(),'-','')),  
  8. (15,'test',replace(uuid(),'-','')),  
  9. (16,'test',replace(uuid(),'-',''));  
  10. 受影響的行: 8  
  11. 時間: 0.038s 

得到時間為0.038,這樣一來可以很明顯節約時間優化SQL

(2)如果在不同客戶端插入很多行,可使用INSERT DELAYED語句得到更高的速度,DELLAYED含義是讓INSERT語句馬上執行,其實數據都被放在內存的隊列中。并沒有真正寫入磁盤。LOW_PRIORITY剛好相反。

(3)將索引文件和數據文件分在不同的磁盤上存放(InnoDB引擎是在同一個表空間的)。

(4)如果批量插入,則可以增加bluk_insert_buffer_size變量值提供速度(只對MyISAM有用)

(5)當從一個文本文件裝載一個表時,使用LOAD DATA INFILE,通常比INSERT語句快20倍。

3、GROUP BY的優化

在默認情況下,MySQL中的GROUP BY語句會對其后出現的字段進行默認排序(非主鍵情況),就好比我們使用ORDER BY col1,col2,col3…所以我們在后面跟上具有相同列(與GROUP BY后出現的col1,col2,col3…相同)ORDER BY子句并沒有影響該SQL的實際執行性能。

那么就會有這樣的情況出現,我們對查詢到的結果是否已經排序不在乎時,可以使用ORDER BY NULL禁止排序達到優化目的。下面使用EXPLAIN命令分析SQL。Java知音公眾號內回復“面試題聚合”,送你一份面試題寶典

在user_1中執行select id, sum(money) form user_1 group by name時,會默認排序(注意group by后的column是非index才會體現group by的排序,如果是primary key,那之前說過了InnoDB默認是按照主鍵index排好序的) 

  1. mysql> select*from user_1;  
  2. +----+----------+-------+  
  3. | id | name     | money |  
  4. +----+----------+-------+  
  5. |  1 | Zhangsan |    32 |  
  6. |  2 | Lisi     |    65 |  
  7. |  3 | Wangwu   |    44 |  
  8. |  4 | Lijian   |   100 |  
  9. +----+----------+-------+  
  10. 4 rows in set 

不禁止排序,即不使用ORDER BY NULL時:有明顯的Using filesort。

當使用ORDER BY NULL禁止排序后,Using filesort不存在

4、ORDER BY 的優化  

MySQL可以使用一個索引來滿足ORDER BY 子句的排序,而不需要額外的排序,但是需要滿足以下幾個條件:

(1)WHERE 條件和OREDR BY 使用相同的索引:即key_part1與key_part2是復合索引,where中使用復合索引中的key_part1

  1. SELECT*FROM user WHERE key_part1=1 ORDER BY key_part1 DESC, key_part2 DESC; 

(2)而且ORDER BY順序和索引順序相同:

  1. SELECT*FROM user ORDER BY key_part1, key_part2; 

(3)并且要么都是升序要么都是降序:

  1. SELECT*FROM user ORDER BY key_part1 DESC, key_part2 DESC; 

但以下幾種情況則不使用索引:

(1)ORDER BY中混合ASC和DESC: 

  1. SELECT*FROM user ORDER BY key_part1 DESC, key_part2 ASC; 

(2)查詢行的關鍵字與ORDER BY所使用的不相同,即WHERE 后的字段與ORDER BY 后的字段是不一樣的 

  1. SELECT*FROM user WHERE key2 = ‘xxx’ ORDER BY key1; 

(3)ORDER BY對不同的關鍵字使用,即ORDER BY后的關鍵字不相同 

  1. SELECT*FROM user ORDER BY key1, key2; 

5、OR的優化

當MySQL使用OR查詢時,如果要利用索引的話,必須每個條件列都使獨立索引,而不是復合索引(多列索引),才能保證使用到查詢的時候使用到索引。

比如我們新建一張用戶信息表user_info 

  1. mysql> select*from user_info;  
  2. +---------+--------+----------+-----------+  
  3. | user_id | idcard | name     | address    |  
  4. +---------+--------+----------+-----------+  
  5. |       1 | 111111 | Zhangsan | Kunming   |  
  6. |       2 | 222222 | Lisi     | Beijing   |  
  7. |       3 | 333333 | Wangwu   | Shanghai  |  
  8. |       4 | 444444 | Lijian   | Guangzhou |  
  9. +---------+--------+----------+-----------+  
  10. 4 rows in set 

之后創建ind_name_id(user_id, name)復合索引、id_index(id_index)獨立索引,idcard主鍵索引三個索引。 

  1. mysql> show index from user_info;  
  2. +-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  3. | Table     | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
  4. +-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  5. | user_info |          0 | PRIMARY     |            1 | idcard      | A         |           4 | NULL     | NULL   |      | BTREE      |         |               | 
  6. | user_info |          1 | ind_name_id |            1 | user_id     | A         |           4 | NULL     | NULL   |      | BTREE      |         |               | 
  7. | user_info |          1 | ind_name_id |            2 | name        | A         |           4 | NULL     | NULL   | YES  | BTREE      |         |               | 
  8. | user_info |          1 | id_index    |            1 | user_id     | A         |           4 | NULL     | NULL   |      | BTREE      |         |               | 
  9. +-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  10. 4 rows in set 

測試一:OR連接兩個有單獨索引的字段,整個SQL查詢才會用到索引(index_merge),并且我們知道OR實際上是把每個結果最后UNION一起的。 

  1. mysql> explain select*from user_info where user_id=1 or idcard='222222' 
  2. +----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+ 
  3. | id | select_type | table     | partitions | type        | possible_keys                | key                 | key_len | ref  | rows | filtered | Extra                                              | 
  4. +----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+ 
  5. |  1 | SIMPLE      | user_info | NULL       | index_merge | PRIMARY,ind_name_id,id_index | ind_name_id,PRIMARY | 4,62    | NULL |    2 |      100 | Using sort_union(ind_name_id,PRIMARY); Using where | 
  6. +----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+ 
  7. 1 row in set 

 測試二:OR使用復合索引的字段name,與沒有索引的address,整個SQL都是ALL全表掃描的 

  1. mysql> explain select*from user_info where name='Zhangsan' or address='Beijing' 
  2. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  3. | id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |  
  4. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  5. |  1 | SIMPLE      | user_info | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |    43.75 | Using where |  
  6. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  7. 1 row in set 

交換OR位置并且使用另外的復合索引的列,也是ALL全表掃描: 

  1. mysql> explain select*from user_info where address='Beijing' or user_id=1 
  2. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  3. | id | select_type | table     | partitions | type | possible_keys        | key  | key_len | ref  | rows | filtered | Extra       |  
  4. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+ 
  5. |  1 | SIMPLE      | user_info | NULL       | ALL  | ind_name_id,id_index | NULL | NULL    | NULL |    4 |    43.75 | Using where |  
  6. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  7. 1 row in set 

6、優化嵌套查詢

使用嵌套查詢有時候可以使用更有效的JOIN連接代替,這是因為MySQL中不需要在內存中創建臨時表完成SELECT子查詢與主查詢兩部分查詢工作。但是并不是所有的時候都成立,最好是在on關鍵字后面的列有索引的話,效果會更好!

比如在表major中major_id是有索引的: 

  1. select * from student u left join major m on u.major_id=m.major_id where m.major_id is null; 

而通過嵌套查詢時,在內存中創建臨時表完成SELECT子查詢與主查詢兩部分查詢工作,會有一定的消耗 

  1. select * from student u where major_id not in (select major_id from major); 

7、使用SQL提示

SQL提示(SQL HINT)是優化數據庫的一個重要手段,就是往SQL語句中加入一些人為的提示來達到優化目的。下面是一些常用的SQL提示:

(1)USE INDEX:使用USE INDEX是希望MySQL去參考索引列表,就可以讓MySQL不需要考慮其他可用索引,其實也就是possible_keys屬性下參考的索引值 

  1. mysql> explain select* from user_info use index(id_index,ind_name_id) where user_id>0;  
  2. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  3. | id | select_type | table     | partitions | type | possible_keys        | key  | key_len | ref  | rows | filtered | Extra       |  
  4. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  5. |  1 | SIMPLE      | user_info | NULL       | ALL  | ind_name_id,id_index | NULL | NULL    | NULL |    4 |      100 | Using where |  
  6. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  7. 1 row in set  
  8. mysql> explain select* from user_info use index(id_index) where user_id>0;  
  9. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  10. | id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |  
  11. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  12. |  1 | SIMPLE      | user_info | NULL       | ALL  | id_index      | NULL | NULL    | NULL |    4 |      100 | Using where |  
  13. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  14. 1 row in set 

(2)IGNORE INDEX忽略索引

我們使用user_id判斷,用不到其他索引時,可以忽略索引。即與USE INDEX相反,從possible_keys中減去不需要的索引,但是實際環境中很少使用。 

  1. mysql> explain select* from user_info ignore index(primary,ind_name_id,id_index) where user_id>0;  
  2. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  3. | id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |  
  4. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  5. |  1 | SIMPLE      | user_info | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |    33.33 | Using where |  
  6. +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+  
  7. 1 row in set 

(3)FORCE INDEX強制索引

比如where user_id > 0,但是user_id在表中都是大于0的,自然就會進行ALL全表搜索,但是使用FORCE INDEX雖然執行效率不是最高(where user_id > 0條件決定的)但MySQL還是使用索引。 

  1. mysql> explain select* from user_info where user_id>0;  
  2. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  3. | id | select_type | table     | partitions | type | possible_keys        | key  | key_len | ref  | rows | filtered | Extra       |  
  4. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  5. |  1 | SIMPLE      | user_info | NULL       | ALL  | ind_name_id,id_index | NULL | NULL    | NULL |    4 |      100 | Using where |  
  6. +----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+  
  7. 1 row in set 

之后強制使用獨立索引id_index(user_id): 

  1. mysql> explain select* from user_info force index(id_index) where user_id>0;  
  2. +----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+  
  3. | id | select_type | table     | partitions | type  | possible_keys | key      | key_len | ref  | rows | filtered | Extra                 |  
  4. +----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+  
  5. |  1 | SIMPLE      | user_info | NULL       | range | id_index      | id_index | 4       | NULL |    4 |      100 | Using index condition |  
  6. +----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+  
  7. 1 row in set 

總結

(1)很多時候數據庫的性能是由于不合適(是指效率不高,可能會導致鎖表等)的SQL語句造成,本篇博文只是介紹簡單的SQL優化

(2)其中有些優化在真正開發中是用不到的,但是一旦出問題性能下降的時候需要去一一分析。 

 

責任編輯:龐桂玉 來源: Java知音
相關推薦

2022-03-27 20:52:41

Chrome插件開發

2021-09-04 07:56:44

Pythonos模塊

2015-09-08 14:42:17

Android性能優化

2021-08-30 10:25:48

JavaScript進階操作前端

2021-08-26 10:25:04

JavaScript進階操作 前端

2021-10-09 07:10:31

JavaScript對象Python

2023-03-17 10:03:51

服務器編輯器vscode

2023-06-01 07:48:03

Solidjsx??React?

2021-08-11 21:46:47

MySQL索引join

2012-03-22 10:26:17

開源云計算

2017-04-20 14:58:16

2014-12-17 14:41:21

云計算互聯網混合云

2022-01-11 06:53:23

IPO開發容器

2020-08-12 15:00:55

MYSQL優化數據庫

2022-03-22 07:38:00

SQL語句MySQL

2017-06-27 14:48:51

開發設計程序員

2022-06-20 05:40:25

數據庫MySQL查詢

2015-04-13 17:39:11

移動IM開發

2013-08-28 10:18:48

2023-01-31 16:35:34

JavaScript測試框架
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久一区二区三区 | 日韩中文字幕视频在线观看 | www.久| 色综合视频 | 91网站在线看 | 毛片网站免费观看 | 欧美理论在线观看 | 欧美一区二区大片 | 国产精品久久久久久久久久久久 | 久久黄网| 久久看片| 在线日韩欧美 | 人人人人干 | 亚洲人成人一区二区在线观看 | 欧美日韩久久精品 | 一级黄色毛片a | 蜜桃av人人夜夜澡人人爽 | 欧美精品一二区 | 欧美视频在线观看 | 婷婷午夜天 | 亚洲色图第一页 | 天天干天天操天天看 | 亚洲精品电影网在线观看 | 国产特一级黄色片 | 青青久久 | 黄色a级一级片 | 99热碰 | 在线视频一区二区三区 | 中文字幕 在线观看 | 日本一区二区三区视频在线 | 欧美成人精品一区 | 国产精品污www一区二区三区 | 亚洲欧美激情精品一区二区 | 日韩伦理电影免费在线观看 | www网站在线观看 | 国产精品久久久久久婷婷天堂 | 四虎影视一区二区 | 韩三级在线观看 | 91精品无人区卡一卡二卡三 | 波多野吉衣在线播放 | 日韩一级精品视频在线观看 |