我有一个MySQL的大查询magento这要花很多时间。我试图优化它,但是我的MySQL知识还不足以让它解决,所以也许有人可以看一看,并给我一些正确方向的提示。OR在MySQL哪里使查询非常缓慢
Select Distinct
`e`.*,
`cat_index`.`position` AS `cat_index_position`,
`price_index`.`price`,
`price_index`.`tax_class_id`,
`price_index`.`final_price`,
IF(price_index.tier_price IS NOT NULL,
LEAST(price_index.min_price, price_index.tier_price),
price_index.min_price) AS `minimal_price`,
`price_index`.`min_price`,
`price_index`.`max_price`,
`price_index`.`tier_price`
From
`catalog_product_entity` AS `e`
Inner Join
`catalog_category_product_index` As `cat_index`
On cat_index.product_id=e.entity_id And
cat_index.store_id=1 And
cat_index.visibility In(2, 4) And
cat_index.category_id='2'
Inner Join
`catalog_product_index_price` AS `price_index`
On price_index.entity_id = e.entity_id And
price_index.website_id = '1' And
price_index.customer_group_id = 0
Left Join
`beta_entity_product` AS `beta`
On e.entity_id = beta.product_id And
beta.entity_id In (81558, 81559, ... stupidly long list of ids)
Left Join
`catalog_product_entity_int` AS `is_uni`
On e.entity_id = is_uni.entity_id And
attribute_id = 179
Where
is_uni.value = 1 OR
beta.product_id IS NOT NULL
如果我只是在WHERE子句中的一切条件1是好的,但与OR需要有时一些时间才能完成并且那太长了。我必须获得哪些选项才能获得更好的结果?另一个问题是,我无法从中进行更多的查询,只是将结果加在一起。一切都必须在1个查询中。
当我做一个对查询我得到以下结果EXPLAIN(JSON格式复制为更好的概述):
{
"data":
[
{
"id": 1,
"select_type": "SIMPLE",
"table": "e",
"type": "ALL",
"possible_keys": "PRIMARY",
"key": null,
"key_len": null,
"ref": null,
"rows": 213396,
"Extra": "Using temporary"
},
{
"id": 1,
"select_type": "SIMPLE",
"table": "beta",
"type": "range",
"possible_keys": "PRIMARY",
"key": "PRIMARY",
"key_len": "4",
"ref": null,
"rows": 2833,
"Extra": "Using where; Using index"
},
{
"id": 1,
"select_type": "SIMPLE",
"table": "is_uni",
"type": "ref",
"possible_keys": "UNQ_CATALOG_PRODUCT_ENTITY_INT_ENTITY_ID_ATTRIBUTE_ID_STORE_ID,IDX_CATALOG_PRODUCT_ENTITY_INT_ATTRIBUTE_ID,IDX_CATALOG_PRODUCT_ENTITY_INT_ENTITY_ID",
"key": "UNQ_CATALOG_PRODUCT_ENTITY_INT_ENTITY_ID_ATTRIBUTE_ID_STORE_ID",
"key_len": "6",
"ref": "unc_cpk.e.entity_id,const",
"rows": 1,
"Extra": "Using where"
},
{
"id": 1,
"select_type": "SIMPLE",
"table": "cat_index",
"type": "eq_ref",
"possible_keys": "PRIMARY,IDX_CAT_CTGR_PRD_IDX_PRD_ID_STORE_ID_CTGR_ID_VISIBILITY,15D3C269665C74C2219037D534F4B0DC",
"key": "PRIMARY",
"key_len": "10",
"ref": "const,unc_cpk.e.entity_id,const",
"rows": 1,
"Extra": "Using where"
},
{
"id": 1,
"select_type": "SIMPLE",
"table": "price_index",
"type": "eq_ref",
"possible_keys": "PRIMARY,IDX_CATALOG_PRODUCT_INDEX_PRICE_CUSTOMER_GROUP_ID,IDX_CATALOG_PRODUCT_INDEX_PRICE_WEBSITE_ID",
"key": "PRIMARY",
"key_len": "8",
"ref": "unc_cpk.cat_index.product_id,const,const",
"rows": 1,
"Extra": "Using where"
}
]
}
这个查询中究竟是这个'或'。它是...“宽”。 –
在最后的查询结束位置:WHERE is_uni.value = 1 OR beta.product_id IS NOT NULL – n3on
可以显示describe table'catalog_product_entity'的输出和EXPLAIN w/o的输出吗? –