peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Tạo Index cho nhiều cột trong MySQL - Freetuts
VIEW
STORED PROCEDURE
INDEX
FULL TEXT SEARCH
TRIGGER
TIPS
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Tạo Index cho nhiều cột trong MySQL

Trong bài này chúng ta sẽ học cách tạo chỉ mục index cho nhiều cột trong MySQL, đây là cách giúp bạn tối ưu hóa câu truy vấn trong một số trường hợp cụ thể.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Tùy vào từng câu truy vấn và nhu cầu của bài toán mà ta sẽ chọn chỉ mục và số cột khác nhau. Việc đánh chỉ mục này giúp MySQL phân loại được dữ liệu và trả kêt quả về tốt hơn.

1. Khi nào tạo chỉ mục tổng hợp nhiều cột?

Chỉ mục tổng hợp là một chỉ mục trên nhiều cột. MySQL cho phép bạn tạo một chỉ mục tổng hợp bao gồm tối đa 16 cột. Việc tạo chỉ mục này giúp các câu truy vấn được xử lý tối ưu hơn.

Ví dụ bạn muốn truy vấn tìm danh sách sinh viên thuộc khoa CNTT và lớp CN01 thì có thể tạo chỉ mục cho 2 field (khoa_id, lop_id). Với cách làm này sẽ giúp tất cả những câu truy vấn có sử dụng cặp điều kiện này đều chạy nhanh hơn.

Bài viết này được đăng tại [free tuts .net]

Để tạo một chỉ mục tổng hợp tại thời điểm tạo bảng thì ta sử dụng câu lệnh sau:

CREATE TABLE table_name (
    c1 data_type PRIMARY KEY,
    c2 data_type,
    c3 data_type,
    c4 data_type,
    INDEX index_name (c2,c3,c4)
);

Trong ví dụ này thì mình đã tạo chỉ mục có tên là index_name gồm ba cột (c2, c3 ,c4).

Hoặc bạn cũng có thể sử dụng lệnh Create Index để tạo chỉ mục.

CREATE INDEX index_name 
ON table_name(c2,c3,c4);

Lưu ý rằng nếu bạn tạo chỉ mục cho n cột thì những câu truy vấn có số cột ít hơn đều được tối ưu hóa, và phải tuân theo thứ tự từ trái sang phải. Như trong ví dụ trên thì những trường hợp sau:

(c1)
(c1,c2)
(c1,c2,c3)

Ví dụ:

SELECT
    *
FROM
    table_name
WHERE
    c1 = v1;


SELECT
    *
FROM
    table_name
WHERE
    c1 = v1 AND 
    c2 = v2;


SELECT  
    *
FROM
    table_name
WHERE
    c1 = v1 AND 
    c2 = v2 AND 
    c3 = v3;

Trình tối ưu hóa truy vấn bắt buộc phải theo thứ tự từ trái qua phải, nếu không thì index không có tác dụng gì cả. Như trong ví dụ trên thì nếu bạn thiết lập where:

  • c1 = v1 and c3 = v3 => sai
  • c1 = v1 and c2 = v2 => đúng
  • c2 = v2 => sai

2. Một ví dụ đặt chỉ mục nhiều cột trong MySQL

Giả sử chúng ta có bảng employees có cấu trúc như sau:

employees table png

Sau đó mình tạo index gồm hai cột firstName và lastName.

CREATE INDEX name 
ON employees(lastName, firstName);

Như vậy chúng ta chỉ có thể truy vấn cho hai trường hợp:

  • Chỉ mỗi lastName
  • Cả lastNamefirstName

Trường hợp chỉ mỗi lastName

Hãy tìm kiếm nhân viên có tên là "Patterson".

SELECT 
    firstName, 
    lastName, 
    email
FROM
    employees
WHERE
    lastName = 'Patterson';

Câu truy vấn này có áp dụng index ở trên bởi vì lệnh where đầu tiên chính là lastName. Hãy đặt thêm lệnh EXPLAIN đằng trước câu Select để xem kết quả.

