好夥計……在你的幫助下我們有乙個解決方案……見……
但我仍然有乙個問題……
我改變了表來新增索引……
alter table `users` add fulltext ( `username` );
alter table `table_1` add fulltext ( `field_abc`,`field_def` );
alter table `table_2` add fulltext ( `field_ghi`,`field_jkl` );
然後我接受了@barmar的建議並將**改為此…
select users.username,
table_1.field_abc, table_1.field_def,
table_2.field_ghi, table_2.field_jkl
from users
left join table_1 on table_1.username = users.username
left join table_2 on table_2.username = users.username
where
match(table_1.field_abc,table_1.field_def,table_2.field_ghi,table_2.field_jkl)
against ("spork yellow" in boolean mode)
group by users.username
order by
( case when match(table_1.field_abc) against ("spork" in boolean mode) then 1 else 0 end ) +
( case when match(table_1.field_abc) against ("yellow" in boolean mode) then 1 else 0 end ) +
( case when match(table_1.field_def) against ("spork" in boolean mode) then 1 else 0 end ) +
( case when match(table_1.field_def) against ("yellow" in boolean mode) then 1 else 0 end ) +
( case when match(table_2.field_ghi) against ("spork" in boolean mode) then 1 else 0 end ) +
( case when match(table_2.field_ghi) against ("yellow" in boolean mode) then 1 else 0 end ) +
( case when match(table_2.field_ghi) against ("spork" in boolean mode) then 1 else 0 end ) +
( case when match(table_2.field_ghi) against ("yellow" in boolean mode) then 1 else 0 end )
)desc;
在我的真實資料庫中有超過1,000,000條記錄,我的結果是6.5027秒.那比a …好多了,花了這麼長時間才發現它!
mysql優化的查詢語句 優化MySQL查詢語句
前言 查詢語句的優化是sql效率優化的乙個方式,可以通過優化sql語句來盡量使用已有的索引,避免全表掃瞄,從而提高查詢效率。最近在對專案中的一些sql進行優化,總結整理了一些方法。1 盡量避免在 where 子句中對字段進行 null 值判斷 應盡量避免在 where 子句中對字段進行 null 值...
mysql 分區間查詢 MySQL區間分組查詢
假設a表為會員資訊表,需要統計男性會員年齡各階段的出現的人數 create table a id int 11 unsigned not null auto increment,name varchar 255 not null default comment 會員名稱 tinyint 1 unsi...
mysql怎麼查詢列命令 mysql常用查詢命令
常用mysql命令 show variables like character set client 查詢字符集 show databases 列出所有的伺服器上的資料庫alter create database if not exists test 建立乙個資料庫 drop database fk...