简体   繁体   English

优化查询(MySQL)

[英]Optimization query (MySQL)

I have this tables in MYSQL: 我在MYSQL中有此表:

products(prodId, Hide)
cat_binds(prodId,SptPath)
params(paramId,prodId,langId,value)

Query 询问

SELECT 
        DISTINCT(params.value) 
FROM 
        products,
        cat_binds, 
        params 
WHERE 
        products.prodId = params.prodId 
        AND 
        params.paramId =  '1' 
        AND 
        params.langId =  '1' 
        AND 
        products.prodId = cat_binds.prodId 
        AND 
        cat_binds.SptPath LIKE '-2147483644\_%' 
        AND 
        products.Hide = '0'

How optimizing this query? 如何优化此查询? How create right indexes which generate less rows wanted for result. 如何创建正确的索引以生成较少的结果行。

Thanks for help 感谢帮助

Use joins instead of cartisians which fetch undesired rows. 使用联接而不是获取不需要的行的笛卡尔。

SELECT DISTINCT
  (params.value)
FROM products
  LEFT JOIN cat_binds
    ON cat_binds.prodId = products.prodId
  LEFT JOIN params
    ON params.prodId = products.prodId
WHERE params.paramId = '1'
    AND params.langId = '1'
    AND cat_binds.SptPath LIKE '-2147483644\_%'
    AND products.Hide = '0'

Your SQL looks good and if you have added the Primary Index on all the IDs fields i think it will fine . 您的SQL看起来不错,如果您在所有ID字段上添加了主索引,我认为它会很好。 MySQL will also optimized internally to make the order correct . MySQL还将在内部进行优化以使顺序正确。 I have only concern about the WHERE condition containing LIKE expression. 我只关心包含LIKE表达式的WHERE条件。 As you are using the '%' condition in the last I think adding Index to this column will be helpful but if you put '%' in front then index will have not much effect 由于您在最后使用'%'条件,因此我认为在此列中添加索引会有所帮助,但是如果您将'%'放在前面,则索引不会有太大影响

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

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