从mysql innodb表中获取过滤器属性(大约1.5米行)

时间:2014-01-03 23:46:58

标签: php mysql

我有一个产品表,上面有1.5米的行。我想获得产品并对其进行过滤(关于其类别,品牌等)

我是这样做的: (场景是:我输入id为35的类别,我想获取其及其子产品)

SELECT distinct r_category as r_category,brand as brand,price as price,offer_id FROM Product where Product.status = 1 and Product.r_category IN (35,279,280,274,276,278,277,275,39,294,295,296,38,292,293,290,291,289,34,271,272,273,36,283,282,284,281,37,285,286,288,287,350,351,348,349,4)

此代码告诉我他们有哪些类别,品牌和价格。

我的产品大约0.049秒,但是当我运行此代码时,它提升到7秒。

0.14223003387451 first query (with limit 20)


7.0965619087219 last query time ( about 957895 rows)

(我的表是innodb,我有索引r_category,品牌,价格等)

全部谢谢

table properties

table indexes

2 个答案:

答案 0 :(得分:1)

要减少查询时间,您可以执行以下操作:

  • 在where子句中使用的所有列中添加索引(本例中为status,r_category)
  • 添加力FORCE INDEX(r_category) - > SELECT distinct r_category as r_category,brand as brand,price as price,offer_id FROM Product FORCE INDEX(r_category) where Product.status = 1 and Product.r_category IN (35,279,280,274,276,278,277,275,39,294,295,296,38,292,293,290,291,289,34,271,272,273,36,283,282,284,281,37,285,286,288,287,350,351,348,349,4)

答案 1 :(得分:0)

如果可以通过限制和偏移对您的查询进行分页。在PHP中从数据库接收957895行时,会有大量内存分配。当您使用较少的内存时,您的脚本将更快地工作。