GIẢI THÍCH MySQL

Khi bạn đưa ra một truy vấn, Trình tối ưu hóa truy vấn MySQL sẽ cố gắng đưa ra một kế hoạch tối ưu để thực hiện truy vấn. Bạn có thể xem thông tin về kế hoạch bằng cách đặt trước truy vấn 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)
5.
********************** 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)
5 là một trong những công cụ mạnh mẽ nhất mà bạn có thể sử dụng để hiểu và tối ưu hóa các truy vấn rắc rối của MySQL, nhưng có một thực tế đáng buồn là nhiều nhà phát triển hiếm khi sử dụng nó. Trong bài viết này, bạn sẽ tìm hiểu đầu ra 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)
5 có thể là gì và cách sử dụng nó để tối ưu hóa lược đồ và truy vấn của bạn

GIẢI THÍCH MySQL

Hiểu đầu ra của EXPLAIN

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)
5 cũng đơn giản như việc chờ đợi nó trước các truy vấn 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)
9. Hãy phân tích đầu ra của một truy vấn đơn giản để làm quen với các cột được trả về bởi lệnh

EXPLAIN SELECT * FROM categoriesG
********************** 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ó vẻ như không phải vậy, nhưng có rất nhiều thông tin được gói gọn trong 10 cột đó. Các cột được truy vấn trả về là

  • EXPLAIN EXTENDED SELECT City.Name FROM City
    JOIN Country ON (City.CountryCode = Country.Code)
    WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
    0 – một mã định danh tuần tự cho mỗ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)
    9 trong truy vấn (khi bạn có các truy vấn con lồng nhau)
  • EXPLAIN EXTENDED SELECT City.Name FROM City
    JOIN Country ON (City.CountryCode = Country.Code)
    WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
    2 – loạ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)
    9. giá trị có thể là
    • EXPLAIN EXTENDED SELECT City.Name FROM City
      JOIN Country ON (City.CountryCode = Country.Code)
      WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
      4 – truy vấn là một 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)
      9 đơn giản mà không có bất kỳ truy vấn phụ hoặc
      EXPLAIN EXTENDED SELECT City.Name FROM City
      JOIN Country ON (City.CountryCode = Country.Code)
      WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
      6 nào
    • CHÍ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)
      9 nằm ở truy vấn ngoài cùng trong một
      EXPLAIN EXTENDED SELECT City.Name FROM City
      JOIN Country ON (City.CountryCode = Country.Code)
      WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
      8
    • DERIVED –
      ********************** 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)
      9 là một phần của truy vấn con trong mệ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
    • SUBQUERY –
      ********************** 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)
      9 đầu tiên trong truy vấn con
    • DEPENDENT SUBQUERY – truy vấn con phụ thuộc vào truy vấn bên ngoài
    • UNCACHEABLE SUBQUERY – truy vấn con không thể lưu vào bộ đệm (có một số điều kiện nhất định để truy vấn có thể lưu vào bộ đệm)
    • UNION –
      ********************** 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)
      9 là câu lệnh thứ hai hoặc sau đó của UNION
    • DEPENDENT UNION –
      ********************** 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)
      9 thứ hai hoặc muộn hơn của một
      EXPLAIN EXTENDED SELECT City.Name FROM City
      JOIN Country ON (City.CountryCode = Country.Code)
      WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
      6 phụ thuộc vào một truy vấn bên ngoài
    • KẾT QUẢ ĐOÀN 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)
      9 là kết quả của một
      EXPLAIN EXTENDED SELECT City.Name FROM City
      JOIN Country ON (City.CountryCode = Country.Code)
      WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
      6
  • ********************** 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)
    7 – bảng được giới thiệu bởi hà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)
    8 – cách MySQL tham gia các bảng được sử dụng. Đây là một trong những trường sâu sắc nhất trong đầu ra vì nó có thể chỉ ra các chỉ mục bị thiếu hoặc cách viết truy vấn cần được xem xét lại. giá trị có thể là
    • hệ thống – bảng chỉ có 0 hoặc một hàng
    • const - bảng chỉ có một hàng phù hợp được lập chỉ mục. Đây là kiểu nối nhanh nhất vì bảng chỉ phải được đọc một lần và giá trị của cột có thể được coi là hằng số khi nối các bảng khác
    • eq_ref – tất cả các phần của chỉ mục được sử dụng bởi liên kết và chỉ mục là
      ********************** 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)
      9 hoặc
      SHOW WARNINGSG
      0. Đây là loại tham gia tốt nhất có thể tiếp theo
    • tham chiếu - tất cả các hàng phù hợp của một cột được lập chỉ mục được đọc cho mỗi kết hợp các hàng từ bảng trước đó. Kiểu liên kết này xuất hiện cho các cột được lập chỉ mục so với việc sử dụng toán tử
      SHOW WARNINGSG
      1 hoặc
      SHOW WARNINGSG
      2
    • toàn văn – liên kết sử dụng chỉ mục
      SHOW WARNINGSG
      3 của bảng
    • ref_or_null – giống như ref nhưng cũng chứa các hàng có giá trị null cho cột
    • index_merge – tham gia sử dụng danh sách các chỉ mục để tạo tập kết quả. Cột khóa của đầu ra 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)
      5 sẽ chứa các khóa được sử dụng
    • unique_subquery – truy vấn con
      SHOW WARNINGSG
      5 chỉ trả về một kết quả từ bảng và sử dụng khóa chính
    • index_subquery – giống như unique_subquery nhưng trả về nhiều hơn một hàng kết quả
    • phạm vi – một chỉ mục được sử dụng để tìm các hàng phù hợp trong một phạm vi cụ thể, thường là khi cột chính được so sánh với một hằng số bằng cách sử dụng các toán tử như
      SHOW WARNINGSG
      6,
      SHOW WARNINGSG
      5,
      SHOW WARNINGSG
      8,
      SHOW WARNINGSG
      9, v.v.
    • chỉ mục - toàn bộ cây chỉ mục được quét để tìm các hàng phù hợp
    • tất cả - toàn bộ bảng được quét để tìm các hàng phù hợp để nối. Đây là loại tham gia tồi tệ nhất và thường chỉ ra việc thiếu các chỉ mục thích hợp trên bảng
  • ********************** 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 – hiển thị các khóa mà MySQL có thể sử dụng để tìm các hàng từ bảng, mặc dù chúng có thể được sử dụng hoặc không trong thực tế. Trên thực tế, cột này thường có thể giúp tối ưu hóa các truy vấn vì nếu cột là NULL, nó cho biết không tìm thấy chỉ mục liên quan nào
  • ********************** 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)
    1 – cho biết chỉ mục thực tế được sử dụng bởi MySQL. Cột này có thể chứa một chỉ mục không được liệt kê trong cột
    ********************** 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)
    2. Trình tối ưu hóa MySQL luôn tìm khóa tối ưu có thể được sử dụng cho truy vấn. Trong khi nối nhiều bảng, nó có thể tìm ra một số khóa khác không được liệt kê trong
    ********************** 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)
    2 nhưng tối ưu hơ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)
    4 – cho biết độ dài của chỉ mục mà Trình tối ưu hóa Truy vấn đã chọn sử dụng. Ví dụ: giá trị
    ********************** 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 là 4 có nghĩa là nó yêu cầu bộ nhớ lưu trữ bốn ký tự. Kiểm tra các yêu cầu lưu trữ kiểu dữ liệu của MySQL để biết thêm về điều này
  • ********************** 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 – Hiển thị các cột hoặc hằng số được so sánh với chỉ mục có tên trong cột chính. MySQL sẽ chọn một giá trị không đổi để so sánh hoặc một cột dựa trên kế hoạch thực hiện truy vấn. Bạn có thể thấy điều này trong ví dụ dưới đây
  • ********************** 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)
    7 – liệt kê số lượng hồ sơ đã được kiểm tra để tạo ra đầu ra. Đây là một cột quan trọng khác đáng để tập trung vào việc tối ưu hóa các truy vấn, đặc biệt đối với các truy vấn sử dụng
    EXPLAIN EXTENDED SELECT City.Name FROM City
    JOIN Country ON (City.CountryCode = Country.Code)
    WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
    8 và các truy vấn con
  • ********************** 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 – chứa thông tin bổ sung về kế hoạch thực hiện truy vấn. Các giá trị như “Sử dụng tạm thời”, “Sử dụng sắp xếp tệp”, v.v. trong cột này có thể chỉ ra một truy vấn rắc rối. Để biết danh sách đầy đủ các giá trị có thể có và ý nghĩa của chúng, hãy xem

