gpt4 book ai didi

mysql - 使我的 mysql 查询更加高效

转载 作者:行者123 更新时间:2023-11-29 14:06:58 24 4
gpt4 key购买 nike

我想要做的是从 Enswitch_Mobile_Users 表中提取 idphone_typeos_version 列。

通过 id,我只需从 Enswitch_Users 表中获取 enswitch_id

然后,计算 Enswitch_Android_PurchasesEnswitch_Iphone_Purchases 中用户列与 enswitch_mobile_users 中的 ID 匹配的所有整体。并获取第一次输入日期和最后一次输入日期。

我设法让它与这个查询一起工作:

SELECT p.user  AS `Mobile_User_ID`, 
e.os_version `Os_Version`,
e.phone_type `Phone_Type`,
eu.enswitch_id `Enswitch_ID`,
Count(1) AS `Buy_Count`,
(SELECT pc.date
FROM
(
SELECT date, user, status
FROM enswitch_android_purchases
UNION
SELECT date, user, status
FROM enswitch_iphone_purchases
) AS pc
WHERE pc.status = 1
AND pc.user = p.user
ORDER BY pc.date ASC
LIMIT 1) AS `First_Purchase`,
(SELECT pc.date
FROM
(
SELECT date, user, status
FROM enswitch_android_purchases
UNION
SELECT date, user, status
FROM enswitch_iphone_purchases
) AS pc
WHERE pc.status = 1
AND pc.user = p.user
ORDER BY pc.date DESC LIMIT 1) AS `Last_Purchase`
FROM
(
SELECT item, date, user, status
FROM enswitch_android_purchases
UNION
SELECT item, date, user, status
FROM enswitch_iphone_purchases
) AS p
LEFT JOIN enswitch_mobile_users e
ON p.user = e.id
LEFT JOIN enswitch_users eu
ON e.user_id = eu.id
WHERE p.`date` >= :from_date
AND p.`date` <= :to_date
AND p.user is not null
AND p.status = 1
GROUP BY `Mobile_User_ID`

但是由于选择的原因,它会非常慢,所以我怎样才能使它更有效率?

最佳答案

您也许可以使用以下命令,将 SELECT 列表中的两个选择替换为 min(p.date)max(p.date) ):

SELECT p.user  AS `Mobile_User_ID`, 
e.os_version `Os_Version`,
e.phone_type `Phone_Type`,
eu.enswitch_id `Enswitch_ID`,
Count(1) AS `Buy_Count`,
min(p.date) AS `First_Purchase`,
max(p.date) AS `Last_Purchase`
FROM
(
SELECT item, date, user, status
FROM enswitch_android_purchases
UNION
SELECT item, date, user, status
FROM enswitch_iphone_purchases
) AS p
LEFT JOIN enswitch_mobile_users e
ON p.user = e.id
LEFT JOIN enswitch_users eu
ON e.user_id = eu.id
WHERE p.`date` >= :from_date
AND p.`date` <= :to_date
AND p.user is not null
AND p.status = 1
GROUP BY p.user

关于mysql - 使我的 mysql 查询更加高效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198300/

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