我是高级查询的新手,所以我可能在概念上有些错误,因为当数据库有超过 100 万条记录时,我会从我的查询中得到这个响应......
ERROR 2013: Lost connection to MySQL server during query
是的!它实际上需要很长时间才能完成之前呕吐。
我的查询是这个...
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
table_1.field_abc REGEXP "(spork|yellow)" OR
table_1.field_def REGEXP "(spork|yellow)" OR
table_2.field_ghi REGEXP "(spork|yellow)" OR
table_2.field_jkl REGEXP "(spork|yellow)"
GROUP BY users.username
ORDER BY
(
( CASE WHEN table_1.field_abc LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_abc LIKE "%yellow%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_def LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_def LIKE "%yellow%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_ghi LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_ghi LIKE "%yellow%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_jkl LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_jkl LIKE "%yellow%" THEN 1 ELSE 0 END )
)DESC;
我在http://sqlfiddle.com/#!2/cbbda/28发布了一个示例数据集(只有几条记录)
sqlfiddle 的示例运行速度很快,因为只有几条记录,但我尝试在我自己的服务器上复制记录,查询运行速度很快,只有几条记录,在添加一百万条记录后速度非常慢。
有什么方法可以快速得到我的结果吗?