Hàng mysql

Khi bạn muốn thực hiện một câu truy vấn (truy vấn), MySQL Query Optimizer sẽ cố gắng đưa ra một kế hoạch tối ưu nhất cho việc thực hiện truy vấn. Bạn có thể xem thông tin về kế hoạch đó bằng cách thêm lệnh

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 vào đầu mỗi truy vấn.
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 là một trong những công cụ quan trọng giúp hiểu và tối ưu truy vấn MySQL, tuy nhiên, điều đáng tiếc là rất nhiều lập trình viên mới khi sử dụng nó. Trong bài viết này, bạn sẽ được nghiên cứu ý nghĩa của từng thành phần trong kết quả trả về của
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 và cách sử dụng nó để tối ưu hóa thiết kế cơ sở dữ liệu cũng như câu truy vấn

Tìm hiểu kết quả trả về của lệnh ********************** 1. row ********************** id: 1 select_type: SIMPLE table: Country type: const possible_keys: PRIMARY key: PRIMARY key_len: 3 ref: const rows: 1 filtered: 100.00 Extra: ********************** 2. row ********************** id: 1 select_type: SIMPLE table: City type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 4079 filtered: 100.00 Extra: Using where 2 rows in set, 1 warning (0.00 sec) 0

Việc sử dụng

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 hết sức đơn giản, chỉ cần thêm nó vào trước
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
5 trong câu hỏi. Trước tiên, hãy cùng tìm hiểu kết quả trả về của một câu truy vấn đơn giản để bạn có thể làm quen với các cột trong bảng kết quả

EXPLAIN SELECT * FROM categories\G
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)

