mysql NOT IN QUERY 优化

发布于 2024-11-09 10:28:19 字数 1283 浏览 0 评论 0原文

我有两个表,名称为:

  1. table_product
  2. table_user_ownned_auction

table_product

specific_product_id      astatus  ... 
(primary_key,autoinc)
--------------------------------------
1                        APAST    ...
2                        ALIVE    ...
3                        ALIVE    ...
4                        APAST    ... 
5                        APAST    ...

table_user_ownned_auction

own_id     specific_product_id   details   
----------------------------------------
1                  1               XXXX
2                  5               XXXX

我需要选择 atatus = APAST,而不是在表 2 中。
这意味着,在上面的结构中,table1 有 3 个 APAST 状态(1、4、5)。但在表 2 Specific_product_id (1,5) 中仅存储,因此我需要选择 Specific_product_id = 4

我使用了此查询

  SELECT * 
    FROM table_product 
   WHERE astatus = 'APAST' 
     AND specific_product_id NOT IN (SELECT specific_product_id 
                                       FROM table_user_ownned_auction )

...这需要这么长时间:

查询花费了 115.1039 秒

执行耗时 115.1039 秒。

解释计划

在此处输入图像描述

我如何优化它或以其他方式选择我想要的内容?

I have two tables named as:

  1. table_product
  2. table_user_ownned_auction

table_product

specific_product_id      astatus  ... 
(primary_key,autoinc)
--------------------------------------
1                        APAST    ...
2                        ALIVE    ...
3                        ALIVE    ...
4                        APAST    ... 
5                        APAST    ...

table_user_ownned_auction

own_id     specific_product_id   details   
----------------------------------------
1                  1               XXXX
2                  5               XXXX

I need to select atatus = APAST, and not in table 2.
Which means, in above structure table1 has 3 APAST status (1,4,5). But in table 2 specific_product_id (1,5) only stored so i need to select specific_product_id = 4

I used this query

  SELECT * 
    FROM table_product 
   WHERE astatus = 'APAST' 
     AND specific_product_id NOT IN (SELECT specific_product_id 
                                       FROM table_user_ownned_auction )

...which takes this long:

Query took 115.1039 sec

...to execute.

EXPLAIN PLAN

enter image description here

How can i optimize it or any other way to select what i want?

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

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

发布评论

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

评论(2

悲欢浪云 2024-11-16 10:28:20

尝试在 table_user_ownned_auction 表上添加索引:

ALTER TABLE table_user_ownned_auction ADD KEY(specific_product_id)

另外,尝试使用 non-exists 连接:

SELECT p.*
FROM table_product p
    LEFT JOIN table_user_ownned_auction l
      ON p.specific_product_id = l.specific_product_id
WHERE p.astatus = 'APAST' 
    AND l.specific_product_id IS NULL

Try adding a index on the table_user_ownned_auction table:

ALTER TABLE table_user_ownned_auction ADD KEY(specific_product_id)

Also, try using a non-exists join:

SELECT p.*
FROM table_product p
    LEFT JOIN table_user_ownned_auction l
      ON p.specific_product_id = l.specific_product_id
WHERE p.astatus = 'APAST' 
    AND l.specific_product_id IS NULL
音栖息无 2024-11-16 10:28:19

使用NOT EXISTS

SELECT p.* 
  FROM TABLE_PRODUCT p
 WHERE p.astatus = 'APAST' 
   AND NOT EXISTS (SELECT NULL
                     FROM TABLE_USER_OWNED_AUCTION uoa
                    WHERE uoa.specific_product_id = p.specific_product_id)

使用LEFT JOIN/IS NULL

   SELECT p.* 
     FROM TABLE_PRODUCT p
LEFT JOIN TABLE_USER_OWNED_AUCTION uoa ON uoa.specific_product_id = p.specific_product_id
    WHERE p.astatus = 'APAST' 
      AND uoa.own_id IS NULL

说明

最佳查询可以通过两个表之间比较的列是否可为 NULL 来确定(即:如果 <任一表中的 code>specific_product_id 都可以为 NULL)。

附录

一旦确定了最佳查询,请查看创建索引(可能覆盖索引) 至少:

  • special_product_id
  • TABLE_PRODUCT.status

Using NOT EXISTS

SELECT p.* 
  FROM TABLE_PRODUCT p
 WHERE p.astatus = 'APAST' 
   AND NOT EXISTS (SELECT NULL
                     FROM TABLE_USER_OWNED_AUCTION uoa
                    WHERE uoa.specific_product_id = p.specific_product_id)

Using LEFT JOIN/IS NULL

   SELECT p.* 
     FROM TABLE_PRODUCT p
LEFT JOIN TABLE_USER_OWNED_AUCTION uoa ON uoa.specific_product_id = p.specific_product_id
    WHERE p.astatus = 'APAST' 
      AND uoa.own_id IS NULL

Explanation

The most optimal query can be determined by if the columns compared between the two tables are NULLable (IE: if the values of specific_product_id in either table can be NULL).

Addendum

Once the optimal query has been determined, take a look at creating indexes (possibly covering indexes) for at least:

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