我有一個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的輸出嗎? –