Có rất nhiều thông tin được bao hàm trong 10 cột trên. Which is

  • ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: Country
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 3
              ref: const
             rows: 1
         filtered: 100.00
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: City
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 4079
         filtered: 100.00
            Extra: Using where
    2 rows in set, 1 warning (0.00 sec)
    
    6 - Số thứ tự cho mỗi câu
    ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: Country
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 3
              ref: const
             rows: 1
         filtered: 100.00
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: City
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 4079
         filtered: 100.00
            Extra: Using where
    2 rows in set, 1 warning (0.00 sec)
    
    5 trong truy vấn của bạn (trường hợp bạn sử dụng các truy vấn lồng nhau (các truy vấn con lồng nhau)

  • ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: Country
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 3
              ref: const
             rows: 1
         filtered: 100.00
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: City
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 4079
         filtered: 100.00
            Extra: Using where
    2 rows in set, 1 warning (0.00 sec)
    
    8 - Loại của câu
    ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: Country
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 3
              ref: const
             rows: 1
         filtered: 100.00
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: City
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 4079
         filtered: 100.00
            Extra: Using where
    2 rows in set, 1 warning (0.00 sec)
    
    5. You can have the following value

    • SHOW WARNINGS\G
      
      0 - Truy vấn là một câu
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: Country
               type: const
      possible_keys: PRIMARY
                key: PRIMARY
            key_len: 3
                ref: const
               rows: 1
           filtered: 100.00
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: City
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 4079
           filtered: 100.00
              Extra: Using where
      2 rows in set, 1 warning (0.00 sec)
      
      5 cơ bản, không có bất cứ truy vấn con (truy vấn con) hay câu lệnh hợp (
      SHOW WARNINGS\G
      
      2) nào
    • SHOW WARNINGS\G
      
      3 - Truy vấn là câu
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: Country
               type: const
      possible_keys: PRIMARY
                key: PRIMARY
            key_len: 3
                ref: const
               rows: 1
           filtered: 100.00
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: City
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 4079
           filtered: 100.00
              Extra: Using where
      2 rows in set, 1 warning (0.00 sec)
      
      5 bên ngoài cùng của lệnh
      SHOW WARNINGS\G
      
      5
    • SHOW WARNINGS\G
      
      6 - Truy vấn là một truy vấn con của một truy vấn khác, nằm trong lệnh của
      SHOW WARNINGS\G
      
      7
    • SHOW WARNINGS\G
      
      8 - Truy vấn đầu tiên của một truy vấn con
    • SHOW WARNINGS\G
      
      9 - Truy vấn con, phụ thuộc vào một truy vấn khác bên ngoài nó
    • ********************** 1. row **********************
        Level: Note
         Code: 1003
      Message: select `World`.`City`.`Name` AS `Name` from `World`.`City` join `World`.`Country` where ((`World`.`City`.`CountryCode` = 'IND'))
      1 row in set (0.00 sec)
      
      0 - Truy vấn không thể lưu lại được (có quy định điều kiện cụ thể, thế nào là truy vấn có thể lưu lại được)
    • SHOW WARNINGS\G
      
      2 - Truy vấn là câu
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: Country
               type: const
      possible_keys: PRIMARY
                key: PRIMARY
            key_len: 3
                ref: const
               rows: 1
           filtered: 100.00
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: City
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 4079
           filtered: 100.00
              Extra: Using where
      2 rows in set, 1 warning (0.00 sec)
      
      5 thứ hai của lệnh
      SHOW WARNINGS\G
      
      2
    • ********************** 1. row **********************
        Level: Note
         Code: 1003
      Message: select `World`.`City`.`Name` AS `Name` from `World`.`City` join `World`.`Country` where ((`World`.`City`.`CountryCode` = 'IND'))
      1 row in set (0.00 sec)
      
      4 - Truy vấn thứ hai hoặc truy vấn tiếp theo của lệnh
      SHOW WARNINGS\G
      
      2 phụ thuộc vào một truy vấn bên ngoài
    • ********************** 1. row **********************
        Level: Note
         Code: 1003
      Message: select `World`.`City`.`Name` AS `Name` from `World`.`City` join `World`.`Country` where ((`World`.`City`.`CountryCode` = 'IND'))
      1 row in set (0.00 sec)
      
      6 - Truy vấn là kết quả của lệnh
      SHOW WARNINGS\G
      
      2
  • ********************** 1. row **********************
      Level: Note
       Code: 1003
    Message: select `World`.`City`.`Name` AS `Name` from `World`.`City` join `World`.`Country` where ((`World`.`City`.`CountryCode` = 'IND'))
    1 row in set (0.00 sec)
    
    8 - Bảng liên quan đến câu hỏi

  • ********************** 1. row **********************
      Level: Note
       Code: 1003
    Message: select `World`.`City`.`Name` AS `Name` from `World`.`City` join `World`.`Country` where ((`World`.`City`.`CountryCode` = 'IND'))
    1 row in set (0.00 sec)
    
    9 - Cách MySQL tham gia các bảng lại với nhau. Đây là một trong những trường quan trọng nhất của kết quả trả về, nó chỉ ra đâu là nơi thiếu chỉ mục (index) và làm cách nào để truy vấn bạn cần phải xem xét lại. Các giá trị trả về có thể là

    • EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      0 - Không có bảng hoặc chỉ có 1 dòng
    • EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      1 - Bảng chỉ có duy nhất 1 dòng đã được đánh chỉ mục phù hợp với điều kiện tìm kiếm. Đây là loại tham gia nhanh nhất, theo bảng chỉ cần đọc một lần duy nhất và giá trị của cột được xem là hằng số khi tham gia với các bảng khác
    • EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      2 - Tất cả các thành phần của chỉ mục được sử dụng bởi lệnh tham gia và chỉ mục thuộc loại
      EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      3 hoặc
      EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      4. Đây là loại tham gia tốt thứ hai (chỉ sau
      EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      1)
    • EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      6 - Tất cả các dòng khớp với điều kiện tìm kiếm và các cột chưa được lập chỉ mục đều được đọc cho mỗi sự kết hợp với các dòng của bảng trước đó. Loại tham gia này có thể được tìm thấy khi cột so sánh với điều kiện
      EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      7 hoặc
      EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      8
    • EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      9 - tham gia sử dụng chỉ mục định dạng
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      0
    • ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      1 - Gần giống như
      EXPLAIN SELECT * FROM
      orderdetails d
      INNER JOIN orders o ON d.orderNumber = o.orderNumber
      INNER JOIN products p ON p.productCode = d.productCode
      INNER JOIN productlines l ON p.productLine = l.productLine
      INNER JOIN customers c on c.customerNumber = o.customerNumber
      WHERE o.orderNumber = 10101\G
      
      6 nhưng bao gồm cả các dòng với cột mang giá trị null
    • ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      3 - tham gia sử dụng một danh sách các mục chỉ để tạo ra kết quả tập tin. Cột
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      4 trong kết quả của
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: Country
               type: const
      possible_keys: PRIMARY
                key: PRIMARY
            key_len: 3
                ref: const
               rows: 1
           filtered: 100.00
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: City
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 4079
           filtered: 100.00
              Extra: Using where
      2 rows in set, 1 warning (0.00 sec)
      
      0 sẽ liệt kê các khóa được sử dụng
    • ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      6 - Truy vấn con với lệnh
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      7 trả về một kết quả duy nhất và sử dụng
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      8
    • ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      9 - Gần giống như
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      6 nhưng trả về nhiều hơn một dòng
    • ALTER TABLE customers
          ADD PRIMARY KEY (customerNumber);
      ALTER TABLE employees
          ADD PRIMARY KEY (employeeNumber);
      ALTER TABLE offices
          ADD PRIMARY KEY (officeCode);
      ALTER TABLE orderdetails
          ADD PRIMARY KEY (orderNumber, productCode);
      ALTER TABLE orders
          ADD PRIMARY KEY (orderNumber),
          ADD KEY (customerNumber);
      ALTER TABLE payments
          ADD PRIMARY KEY (customerNumber, checkNumber);
      ALTER TABLE productlines
          ADD PRIMARY KEY (productLine);
      ALTER TABLE products
          ADD PRIMARY KEY (productCode),
          ADD KEY (buyPrice),
          ADD KEY (productLine);
      ALTER TABLE productvariants
          ADD PRIMARY KEY (variantId),
          ADD KEY (buyPrice),
          ADD KEY (productCode);
      
      1 - Chỉ mục được sử dụng để tìm ra các dòng phụ kiện điều kiện tìm kiếm, cụ thể là khi từ khóa được so sánh với hằng số thông tin qua các toán tử
      ALTER TABLE customers
          ADD PRIMARY KEY (customerNumber);
      ALTER TABLE employees
          ADD PRIMARY KEY (employeeNumber);
      ALTER TABLE offices
          ADD PRIMARY KEY (officeCode);
      ALTER TABLE orderdetails
          ADD PRIMARY KEY (orderNumber, productCode);
      ALTER TABLE orders
          ADD PRIMARY KEY (orderNumber),
          ADD KEY (customerNumber);
      ALTER TABLE payments
          ADD PRIMARY KEY (customerNumber, checkNumber);
      ALTER TABLE productlines
          ADD PRIMARY KEY (productLine);
      ALTER TABLE products
          ADD PRIMARY KEY (productCode),
          ADD KEY (buyPrice),
          ADD KEY (productLine);
      ALTER TABLE productvariants
          ADD PRIMARY KEY (variantId),
          ADD KEY (buyPrice),
          ADD KEY (productCode);
      
      2,
      ********************** 1. row **********************
                 id: 1
        select_type: SIMPLE
              table: l
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 7
              Extra:
      ********************** 2. row **********************
                 id: 1
        select_type: SIMPLE
              table: p
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 110
              Extra: Using where; Using join buffer
      ********************** 3. row **********************
                 id: 1
        select_type: SIMPLE
              table: c
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 122
              Extra: Using join buffer
      ********************** 4. row **********************
                 id: 1
        select_type: SIMPLE
              table: o
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 326
              Extra: Using where; Using join buffer
      ********************** 5. row **********************
                 id: 1
        select_type: SIMPLE
              table: d
               type: ALL
      possible_keys: NULL
                key: NULL
            key_len: NULL
                ref: NULL
               rows: 2996
              Extra: Using where; Using join buffer
      5 rows in set (0.00 sec)
      
      7,
      ALTER TABLE customers
          ADD PRIMARY KEY (customerNumber);
      ALTER TABLE employees
          ADD PRIMARY KEY (employeeNumber);
      ALTER TABLE offices
          ADD PRIMARY KEY (officeCode);
      ALTER TABLE orderdetails
          ADD PRIMARY KEY (orderNumber, productCode);
      ALTER TABLE orders
          ADD PRIMARY KEY (orderNumber),
          ADD KEY (customerNumber);
      ALTER TABLE payments
          ADD PRIMARY KEY (customerNumber, checkNumber);
      ALTER TABLE productlines
          ADD PRIMARY KEY (productLine);
      ALTER TABLE products
          ADD PRIMARY KEY (productCode),
          ADD KEY (buyPrice),
          ADD KEY (productLine);
      ALTER TABLE productvariants
          ADD PRIMARY KEY (variantId),
          ADD KEY (buyPrice),
          ADD KEY (productCode);
      
      4,
      ALTER TABLE customers
          ADD PRIMARY KEY (customerNumber);
      ALTER TABLE employees
          ADD PRIMARY KEY (employeeNumber);
      ALTER TABLE offices
          ADD PRIMARY KEY (officeCode);
      ALTER TABLE orderdetails
          ADD PRIMARY KEY (orderNumber, productCode);
      ALTER TABLE orders
          ADD PRIMARY KEY (orderNumber),
          ADD KEY (customerNumber);
      ALTER TABLE payments
          ADD PRIMARY KEY (customerNumber, checkNumber);
      ALTER TABLE productlines
          ADD PRIMARY KEY (productLine);
      ALTER TABLE products
          ADD PRIMARY KEY (productCode),
          ADD KEY (buyPrice),
          ADD KEY (productLine);
      ALTER TABLE productvariants
          ADD PRIMARY KEY (variantId),
          ADD KEY (buyPrice),
          ADD KEY (productCode);
      
      5,
    • ALTER TABLE customers
          ADD PRIMARY KEY (customerNumber);
      ALTER TABLE employees
          ADD PRIMARY KEY (employeeNumber);
      ALTER TABLE offices
          ADD PRIMARY KEY (officeCode);
      ALTER TABLE orderdetails
          ADD PRIMARY KEY (orderNumber, productCode);
      ALTER TABLE orders
          ADD PRIMARY KEY (orderNumber),
          ADD KEY (customerNumber);
      ALTER TABLE payments
          ADD PRIMARY KEY (customerNumber, checkNumber);
      ALTER TABLE productlines
          ADD PRIMARY KEY (productLine);
      ALTER TABLE products
          ADD PRIMARY KEY (productCode),
          ADD KEY (buyPrice),
          ADD KEY (productLine);
      ALTER TABLE productvariants
          ADD PRIMARY KEY (variantId),
          ADD KEY (buyPrice),
          ADD KEY (productCode);
      
      6 - Toàn bộ cây mục chỉ được duyệt để tìm ra dòng thỏa mãn điều kiện
    • ALTER TABLE customers
          ADD PRIMARY KEY (customerNumber);
      ALTER TABLE employees
          ADD PRIMARY KEY (employeeNumber);
      ALTER TABLE offices
          ADD PRIMARY KEY (officeCode);
      ALTER TABLE orderdetails
          ADD PRIMARY KEY (orderNumber, productCode);
      ALTER TABLE orders
          ADD PRIMARY KEY (orderNumber),
          ADD KEY (customerNumber);
      ALTER TABLE payments
          ADD PRIMARY KEY (customerNumber, checkNumber);
      ALTER TABLE productlines
          ADD PRIMARY KEY (productLine);
      ALTER TABLE products
          ADD PRIMARY KEY (productCode),
          ADD KEY (buyPrice),
          ADD KEY (productLine);
      ALTER TABLE productvariants
          ADD PRIMARY KEY (variantId),
          ADD KEY (buyPrice),
          ADD KEY (productCode);
      
      7 - Toàn bộ bảng được duyệt để tìm dòng tham gia. Đây là loại tham gia tồi tệ nhất và thường cho thấy sự thiếu xót trong việc đánh chỉ mục
  • ALTER TABLE customers
        ADD PRIMARY KEY (customerNumber);
    ALTER TABLE employees
        ADD PRIMARY KEY (employeeNumber);
    ALTER TABLE offices
        ADD PRIMARY KEY (officeCode);
    ALTER TABLE orderdetails
        ADD PRIMARY KEY (orderNumber, productCode);
    ALTER TABLE orders
        ADD PRIMARY KEY (orderNumber),
        ADD KEY (customerNumber);
    ALTER TABLE payments
        ADD PRIMARY KEY (customerNumber, checkNumber);
    ALTER TABLE productlines
        ADD PRIMARY KEY (productLine);
    ALTER TABLE products
        ADD PRIMARY KEY (productCode),
        ADD KEY (buyPrice),
        ADD KEY (productLine);
    ALTER TABLE productvariants
        ADD PRIMARY KEY (variantId),
        ADD KEY (buyPrice),
        ADD KEY (productCode);
    
    8 - Hiển thị
    ALTER TABLE customers
        ADD PRIMARY KEY (customerNumber);
    ALTER TABLE employees
        ADD PRIMARY KEY (employeeNumber);
    ALTER TABLE offices
        ADD PRIMARY KEY (officeCode);
    ALTER TABLE orderdetails
        ADD PRIMARY KEY (orderNumber, productCode);
    ALTER TABLE orders
        ADD PRIMARY KEY (orderNumber),
        ADD KEY (customerNumber);
    ALTER TABLE payments
        ADD PRIMARY KEY (customerNumber, checkNumber);
    ALTER TABLE productlines
        ADD PRIMARY KEY (productLine);
    ALTER TABLE products
        ADD PRIMARY KEY (productCode),
        ADD KEY (buyPrice),
        ADD KEY (productLine);
    ALTER TABLE productvariants
        ADD PRIMARY KEY (variantId),
        ADD KEY (buyPrice),
        ADD KEY (productCode);
    
    9 có thể được sử dụng bởi MySQL để tìm dòng trong bảng, tuy nhiên nó có thể hoặc không thể được sử dụng. Trong thực tế, cột này tăng gấp đôi khi giúp truy vấn tối ưu, bởi nếu cột này trống (NULL), nó thường cho thấy không có chỉ mục liên quan được định nghĩa trong bảng

  • key - Key thực sự được sử dụng bởi MySQL. Cột này có thể chứa từ khoá không được liệt kê ở cột

    ALTER TABLE customers
        ADD PRIMARY KEY (customerNumber);
    ALTER TABLE employees
        ADD PRIMARY KEY (employeeNumber);
    ALTER TABLE offices
        ADD PRIMARY KEY (officeCode);
    ALTER TABLE orderdetails
        ADD PRIMARY KEY (orderNumber, productCode);
    ALTER TABLE orders
        ADD PRIMARY KEY (orderNumber),
        ADD KEY (customerNumber);
    ALTER TABLE payments
        ADD PRIMARY KEY (customerNumber, checkNumber);
    ALTER TABLE productlines
        ADD PRIMARY KEY (productLine);
    ALTER TABLE products
        ADD PRIMARY KEY (productCode),
        ADD KEY (buyPrice),
        ADD KEY (productLine);
    ALTER TABLE productvariants
        ADD PRIMARY KEY (variantId),
        ADD KEY (buyPrice),
        ADD KEY (productCode);
    
    8. Trình độ tối ưu của MySQL luôn cố gắng tìm từ khóa tối ưu nhất cho truy vấn. Khi kết hợp nhiều bảng, nó có thể sử dụng từ khóa không nằm trong danh sách
    ALTER TABLE customers
        ADD PRIMARY KEY (customerNumber);
    ALTER TABLE employees
        ADD PRIMARY KEY (employeeNumber);
    ALTER TABLE offices
        ADD PRIMARY KEY (officeCode);
    ALTER TABLE orderdetails
        ADD PRIMARY KEY (orderNumber, productCode);
    ALTER TABLE orders
        ADD PRIMARY KEY (orderNumber),
        ADD KEY (customerNumber);
    ALTER TABLE payments
        ADD PRIMARY KEY (customerNumber, checkNumber);
    ALTER TABLE productlines
        ADD PRIMARY KEY (productLine);
    ALTER TABLE products
        ADD PRIMARY KEY (productCode),
        ADD KEY (buyPrice),
        ADD KEY (productLine);
    ALTER TABLE productvariants
        ADD PRIMARY KEY (variantId),
        ADD KEY (buyPrice),
        ADD KEY (productCode);
    
    8 nhưng lại mang lại hiệu quả cao hơn

  • ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: o
             type: const
    possible_keys: PRIMARY,customerNumber
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: c
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 3. row **********************
               id: 1
      select_type: SIMPLE
            table: d
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 4
            Extra:
    ********************** 4. row **********************
               id: 1
      select_type: SIMPLE
            table: p
             type: eq_ref
    possible_keys: PRIMARY,productLine
              key: PRIMARY
          key_len: 17
              ref: classicmodels.d.productCode
             rows: 1
            Extra:
    ********************** 5. row **********************
               id: 1
      select_type: SIMPLE
            table: l
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 52
              ref: classicmodels.p.productLine
             rows: 1
            Extra:
    5 rows in set (0.00 sec)
    
    2 - Chiều dài của khóa mà truy vấn tối ưu (Trình tối ưu hóa truy vấn) sử dụng. Ví dụ,
    ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: o
             type: const
    possible_keys: PRIMARY,customerNumber
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: c
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 3. row **********************
               id: 1
      select_type: SIMPLE
            table: d
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 4
            Extra:
    ********************** 4. row **********************
               id: 1
      select_type: SIMPLE
            table: p
             type: eq_ref
    possible_keys: PRIMARY,productLine
              key: PRIMARY
          key_len: 17
              ref: classicmodels.d.productCode
             rows: 1
            Extra:
    ********************** 5. row **********************
               id: 1
      select_type: SIMPLE
            table: l
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 52
              ref: classicmodels.p.productLine
             rows: 1
            Extra:
    5 rows in set (0.00 sec)
    
    2 mang giá trị 4 có nghĩa là nó cần nhớ để lưu 4 ký tự. Bạn đọc có thể xem lại Yêu cầu về bộ nhớ cho từng kiểu dữ liệu trong MySQL

  • EXPLAIN SELECT * FROM
    orderdetails d
    INNER JOIN orders o ON d.orderNumber = o.orderNumber
    INNER JOIN products p ON p.productCode = d.productCode
    INNER JOIN productlines l ON p.productLine = l.productLine
    INNER JOIN customers c on c.customerNumber = o.customerNumber
    WHERE o.orderNumber = 10101\G
    
    6 - Cột tên hoặc hằng số được dùng để so sánh với chỉ mục được nêu ra ở cột
    ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: l
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 7
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: p
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 110
            Extra: Using where; Using join buffer
    ********************** 3. row **********************
               id: 1
      select_type: SIMPLE
            table: c
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 122
            Extra: Using join buffer
    ********************** 4. row **********************
               id: 1
      select_type: SIMPLE
            table: o
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 326
            Extra: Using where; Using join buffer
    ********************** 5. row **********************
               id: 1
      select_type: SIMPLE
            table: d
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 2996
            Extra: Using where; Using join buffer
    5 rows in set (0.00 sec)
    
    4. MySQL can get a constant number, or a column for too process to query. Bạn có thể thấy trong ví dự kiến ​​sẽ được liệt kê dưới đây

  • ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: o
             type: const
    possible_keys: PRIMARY,customerNumber
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: c
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 3. row **********************
               id: 1
      select_type: SIMPLE
            table: d
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 4
            Extra:
    ********************** 4. row **********************
               id: 1
      select_type: SIMPLE
            table: p
             type: eq_ref
    possible_keys: PRIMARY,productLine
              key: PRIMARY
          key_len: 17
              ref: classicmodels.d.productCode
             rows: 1
            Extra:
    ********************** 5. row **********************
               id: 1
      select_type: SIMPLE
            table: l
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 52
              ref: classicmodels.p.productLine
             rows: 1
            Extra:
    5 rows in set (0.00 sec)
    
    6 - Số lượng bản ghi đã được duyệt để trả về kết quả. Đây cũng là một cột hết sức quan trọng đối với việc truy vấn tối ưu, tốt nhất là khi bạn sử dụng
    SHOW WARNINGS\G
    
    5 hoặc truy vấn con

  • ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: o
             type: const
    possible_keys: PRIMARY,customerNumber
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: c
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 3. row **********************
               id: 1
      select_type: SIMPLE
            table: d
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 4
            Extra:
    ********************** 4. row **********************
               id: 1
      select_type: SIMPLE
            table: p
             type: eq_ref
    possible_keys: PRIMARY,productLine
              key: PRIMARY
          key_len: 17
              ref: classicmodels.d.productCode
             rows: 1
            Extra:
    ********************** 5. row **********************
               id: 1
      select_type: SIMPLE
            table: l
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 52
              ref: classicmodels.p.productLine
             rows: 1
            Extra:
    5 rows in set (0.00 sec)
    
    8 - Các thông tin bổ sung liên quan đến quá trình thực hiện truy vấn. Các kiểu giá trị như
    ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: o
             type: const
    possible_keys: PRIMARY,customerNumber
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 2. row **********************
               id: 1
      select_type: SIMPLE
            table: c
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    ********************** 3. row **********************
               id: 1
      select_type: SIMPLE
            table: d
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 4
            Extra:
    ********************** 4. row **********************
               id: 1
      select_type: SIMPLE
            table: p
             type: eq_ref
    possible_keys: PRIMARY,productLine
              key: PRIMARY
          key_len: 17
              ref: classicmodels.d.productCode
             rows: 1
            Extra:
    ********************** 5. row **********************
               id: 1
      select_type: SIMPLE
            table: l
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 52
              ref: classicmodels.p.productLine
             rows: 1
            Extra:
    5 rows in set (0.00 sec)
    
    9 (dùng tạm thời),
    ********************** 1. row **********************
               id: 1
      select_type: SIMPLE
            table: categories
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 4
            Extra:
    1 row in set (0.00 sec)
    
    00 (dùng sắp xếp tệp),. of this column can for see a query is not good good. Danh sách đầy đủ của các giá trị có thể có ở cột này có thể xem tại

Bạn có thể bổ sung thêm từ khóa

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
01 sau khi
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 và MySQL sẽ đưa ra các thông tin bổ sung về quá trình thực hiện truy vấn. Để xem chi tiết, hãy thực hiện lệnh
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
03 ngay sau lệnh
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0. Nó thường được sử dụng để xem các câu truy vấn được thực hiện sau bất kỳ sự thay đổi nào được tạo ra bởi Trình tối ưu hóa truy vấn

EXPLAIN EXTENDED SELECT City.Name FROM City
JOIN Country ON (City.CountryCode = Country.Code)
WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'\G
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
SHOW WARNINGS\G
********************** 1. row **********************
  Level: Note
   Code: 1003
Message: select `World`.`City`.`Name` AS `Name` from `World`.`City` join `World`.`Country` where ((`World`.`City`.`CountryCode` = 'IND'))
1 row in set (0.00 sec)

Khắc phục sự cố về hiệu năng với EXPLAIN

Giờ hãy cùng tìm hiểu cách nào chúng ta có thể tối ưu hóa một truy vấn hiệu năng thấp bằng cách phân tích kết quả của

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0. Trong thực tế, không có gì phải nghi ngờ khi chúng ta sẽ có rất nhiều bảng với rất nhiều quan hệ với nhau, tuy nhiên đôi khi thật khó để biết được cách tốt nhất để viết một câu hỏi

Ở đây, tôi đã tạo ra một cơ sở dữ liệu mẫu cho một ứng dụng thương mại ứng dụng mà ở đó không có mục chính, từ khóa chính và sẽ mô tả ảnh hưởng của thiết kế suy thoái này bằng cách viết ra các truy vấn phức tạp. Bạn có thể tải xuống Mô hình DB từ Github

Từ người dịch. Để bạn đọc có thể hiểu được mô hình DB sử dụng trong bài viết này, tôi đã vẽ lại theo mô hình của tác giả và đính kèm ảnh bên dưới. Rất mong có thể giúp ích cho phần truy vấn tìm hiểu sau đây

Hàng mysql

EXPLAIN SELECT * FROM
orderdetails d
INNER JOIN orders o ON d.orderNumber = o.orderNumber
INNER JOIN products p ON p.productCode = d.productCode
INNER JOIN productlines l ON p.productLine = l.productLine
INNER JOIN customers c on c.customerNumber = o.customerNumber
WHERE o.orderNumber = 10101\G
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: l
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 7
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: p
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 110
        Extra: Using where; Using join buffer
********************** 3. row **********************
           id: 1
  select_type: SIMPLE
        table: c
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 122
        Extra: Using join buffer
********************** 4. row **********************
           id: 1
  select_type: SIMPLE
        table: o
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 326
        Extra: Using where; Using join buffer
********************** 5. row **********************
           id: 1
  select_type: SIMPLE
        table: d
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 2996
        Extra: Using where; Using join buffer
5 rows in set (0.00 sec)

Nhìn vào kết quả trên, bạn thấy được tất cả các biểu hiện của một vấn đề tồi tệ. Tuy nhiên, kể cả khi tôi viết truy vấn tốt hơn, thì kết quả vẫn tương tự, do không có mục đích. Loại tham gia là

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
06 (kém tồi tệ nhất), có nghĩa là MySQL không thể xác định bất kỳ từ khóa nào để sử dụng cho tham gia, và do vậy cả
ALTER TABLE customers
    ADD PRIMARY KEY (customerNumber);
ALTER TABLE employees
    ADD PRIMARY KEY (employeeNumber);
ALTER TABLE offices
    ADD PRIMARY KEY (officeCode);
ALTER TABLE orderdetails
    ADD PRIMARY KEY (orderNumber, productCode);
ALTER TABLE orders
    ADD PRIMARY KEY (orderNumber),
    ADD KEY (customerNumber);
ALTER TABLE payments
    ADD PRIMARY KEY (customerNumber, checkNumber);
ALTER TABLE productlines
    ADD PRIMARY KEY (productLine);
ALTER TABLE products
    ADD PRIMARY KEY (productCode),
    ADD KEY (buyPrice),
    ADD KEY (productLine);
ALTER TABLE productvariants
    ADD PRIMARY KEY (variantId),
    ADD KEY (buyPrice),
    ADD KEY (productCode);
8 và
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: l
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 7
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: p
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 110
        Extra: Using where; Using join buffer
********************** 3. row **********************
           id: 1
  select_type: SIMPLE
        table: c
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 122
        Extra: Using join buffer
********************** 4. row **********************
           id: 1
  select_type: SIMPLE
        table: o
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 326
        Extra: Using where; Using join buffer
********************** 5. row **********************
           id: 1
  select_type: SIMPLE
        table: d
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 2996
        Extra: Using where; Using join buffer
5 rows in set (0.00 sec)
4 đều trống. Quan trọng hơn, trường
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: o
         type: const
possible_keys: PRIMARY,customerNumber
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: c
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
********************** 3. row **********************
           id: 1
  select_type: SIMPLE
        table: d
         type: ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 4
        Extra:
********************** 4. row **********************
           id: 1
  select_type: SIMPLE
        table: p
         type: eq_ref
possible_keys: PRIMARY,productLine
          key: PRIMARY
      key_len: 17
          ref: classicmodels.d.productCode
         rows: 1
        Extra:
********************** 5. row **********************
           id: 1
  select_type: SIMPLE
        table: l
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 52
          ref: classicmodels.p.productLine
         rows: 1
        Extra:
5 rows in set (0.00 sec)
6 cho thấy MySQL phải duyệt tất cả các bản ghi của từng bảng cho câu hỏi này. Có nghĩa là để chạy câu truy vấn, nó cần phải duyệt
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
10 bản ghi để tìm ra kết quả. Điều này thật khủng khiếp và nó sẽ còn tăng cường hơn nữa khi cơ sở dữ liệu lớn hơn

Bây giờ, chúng ta sẽ thử thêm 1 số mục khác hiển thị, giống như từ khóa chính cho từng bảng, và thực hiện truy vấn một lần nữa. Theo thông tin cơ bản, bạn tìm đến các cột được sử dụng để THAM GIA và để chúng làm từ khóa, bởi MySQL sẽ luôn tìm theo các cột đó để tra cứu các bản ghi

ALTER TABLE customers
    ADD PRIMARY KEY (customerNumber);
ALTER TABLE employees
    ADD PRIMARY KEY (employeeNumber);
ALTER TABLE offices
    ADD PRIMARY KEY (officeCode);
ALTER TABLE orderdetails
    ADD PRIMARY KEY (orderNumber, productCode);
ALTER TABLE orders
    ADD PRIMARY KEY (orderNumber),
    ADD KEY (customerNumber);
ALTER TABLE payments
    ADD PRIMARY KEY (customerNumber, checkNumber);
ALTER TABLE productlines
    ADD PRIMARY KEY (productLine);
ALTER TABLE products
    ADD PRIMARY KEY (productCode),
    ADD KEY (buyPrice),
    ADD KEY (productLine);
ALTER TABLE productvariants
    ADD PRIMARY KEY (variantId),
    ADD KEY (buyPrice),
    ADD KEY (productCode);

Giờ chúng ta chạy lại truy vấn sau khi đã thêm chỉ mục

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: o
         type: const
possible_keys: PRIMARY,customerNumber
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: c
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
********************** 3. row **********************
           id: 1
  select_type: SIMPLE
        table: d
         type: ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 4
        Extra:
********************** 4. row **********************
           id: 1
  select_type: SIMPLE
        table: p
         type: eq_ref
possible_keys: PRIMARY,productLine
          key: PRIMARY
      key_len: 17
          ref: classicmodels.d.productCode
         rows: 1
        Extra:
********************** 5. row **********************
           id: 1
  select_type: SIMPLE
        table: l
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 52
          ref: classicmodels.p.productLine
         rows: 1
        Extra:
5 rows in set (0.00 sec)

Sau khi thêm chỉ mục, số lượng bản ghi cần được duyệt giảm xuống còn

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
11. Điều đó có nghĩa là, với mỗi bản ghi có
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
12 là 10101 trong bảng
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
13, MySQL có thể tìm thấy trự tiếp bản ghi đối tượng trong tất cả các bản ghi khác bằng cách sử dụng chỉ mục và không cần phải duyệt lại cả bảng

Ở dòng đầu tiên, loại tham gia là

EXPLAIN SELECT * FROM
orderdetails d
INNER JOIN orders o ON d.orderNumber = o.orderNumber
INNER JOIN products p ON p.productCode = d.productCode
INNER JOIN productlines l ON p.productLine = l.productLine
INNER JOIN customers c on c.customerNumber = o.customerNumber
WHERE o.orderNumber = 10101\G
1, loại nhanh nhất với bảng có nhiều hơn 1 bản ghi. MySQL can use main key in this field. Trường
EXPLAIN SELECT * FROM
orderdetails d
INNER JOIN orders o ON d.orderNumber = o.orderNumber
INNER JOIN products p ON p.productCode = d.productCode
INNER JOIN productlines l ON p.productLine = l.productLine
INNER JOIN customers c on c.customerNumber = o.customerNumber
WHERE o.orderNumber = 10101\G
6 trả về kết quả
EXPLAIN SELECT * FROM
orderdetails d
INNER JOIN orders o ON d.orderNumber = o.orderNumber
INNER JOIN products p ON p.productCode = d.productCode
INNER JOIN productlines l ON p.productLine = l.productLine
INNER JOIN customers c on c.customerNumber = o.customerNumber
WHERE o.orderNumber = 10101\G
1, có nghĩa là không có gì ngoài giá trị 10101 được sử dụng trong lệnh
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
17

Tiếp theo hãy xem một truy vấn khác. Ở đây, chúng ta chỉ đơn giản là hợp 2 bảng lại,

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
18 và
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
19, cả hai đều được tham gia với
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
20. Bảng
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
19 bao gồm các biến có thể của
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
22 ở dạng khóa bên ngoài

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
0
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
1

Bạn có thể thấy rất nhiều vấn đề trong câu hỏi này. Nó duyệt tất cả các bản ghi ở bảng

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
18 và
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
19. Vì không có bất kỳ mục nào trong các bảng này cho trường
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
25 và
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
26, kết quả trả về cột
ALTER TABLE customers
    ADD PRIMARY KEY (customerNumber);
ALTER TABLE employees
    ADD PRIMARY KEY (employeeNumber);
ALTER TABLE offices
    ADD PRIMARY KEY (officeCode);
ALTER TABLE orderdetails
    ADD PRIMARY KEY (orderNumber, productCode);
ALTER TABLE orders
    ADD PRIMARY KEY (orderNumber),
    ADD KEY (customerNumber);
ALTER TABLE payments
    ADD PRIMARY KEY (customerNumber, checkNumber);
ALTER TABLE productlines
    ADD PRIMARY KEY (productLine);
ALTER TABLE products
    ADD PRIMARY KEY (productCode),
    ADD KEY (buyPrice),
    ADD KEY (productLine);
ALTER TABLE productvariants
    ADD PRIMARY KEY (variantId),
    ADD KEY (buyPrice),
    ADD KEY (productCode);
8 và
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: l
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 7
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: p
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 110
        Extra: Using where; Using join buffer
********************** 3. row **********************
           id: 1
  select_type: SIMPLE
        table: c
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 122
        Extra: Using join buffer
********************** 4. row **********************
           id: 1
  select_type: SIMPLE
        table: o
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 326
        Extra: Using where; Using join buffer
********************** 5. row **********************
           id: 1
  select_type: SIMPLE
        table: d
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 2996
        Extra: Using where; Using join buffer
5 rows in set (0.00 sec)
4 đều trống. Trạng thái của
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
18 và
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
30 được kiểm tra sau khi hợp lại
SHOW WARNINGS\G
2, do vậy công việc cho họ vào trong
SHOW WARNINGS\G
2 sẽ làm giảm số lượng bản ghi. Giờ chúng ta thử thêm vài mục và viết lại truy vấn

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
2_______7_______3
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
4

Như bạn thấy ở kết quả, số lượng dòng được duyệt giảm đáng kể, từ

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
33 xuống còn
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
34, thực sự là một hiệu quả rất đáng ghi nhận. Nếu bạn cố gắng chạy cùng một truy vấn mà không sắp xếp lại từ trước, ngay sau khi thêm chỉ mục, bạn sẽ không thể thấy số lượng giảm đáng kể như vậy. MySQL không thể sử dụng chỉ mục bởi nó sử dụng
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
17 trong kết quả trả về. Sau khi chuyển điều kiện sang bên trong
SHOW WARNINGS\G
2, MySQL has been used index. Điều đó có nghĩa là thêm chỉ mục không phải là đủ, MySWL sẽ không thể sử dụng chỉ mục đó trừ khi bạn viết câu truy vấn thích hợp

Kết luận

Trong bài viết này, tôi đã thảo luận về từ khóa

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 của MySQL, ý nghĩa của kết quả trả về và cách sử dụng nó để viết truy vấn tốt hơn. Trong thực tế, nó còn có thể hữu dụng hơn ví dụ được đưa ra ở đây. Thực tế, bạn sẽ thường xuyên tham gia nhiều bảng với câu lệnh
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
17 phức tạp. Việc chỉ đơn giản thêm chỉ mục sẽ không phải lúc nào cũng giúp bạn, bạn cần dành thời gian suy nghĩ kỹ càng hơn và viết truy vấn tốt hơn

Quan điểm cá nhân

Lần đầu tiên tôi biết đến

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 cũng là khi tôi biết được chương trình của mình thực hiện quá nhiều truy vấn sử dụng
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
40 (Phần
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: o
         type: const
possible_keys: PRIMARY,customerNumber
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: c
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
********************** 3. row **********************
           id: 1
  select_type: SIMPLE
        table: d
         type: ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 4
        Extra:
********************** 4. row **********************
           id: 1
  select_type: SIMPLE
        table: p
         type: eq_ref
possible_keys: PRIMARY,productLine
          key: PRIMARY
      key_len: 17
          ref: classicmodels.d.productCode
         rows: 1
        Extra:
********************** 5. row **********************
           id: 1
  select_type: SIMPLE
        table: l
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 52
          ref: classicmodels.p.productLine
         rows: 1
        Extra:
5 rows in set (0.00 sec)
8 trả về
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
1 row in set (0.00 sec)
00). Đây là 1 sắp xếp không có chỉ mục và rất nguy hiểm khi làm việc với lượng dữ liệu lớn. Việc sử dụng
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 giúp tôi phát hiện ra sự thiếu xót trong thiết kế DB của mình ở thời điểm đó và qua đó biết cách chỉnh sửa thiết kế của mình

Ngày nay, việc sử dụng các khung hỗ trợ ORM khiến cho các thành viên lập trình không quan tâm đến quá trình truy vấn dữ liệu trong DB, kéo theo đó là các vấn đề về hiệu năng khi ứng dụng được bật lên. Ngược lại, việc viết truy vấn bằng tay, bất chấp thời gian nhưng trả lại hiệu quả cao về hiệu năng sau này cũng như nâng cao hiểu biết của người lập trình. Khi tôi còn đang thực hiện tập tin sinh, tôi phải viết tất cả truy vấn bằng tay và theo chỉ định của người hướng dẫn, tôi phải tự

********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 tất cả truy vấn trước khi viết mã. Những kinh nghiệm đó đã giúp tôi tích lũy được rất nhiều kinh nghiệm cho công việc sau này. Rất mong mọi người cũng có cùng điểm và dành thời gian
********************** 1. row **********************
           id: 1
  select_type: SIMPLE
        table: Country
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 3
          ref: const
         rows: 1
     filtered: 100.00
        Extra:
********************** 2. row **********************
           id: 1
  select_type: SIMPLE
        table: City
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4079
     filtered: 100.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)
0 câu hỏi của mình trước khi bắt tay vào code ứng dụng thực tế