Bạn cũng có thể thêm từ khóa

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 = 10101G
0 sau
********************** 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)
5 trong truy vấn của mình và MySQL sẽ hiển thị cho bạn thông tin bổ sung về cách nó thực hiện truy vấn. Để xem thông tin, hãy làm theo 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)
5 của bạn vớ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 = 10101G
3. Điều này chủ yếu hữu ích để xem truy vấn được thực thi sau khi bất kỳ chuyển đổi nào được thực hiện 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 WARNINGSG
********************** 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ố Hiệu suất với EXPLAIN

Bây giờ, hãy xem cách chúng ta có thể tối ưu hóa một truy vấn hoạt động kém bằng cách phân tích đầu ra 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)
5. Khi xử lý một ứng dụng trong thế giới thực, chắc chắn sẽ có một số bảng có nhiều mối quan hệ giữa chúng, nhưng đôi khi thật khó để dự đoán cách viết truy vấn tối ưu nhất

Ở đây, tôi đã tạo một cơ sở dữ liệu mẫu cho một ứng dụng thương mại điện tử không có bất kỳ chỉ mục hoặc khóa chính nào và sẽ chứng minh tác động của một thiết kế tồi tệ như vậy bằng cách viết một truy vấn khá tệ. Bạn có thể tải xuống mẫu lược đồ từ GitHub

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 = 10101G
********************** 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)