EXPLAIN SELECT 
    firstName, 
    lastName, 
    email
FROM
    employees
WHERE
    lastName = 'Patterson';

Kết quả:

MySQL Composite Index Example 1 png

Như vậy câu SQL trên đã duyệt qua 3 rows.

Trường hợp có cả lastName và firstName

SELECT 
    firstName, 
    lastName, 
    email
FROM
    employees
WHERE
    lastName = 'Patterson' AND
    firstName = 'Steve';

Tương tự trường hợp này vẫn được áp dụng index ở trên, bởi vì theo thứ tự index là lastName -> fistName.

MySQL Composite Index Example 2 png

Như trong hình là câu SQL đã duyệt qua 1 rows.

Trường hợp truy vấn mỗi firstName

SELECT 
    firstName, 
    lastName, 
    email
FROM
    employees
WHERE
    firstName = 'Leslie';

Trường hợp này index trên không có tác dụng gì cả.

Như vậy là mình đã hướng dẫn xong cách tạo ra index trên nhiều column, cũng như nguyên tắc áp dung index vào nhiều trường hợp khác nhau.

Hãy nhớ thứ tự các field rất quan trọng trong việc đặt thứ tự các lệnh where nhé.

Cùng chuyên mục:

Hàm ENCRYPT trong MySQL

Hàm ENCRYPT trong MySQL

Cách sử dụng ENCRYPT trong MySQL

Hàm MD5 trong MySQL

Hàm MD5 trong MySQL

Cách sử dụng MD5 trong MySQL

Hàm OLD_PASSWORD trong MySQL

Hàm OLD_PASSWORD trong MySQL

Cách sử dụng OLD_PASSWORD trong MySQL

Hàm PASSWORD trong MySQL

Hàm PASSWORD trong MySQL

Cách sử dụng PASSWORD trong MySQL

Hàm ASCII trong MySQL

Hàm ASCII trong MySQL

Cách sử dụng ASCII trong MySQL

Hàm CHAR_LENGTH trong MySQL

Hàm CHAR_LENGTH trong MySQL

Cách sử dụng CHAR_LENGTH trong MySQL

Hàm CHARACTER_LENGTH trong MySQL

Hàm CHARACTER_LENGTH trong MySQL

Cách sử dụng CHARACTER_LENGTH trong MySQL

Hàm CONCAT trong MySQL

Hàm CONCAT trong MySQL

Cách sử dụng CONCAT trong MySQL

Hàm CONCAT_WS trong MySQL

Hàm CONCAT_WS trong MySQL

Cách sử dụng CONCAT_WS trong MySQL

Hàm FIELD trong MySQL

Hàm FIELD trong MySQL

Cách sử dụng FIELD trong MySQL

Hàm FIND_IN_SET trong MySQL

Hàm FIND_IN_SET trong MySQL

Cách sử dụng FIND_IN_SET trong MySQL

Hàm FORMAT trong MySQL

Hàm FORMAT trong MySQL

Cách sử dụng FORMAT trong MySQL

Hàm INSERT trong MySQL

Hàm INSERT trong MySQL

Cách sử dụng INSERT trong MySQL

Hàm INSTR trong MySQL

Hàm INSTR trong MySQL

Cách sử dụng INSTR trong MySQL

Hàm LCASE trong MySQL

Hàm LCASE trong MySQL

Cách sử dụng LCASE trong MySQL

Hàm LEFT trong MySQL

Hàm LEFT trong MySQL

Cách sử dụng LEFT trong MySQL

Hàm LENGTH trong MySQL

Hàm LENGTH trong MySQL

Cách sử dụng LENGTH trong MySQL

Hàm LOCATE trong MySQL

Hàm LOCATE trong MySQL

Cách sử dụng LOCATE trong MySQL

Hàm LOWER trong MySQL

Hàm LOWER trong MySQL

Cách sử dụng LOWER trong MySQL

Hàm LPAD trong MySQL

Hàm LPAD trong MySQL

Cách sử dụng LPAD trong MySQL

Top