gpt4 book ai didi

php - JOINed 表上的 GROUP BY 和 ORDER BY - 复杂且缓慢

转载 作者:可可西里 更新时间:2023-11-01 07:39:59 25 4
gpt4 key购买 nike

故事是这样的……我有用户,他们有 child 。我想每天使用 CRON JOB 优惠券向在 child 出生日期间隔内有 child 的用户发送。我想知道谁将成为获得优惠券的用户以及哪个 child 。此外,我只想为每个 child 发送一张优惠券,并且该 child 必须是用户拥有的最小的 child 。

我有以下表格

Children
+--------------------------------------+
- Primary Key: childrenID (int)
- Index: userID (int)
- Index: childBirthDate (date)
+--------------------------------------+
- childrenID - userID - childBirthDate -
- 1 - 1 - 21/01/2000 -
- 2 - 1 - 01/11/2013 -
- 3 - 1 - 25/10/2013 -
- 4 - 2 - 01/11/2013 -
- 5 - 3 - 01/11/2013 -
+--------------------------------------+

Users
+------------------------+
- Primary Key: userID (int)
- Index: categoryGroup (varchar)
+------------------------+
- userID - categoryGroup -
- 1 - 'Group1' -
- 2 - 'Group1' -
- 3 - 'Group2' -
- 4 - 'Group2' -
+------------------------+

CuponRequests
+------------------------+
- Primary Key: ID (int)
- Index: userID (int)
- Index: cuponID (int)
+-----------------------+
- ID - cuponID - userID -
- 1 - 1 - 1 -
- 1 - 2 - 1 -
- 1 - 1 - 2 -
+-----------------------+

这基本上是具有相关列的三个主要表格我有以下 SQL 查询来执行和获取我需要的结果。

SELECT users.userID,
users.categoryGroup children.childBirthDate,
children.childrenID
FROM users,
(SELECT *
FROM
(SELECT children.childrenID,
children.childBirthDate,
users.userID AS child_uid
FROM children,
users
WHERE children.userID = users.userID
ORDER BY children.childBirthDate DESC)t1
GROUP BY child_uid)children
WHERE (children.childBirthDate <= DATE_SUB(CURDATE(), INTERVAL 5 MONTH))
AND (children.childBirthDate > DATE_SUB(CURDATE() , INTERVAL 6 MONTH))
AND (children.child_uid = users.userID)
AND ('Group1, Group2' LIKE CONCAT('%', users.categoryGroup, '%'))
AND NOT EXISTS
(SELECT userID,
cuponID
FROM cuponRequests
WHERE userID = users.userID
AND cuponID = 1)
AND userID = 1
ORDER BY children.childBirthDate DESC

对于这个查询,我试图只针对一个用户和一张优惠券但这是自然行为——查询对所有用户有效

“cuponID”和间隔来自脚本的 PHP 端 - 我迭代“cupons”表(此处未提及)并在每个“优惠券”行上执行此查询)

问题是这个查询被执行了大约 1.5 秒 (O.O)除了在 CRON JOB 环境中运行此脚本外,此脚本还会在用户注册到网站后立即运行。我有 96 个杯子 - 这会使注册速度减慢大约一分钟(很多)


我认为这个查询

SELECT *
FROM
(SELECT children.childrenID,
children.childBirthDate,
users.userID AS child_uid
FROM children,
users
WHERE children.userID = users.userID
ORDER BY children.childBirthDate DESC)t1
GROUP BY child_uid

减慢速度。我尝试在这样的选择查询中执行 JOIN 而不是选择查询:

FROM users LEFT JOIN children ON children.userID = users.userID

但是后来我失去了“ORDER BY childBirthDate DESC”来得到这个用户最小的 child ,我失去了“GROUP BY child_uid”来得到他的一个 child

有什么想法可以让事情变得更快但仍然有效吗?

附言对不起,我的英语不好。


编辑:

这是 EXPLAIN SQL 的输出

+----+--------------------+---------------+-------+----------------+---------+---------+------------------------------+-------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------------+-------+----------------+---------+---------+------------------------------+-------+-----------------------------------------------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
| 4 | DEPENDENT SUBQUERY | cuponRequests | ref | userID,cuponID | userID | 5 | const | 1 | Using where |
| 2 | DERIVED | <derived3> | ALL | NULL | NULL | NULL | NULL | 73526 | Using temporary; Using filesort |
| 3 | DERIVED | users | index | PRIMARY | PRIMARY | 4 | NULL | 69271 | Using index; Using temporary; Using filesort |
| 3 | DERIVED | children | ref | userID | userID | 4 | users.userID | 1 | |
+----+--------------------+---------------+-------+----------------+---------+---------+------------------------------+-------+-----------------------------------------------------+

最佳答案

这个查询应该快得多。我已经移动了关于出生日期的条件。

SELECT *
FROM
(SELECT children.childrenID,
children.childBirthDate,
users.userID AS child_uid
FROM children,
users
WHERE children.userID = users.userID
AND children.childBirthDate <= DATE_SUB(CURDATE(), INTERVAL 5 MONTH)
AND children.childBirthDate > DATE_SUB(CURDATE() , INTERVAL 6 MONTH)
ORDER BY children.childBirthDate DESC)t1
GROUP BY child_uid

编辑

以我能写的最快形式的完整查询。我从 LIKE 中删除了 %,将子查询更改为连接并删除了 *。关于出生日期的条件也被移动了。不过,可能会有错误。

SELECT users.userID,
users.categoryGroup, children.childBirthDate,
children.childrenID
FROM
(SELECT MIN(childBirthDate) AS childBirthDate, userID
FROM children
WHERE childBirthDate <= DATE_SUB(CURDATE(), INTERVAL 5 MONTH)
AND childBirthDate > DATE_SUB(CURDATE() , INTERVAL 6 MONTH)
GROUP BY userID) AS ch1
INNER JOIN users ON users.userID = ch1.userID
INNER JOIN children ON users.userID = children.userID AND ch1.childBirthDate = children.childBirthDate
LEFT JOIN CuponRequests ON CuponRequests.userID = userID AND cuponID = 1
WHERE ('Group1' LIKE users.categoryGroup OR 'Group2' LIKE users.categoryGroup)
AND CuponRequest.ID IS NULL
AND userID = 1
ORDER BY children.childBirthDate DESC

详细描述

  • 子查询可能很慢。有时优化器无法做正确的事情。使用 ON 子句编写连接应该更安全。
  • 带有GROUP BY 的语句对于优化器来说更加复杂。在其中写入附加条件可能会有所帮助。
  • LIKE '%something%' 语句很难使用索引。 LIKE 'something%'LIKE 'something' 速度要快得多。
  • 有时将 * 更改为所需参数的显式列表是个好主意。有时所有需要的信息都在索引中,不需要直接从表中读取。它在极端情况下可能会有所帮助。

关于php - JOINed 表上的 GROUP BY 和 ORDER BY - 复杂且缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20192181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com