Nếu bạn nhìn vào kết quả trên, bạn có thể thấy tất cả các dấu hiệu của một truy vấn không hợp lệ. Nhưng ngay cả khi tôi đã viết một truy vấn tốt hơn, kết quả vẫn như vậy vì không có chỉ mục. Loại liên kết được hiển thị là “TẤT CẢ” (tệ nhất), có nghĩa là MySQL không thể xác định bất kỳ khóa nào có thể được sử dụng trong liên kết và do đó, các cột

********************** 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 và
********************** 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)
1 là null. Quan trọng nhất, cột
********************** 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)
7 hiển thị MySQL quét tất cả các bản ghi của mỗi bảng để truy vấn. Điều đó có nghĩa là để thực hiện truy vấn, nó sẽ quét 7 × 110 × 122 × 326 × 2996 = 91.750.822.240 bản ghi để tìm bốn kết quả phù hợp. Điều đó thực sự khủng khiếp và nó sẽ chỉ tăng theo cấp số nhân khi cơ sở dữ liệu phát triển

Bây giờ, hãy thêm một số chỉ mục rõ ràng, chẳng hạn như khóa chính cho mỗi bảng và thực hiện lại truy vấn. Theo nguyên tắc chung, bạn có thể xem các cột được sử dụng trong mệnh đề

EXPLAIN EXTENDED SELECT City.Name FROM City
JOIN Country ON (City.CountryCode = Country.Code)
WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
8 của truy vấn như các ứng cử viên tốt cho các khóa vì MySQL sẽ luôn quét các cột đó để tìm các bản ghi phù hợp

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);

Hãy chạy lại cùng một truy vấn sau khi thêm các chỉ mục và kết quả sẽ như thế nà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)

Sau khi thêm chỉ mục, số lượng bản ghi được quét đã giảm xuống còn 1 × 1 × 4 × 1 × 1 = 4. Điều đó có nghĩa là đối với mỗi bản ghi 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 = 10101G
9 10101 trong bả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, MySQL có thể trực tiếp tìm thấy bản ghi phù hợp trong tất cả các bảng khác bằng cách sử dụng các chỉ mục và không cần phải quét toàn bộ bảng

Trong đầu ra của hàng đầu tiên, bạn có thể thấy kiểu nối được sử dụng là “const”, đây là kiểu nối nhanh nhất cho một bảng có nhiều bản ghi. MySQL đã có thể sử dụng khóa

********************** 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 làm chỉ mục. Cột tham chiếu hiển thị “const”, không có gì khác ngoài giá trị 10101 được sử dụng trong mệ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)
2 của truy vấn

