我是一个新手,有麻烦与一个或查询!
当我做它没有或执行时间是:0.02秒
SELECT DISTINCT p.*,
ht.html_title,
ht.html_content,
sc.sub_cat_name,
sc.main_cat_id,
us.username,
c.name AS cityname
FROM ad_product p,
ad_html ht,
ad_catagory_sub sc,
ad_user us,
ad_cities c
WHERE ( ht.ad_id = p.id )
AND ( sc.sub_cat_id = p.category )
AND ( us.id = p.user_id )
AND ( p.category = 216 )
AND ( p.status = 'active' )
AND ( c.id = p.city )
AND ( p.city = 135 )
ORDER BY p.created_at DESC;
当我在任何条件下试的时候,大约需要4.70秒
SELECT DISTINCT p.*,
ht.html_title,
ht.html_content,
sc.sub_cat_name,
sc.main_cat_id,
us.username,
c.name AS cityname
FROM ad_product p,
ad_html ht,
ad_catagory_sub sc,
ad_user us,
ad_cities c
WHERE ( ht.ad_id = p.id )
AND ( sc.sub_cat_id = p.category )
AND ( us.id = p.user_id )
AND ( p.category = 216
OR p.parent_category = 216 )
AND ( p.status = 'active' )
AND ( c.id = p.city )
AND ( p.city = 135 )
ORDER BY p.created_at DESC;
解释:
+----+-------------+-------+------------+-------------+--------------------------------------------------------------------------------+--------------------------+---------+---------------------------+-------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------------+--------------------------------------------------------------------------------+--------------------------+---------+---------------------------+-------+----------+----------------------------------------------------+
| 1 | SIMPLE | c | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using temporary; Using filesort |
| 1 | SIMPLE | p | NULL | index_merge | PRIMARY,id,parent_category,city,user_id_2,category,status,id_2,id_3,category_2 | category,parent_category | 4,4 | NULL | 67889 | 4.74 | Using union(category,parent_category); Using where |
| 1 | SIMPLE | sc | NULL | eq_ref | PRIMARY | PRIMARY | 4 | abc_classified.p.category | 1 | 100.00 | NULL |
| 1 | SIMPLE | us | NULL | eq_ref | PRIMARY | PRIMARY | 4 | abc_classified.p.user_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | ht | NULL | ref | ad_id | ad_id | 4 | abc_classified.p.id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------------+--------------------------------------------------------------------------------+--------------------------+---------+---------------------------+-------+----------+----------------------------------------------------+
我想我已经定义了适当的索引,有人能指出我做错了什么吗?或者如何减少执行时间?我知道为什么会这样?
3条答案
按热度按时间z0qdvdin1#
在where条件中定义精确值时,sql使用
hash join
. 所以它比其他连接要快得多。当您将其更改为非精确值条件(x或y)时,它会变慢。解决办法是做一个
index
以减少要搜索的记录。nwsw7zdq2#
我认为你的查询将受益于工会而不是or。这将创建两组数据。一个在类别ID上,另一个在父类别ID上。这比定义两个负集要快。一组没有categoryid=216,一组没有父categoryid=216。
8ehkhllq3#
@dwight reynoldson answers在sql server上运行得很好,我在mysql上找到了一个“order by”问题的解决方法。我所做的只是
ORDER BY created_at DESC
而不是ORDER BY p.created_at DESC
是p制造了所有的问题。回答: