Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Cải thiện hiệu suất tìm kiếm MySQL với wildcard (%%)

MySQL không cải thiện hiệu suất tìm kiếm khi có wildcard dẫn đầu (%...) vì không thể dùng index. Nếu đổi thành 'chữ_cái%' thì có thể dùng index.

Cú pháp tốt hơn với trailing wildcard:

SELECT *FROM yourTableName WHERE yourColumnName LIKE 'anyLetter%';

Demo: Tạo bảng TrailingWildCardDemo, chèn 7 tên.

Truy vấn LIKE 'j%':

mysql> select *from TrailingWildCardDemo where Name LIKE 'j%';
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Johnson | | 7 | James | +----+---------+ 3 rows in set (0.00 sec)