简体   繁体   English

在PHP中查询大MySql表效率?

[英]Querying a large MySql table efficiency in PHP?

My database has almost 15.000.000 entries.我的数据库有近 15.000.000 个条目。 Each entry is a phrase and each phrase starts in upper case and ends in a point (.) ex: "This is a sample phrase."每个条目都是一个短语,每个短语都以大写字母开头并以点 (.) 结尾,例如:“这是一个示例短语”。

I need to get 10 random phrases that contain a specific word ( $q ).我需要获得 10 个包含特定单词( $q )的随机短语。 Because $q can be in the beginning of the phrase i will use also $q_upper for the query.因为 $q 可以在短语的开头,所以我也将使用 $q_upper 进行查询。

Using this query:使用此查询:

$res = mysqli_query($db,"SELECT quotes FROM corpus WHERE quotes LIKE '% ".$q." %' or quotes LIKE '% ".$q.".' or quotes '".$q_upper." %' order BY RAND() LIMIT 10");

Because its a large database it takes around 10 sec and a lot of resources to deliver results.因为它是一个大型数据库,它需要大约 10 秒和大量资源来提供结果。

Is there a way I can optimize this query (or maybe split the database ) in order to make it efficiency.有没有办法可以优化这个查询(或者拆分数据库)以提高效率。

Use a case-folding collation -- That gets rid of q_upper.使用 case-folding collation -- 去掉 q_upper。

Use FULLTEXT instead of LIKE -- that gives a lot of speed.使用 FULLTEXT 而不是 LIKE - 这样可以提高速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM