1. 程式人生 > >MySQL聯合索引VS單列索引

MySQL聯合索引VS單列索引

MySQL聯合索引VS單列索引

以一個一千萬資料量的表格為例

1. 建表建索引

USE foo;
DROP TABLE IF EXISTS tmp;
CREATE TABLE tmp (
  id         INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  school_id  INT UNSIGNED NOT NULL,
  student_id INT UNSIGNED NOT NULL,
  INDEX school_id(school_id),
  INDEX student_id(student_id),
  INDEX school_id_and_student_id(school_id, student_id)
);

2. 插入1000萬條資料

DROP PROCEDURE IF EXISTS tmpproc;
CREATE PROCEDURE tmpproc() BEGIN
  DECLARE i INT UNSIGNED DEFAULT 0;
  DECLARE j INT UNSIGNED DEFAULT 0;
  WHILE i < 100000 DO
    SET i = i + 1;
    SET j = 0;
    START TRANSACTION;
    WHILE j < 100 DO
      INSERT INTO tmp (school_id, student_id) VALUES (i, i * 100 + j);
      SET j = j + 1;
    END WHILE;
    COMMIT;
  END WHILE;
END;
CALL tmpproc();

3. 查詢速度比較

走聯合索引

SELECT *
FROM tmp
WHERE school_id = 88888
  AND student_id = 8888888;

耗時 9ms

走單列索引

SELECT *
FROM tmp
WHERE student_id = 7777777;

耗時 9ms

多執行幾次後,兩個查詢耗時互有出入

4. 結論

在查詢速度上沒什麼區別。至少在1000萬的資料量上很難體現出來。

SQL日誌

[2018-11-22 10:53:50] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> USE foo
[2018-11-22 10:53:50] completed in 25 ms
[2018-11-22 10:53:50] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> DROP TABLE IF EXISTS tmp
[2018-11-22 10:53:51] completed in 682 ms
[2018-11-22 10:53:51] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> CREATE TABLE tmp (
       id         INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
       school_id  INT UNSIGNED NOT NULL,
       student_id INT UNSIGNED NOT NULL,
       INDEX school_id(school_id),
       INDEX student_id(student_id),
       INDEX school_id_and_student_id(school_id, student_id)
     )
[2018-11-22 10:53:51] completed in 45 ms
[2018-11-22 10:53:51] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> DROP PROCEDURE IF EXISTS tmpproc
[2018-11-22 10:53:51] completed in 7 ms
[2018-11-22 10:53:51] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> CREATE PROCEDURE tmpproc() BEGIN
       DECLARE i INT UNSIGNED DEFAULT 0;
       DECLARE j INT UNSIGNED DEFAULT 0;
       WHILE i < 100000 DO
         SET i = i + 1;
         SET j = 0;
         START TRANSACTION;
         WHILE j < 100 DO
           INSERT INTO tmp (school_id, student_id) VALUES (i, i * 100 + j);
           SET j = j + 1;
         END WHILE;
         COMMIT;
       END WHILE;
     END
[2018-11-22 10:53:51] completed in 3 ms
[2018-11-22 10:53:51] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> CALL tmpproc()
[2018-11-22 11:02:24] completed in 8 m 32 s 887 ms
[2018-11-22 11:02:24] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> select database()
[2018-11-22 11:02:24] completed in 14 ms
sql> SELECT *
     FROM tmp
     ORDER BY id DESC
[2018-11-22 11:02:24] 500 rows retrieved starting from 1 in 111 ms (execution: 8 ms, fetching: 103 ms)
[2018-11-22 11:02:24] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> select database()
[2018-11-22 11:02:24] completed in 10 ms
sql> SELECT *
     FROM tmp
     WHERE school_id = 88888
       AND student_id = 8888888
[2018-11-22 11:02:24] 1 row retrieved starting from 1 in 116 ms (execution: 9 ms, fetching: 107 ms)
[2018-11-22 11:02:24] [1287] '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
sql> select database()
[2018-11-22 11:02:24] completed in 7 ms
sql> SELECT *
     FROM tmp
     WHERE student_id = 7777777
[2018-11-22 11:02:24] 1 row retrieved starting from 1 in 87 ms (execution: 9 ms, fetching: 78 ms)