想学习改善mysql查询慢的问题

发布于 2024-11-05 22:58:08 字数 2202 浏览 0 评论 0原文

我有一个 MySQL 查询来选择所有产品 ID,并将某些过滤器应用于产品。这个查询 有效,但我想学习改进这个查询。欢迎使用此查询的替代方案并提供解释。

SELECT kkx_products.id from kkx_products WHERE display = 'yes' AND id in
    (SELECT product_id FROM `kkx_filters_products` WHERE `filter_id` in 
       (SELECT id FROM `kkx_filters` WHERE kkx_filters.urlname = "comics" OR kkx_filters.urlname = "comicsgraphicnovels") 
    group by product_id having count(*) = 2) 
    ORDER BY kkx_products.id desc LIMIT 0, 24 

我已经包含了查询中使用的表的结构。

解释kkx_filters

Field            Type                 Null  Key     Default        Extra
id               int(11) unsigned     NO    PRI     NULL           auto_increment
name             varchar(50)          NO             
filtergroup_id   int(11)              YES   MUL     NULL     
urlname          varchar(50)          NO    MUL     NULL     
date_modified    timestamp            NO            CURRENT_TIMESTAMP    
orderid          float(11,2)          NO            NULL    

解释kkx_filters_products

Field            Type                 Null  Key   Default          Extra
filter_id        int(11)              NO    PRI   0    
product_id       int(11)              NO    PRI   0   

解释kkx_products

Field            Type                 Null  Key   Default          Extra
id               int(11)              NO    PRI   NULL             auto_increment
title            varchar(255)         NO           
urlname          varchar(50)          NO    MUL        
description      longtext             NO          NULL   
price            float(11,2)          NO          NULL   
orderid          float(11,2)          NO          NULL   
imageurl         varchar(255)         NO            
date_created     datetime             NO          NULL    
date_modified    timestamp            NO          CURRENT_TIMESTAMP    
created_by       varchar(11)          NO          NULL    
modified_by      varchar(11)          NO          NULL   
productnumber    varchar(32)          NO           
instock          enum('yes','no')     NO          yes    
display          enum('yes','no')     NO          yes    

I have a MySQL query to select all product id's with certain filters applied to the products. This query
works but I want to learn to improve this query. Alternatives for this query are welcome with explanation.

SELECT kkx_products.id from kkx_products WHERE display = 'yes' AND id in
    (SELECT product_id FROM `kkx_filters_products` WHERE `filter_id` in 
       (SELECT id FROM `kkx_filters` WHERE kkx_filters.urlname = "comics" OR kkx_filters.urlname = "comicsgraphicnovels") 
    group by product_id having count(*) = 2) 
    ORDER BY kkx_products.id desc LIMIT 0, 24 

I've included the structure of the tables being used in the query.

EXPLAINkkx_filters;

Field            Type                 Null  Key     Default        Extra
id               int(11) unsigned     NO    PRI     NULL           auto_increment
name             varchar(50)          NO             
filtergroup_id   int(11)              YES   MUL     NULL     
urlname          varchar(50)          NO    MUL     NULL     
date_modified    timestamp            NO            CURRENT_TIMESTAMP    
orderid          float(11,2)          NO            NULL    

EXPLAIN kkx_filters_products;

Field            Type                 Null  Key   Default          Extra
filter_id        int(11)              NO    PRI   0    
product_id       int(11)              NO    PRI   0   

EXPLAIN kkx_products;

Field            Type                 Null  Key   Default          Extra
id               int(11)              NO    PRI   NULL             auto_increment
title            varchar(255)         NO           
urlname          varchar(50)          NO    MUL        
description      longtext             NO          NULL   
price            float(11,2)          NO          NULL   
orderid          float(11,2)          NO          NULL   
imageurl         varchar(255)         NO            
date_created     datetime             NO          NULL    
date_modified    timestamp            NO          CURRENT_TIMESTAMP    
created_by       varchar(11)          NO          NULL    
modified_by      varchar(11)          NO          NULL   
productnumber    varchar(32)          NO           
instock          enum('yes','no')     NO          yes    
display          enum('yes','no')     NO          yes    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2024-11-12 22:58:08

不要在条件语句中使用内联查询,而是尝试使用 EXISTS 块...
http://dev.mysql.com /doc/refman/5.0/en/exists-and-not-exists-subqueries.html

您将能够看到解释计划中的差异。在对结果集中的每条记录执行查询之前,并且内联视图结果集中的每个结果都有其自己的查询执行。

您会看到嵌套内联视图如何导致成本呈指数级增长。 EXISTS 不是这样工作的。

使用 EXISTS 的示例:

考虑 tbl1 具有列 id 和 data。 tbl2 具有 id、parentid 和 data 列。

SELECT a.*
FROM tbl1 a
WHERE 1 = 1 
AND EXISTS (
  SELECT NULL 
  FROM tbl2 b
  WHERE b.parentid = a.id 
  AND b.data = 'SOME CONDITIONAL DATA TO CONSTRAIN ON'
)

1) 我们可以假设 1 = 1 是某个条件,对于每个记录都等于 true
2)无论我们在 EXISTS 语句中选择什么,NULL 都可以。
3)重要的是查看b.parentid = a.id,这将我们的exist语句链接到结果集

Instead of using inline queries in your criteria statements, try using the EXISTS block...
http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

You will be able to see the difference in your explain plan. Before you had a query executing for each and every record in your result set, and every result in that inline view result set had its own query executing to.

You see how nested inline views can create an exponential increase in cost. EXISTS doesn't work that way.

Example of the use of EXISTS:

Consider tbl1 has columns id and data. tbl2 has columns id, parentid, and data.

SELECT a.*
FROM tbl1 a
WHERE 1 = 1 
AND EXISTS (
  SELECT NULL 
  FROM tbl2 b
  WHERE b.parentid = a.id 
  AND b.data = 'SOME CONDITIONAL DATA TO CONSTRAIN ON'
)

1) We can assume the 1 = 1 is some condition that equates to true for every record
2) Doesn't matter what we select in the EXISTS statment really, NULL is fine.
3) It is important to look at b.parentid = a.id, this links our exist statement to the result set

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文