在我们的应用程序中,我们使用这 3 个表:
- 类别
- 城市(category_id)
- city_data(距离,city_id)
$q = "SELECT a.id as aid,a.distance as adistance, b.id as bid,b.distance as bdistance FROM city_data as a JOIN city_data as b on a.id != b.id JOIN cities AS a_cities ON a.city_id = a_cities.id JOIN cities AS b_cities ON b.city_id = b_cities.id WHERE (a_cities.category_id='".$_GET["c"]."' AND b_cities.category_id='".$_GET["c"]."') AND abs(a.distance - b.distance) < 100 ORDER BY RAND() LIMIT 1";
表城市,city_data具有相同的行数 -几乎 5.000。上面的查询大约需要45 秒,这很糟糕。更糟糕的是,这些表应该有另外 5.000 行,总共有 10.000 行......
我想问你任何方式,如何减少执行上述查询的时间...... 45s是不可接受的......
我有什么办法可以解决这个问题吗?
编辑:谢谢你的建议,我删除了 ORDER BY RAND()部分,时间真的很短,大约 22 秒,但这对于通常使用来说仍然太高了