Hãy xem thêm một truy vấn ví dụ. Ở đây về cơ bản, chúng ta sẽ lấy hợp của hai bả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)
3 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, mỗi bảng được nối với
********************** 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)
5. Bả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)
4 bao gồm các biến thể sản phẩm khác nhau với
********************** 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 làm khóa tham chiếu và giá của chú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)
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 một số vấn đề trong truy vấn này. Nó quét tất cả các bản ghi trong bả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)
3 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. Vì không có chỉ mục trên các bảng này cho các 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);
0 và
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, nên các cột
********************** 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 và
********************** 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)
1 của kết quả hiển thị null. Trạng thái của
********************** 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 và
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 được kiểm tra sau
EXPLAIN EXTENDED SELECT City.Name FROM City
JOIN Country ON (City.CountryCode = Country.Code)
WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
6, vì vậy việc di chuyển chúng vào bên trong
EXPLAIN EXTENDED SELECT City.Name FROM City
JOIN Country ON (City.CountryCode = Country.Code)
WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
6 sẽ làm giảm số lượng bản ghi. Hãy thêm một số chỉ mục bổ sung 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
********************** 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)
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 có thể thấy trong kết quả, giờ đây số lượng hàng gần đúng được quét đã giảm đáng kể từ 2.625.810 (219 × 110 × 109) xuống còn 276 (12 × 23), đây là mức tăng hiệu suất rất lớn. Nếu bạn thử cùng một truy vấn, không có sự sắp xếp lại trước đó, chỉ sau khi thêm các chỉ mục, bạn sẽ không thấy giảm nhiều. MySQL không thể sử dụng các chỉ mục vì nó có mệ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)
2 trong kết quả dẫn xuất. Sau khi di chuyển các điều kiện đó vào bên trong
EXPLAIN EXTENDED SELECT City.Name FROM City
JOIN Country ON (City.CountryCode = Country.Code)
WHERE City.CountryCode = 'IND' AND Country.Continent = 'Asia'G
6, nó có thể sử dụng các chỉ mục. Điều này có nghĩa là chỉ thêm một chỉ mục không phải lúc nào cũng đủ;

Tóm lược

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

********************** 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)
5, ý nghĩa của đầu ra của nó và cách bạn có thể sử dụng đầu ra của nó để xây dựng các truy vấn tốt hơn. Trong thế giới thực, nó có thể hữu ích hơn các kịch bản được trình bày ở đây. Thông thường, bạn sẽ nối một số bảng lại với nhau và sử dụng các mệ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)
2 phức tạp. Các chỉ mục được thêm đơn giản trên một vài cột có thể không phải lúc nào cũng hữu ích và sau đó là lúc để xem xét kỹ hơn các truy vấn của bạn

Hình ảnh qua Efman / Shutterstock

Chia sẻ bài viết này

xấu hổ C

Shameer là một lập trình viên đam mê và đam mê mã nguồn mở đến từ Kerala, Ấn Độ. Anh ấy có kinh nghiệm phát triển web bằng Scala, PHP, Ruby, MySQL và JavaScript. Khi không làm việc, Shameer dành thời gian viết mã cho các dự án cá nhân, học hỏi, xem diễn viên trên màn hình, viết blog, v.v. Các lĩnh vực quan tâm cụ thể của ông bao gồm điện toán đám mây, quản trị hệ thống và cơ sở dữ liệu

MySQL là gì và tại sao nó được sử dụng?

MySQL là hệ thống quản lý cơ sở dữ liệu . Nó có thể là bất cứ thứ gì từ danh sách mua sắm đơn giản đến thư viện ảnh hoặc lượng thông tin khổng lồ trong mạng công ty. Để thêm, truy cập và xử lý dữ liệu được lưu trữ trong cơ sở dữ liệu máy tính, bạn cần một hệ thống quản lý cơ sở dữ liệu như MySQL Server.

Sự khác biệt giữa SQL và MySQL là gì?

SQL là ngôn ngữ lập trình truy vấn quản lý RDBMS. MySQL là một hệ thống quản lý cơ sở dữ liệu quan hệ sử dụng SQL . SQL chủ yếu được sử dụng để truy vấn và vận hành các hệ thống cơ sở dữ liệu. MySQL cho phép bạn xử lý, lưu trữ, sửa đổi và xóa dữ liệu và lưu trữ dữ liệu một cách có tổ chức.

MySQL giải thích chi tiết với ví dụ là gì?

MySQL là hệ thống quản lý cơ sở dữ liệu quan hệ (RDBMS) do Oracle phát triển dựa trên ngôn ngữ truy vấn có cấu trúc (SQL) . Cơ sở dữ liệu là một tập hợp dữ liệu có cấu trúc. Nó có thể là bất cứ thứ gì, từ danh sách mua sắm đơn giản đến thư viện ảnh hoặc nơi chứa lượng thông tin khổng lồ trong mạng công ty.

Giải thích nghĩa là gì trong MySQL?

Câu lệnh EXPLAIN cung cấp thông tin về cách MySQL thực thi các câu lệnh . EXPLAIN hoạt động với các câu lệnh SELECT , DELETE , INSERT , REPLACE và UPDATE. EXPLAIN trả về một hàng thông tin cho mỗi bảng được sử dụng trong câu lệnh SELECT.