简体   繁体   English

如何处理大型mysql查询

[英]How to handle large mysql queries

i have the following query: 我有以下查询:

SELECT  p2c.pid AS productNumber,
        p.name AS productName
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  pid = p2c.pid
        ) AS registered
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  pid = p2c.pid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
        ) AS inWarranty
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  pid = p2c.pid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
        ) AS outOfWarranty
    ,   (
            SELECT DATE_FORMAT( MAX( from_unixtime(purchased) ), '%d.%m.%Y')
            FROM   products2customers
            WHERE  pid = p2c.pid
        ) AS lastPurchased
    ,   (
            SELECT DATE_FORMAT( date_add( MAX( from_unixtime(purchased) ), INTERVAL 5 YEAR), '%d.%m.%Y')
            FROM   products2customers
            WHERE  pid = p2c.pid
        ) AS warrantyUntil
FROM    (
            SELECT DISTINCT
                p2c.pid
            FROM
                products2customers p2c
        ) AS p2c
JOIN
    products p
ON
    p.id = p2c.pid
ORDER BY
    inWarranty DESC

The query is executed on a database table, which has 25.000 rows. 查询在具有25.000行的数据库表上执行。 The execution time for this query is at about 40 secs. 该查询的执行时间约为40秒。 The results will be shown at a webpage, so it's not so great to wait 40secs for the result. 结果将显示在网页上,因此等待40秒以获取结果并不是很好。

Is there a way, to execute this query and save it's output? 有没有办法执行此查询并保存其输出? Because it would be enough if this query is executed every night. 因为如果每天晚上执行此查询就足够了。

What's the best way for doing this? 最好的方法是什么? Should I create a cronjob and execute this query and write the result in a database? 我应该创建一个cronjob并执行此查询并将结果写入数据库吗? Or is there a better way? 或者,还有更好的方法?

Or can i optimize this query, to make it faster? 或者我可以优化此查询以使其更快?

I think all those correlated subqueries are killing you. 我认为所有这些相关的子查询都在杀死您。 Try this: 尝试这个:

SELECT  p2c.pid AS productNumber,
        p.name AS productName,
        COUNT(*) AS registered,
        SUM(date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()) AS inWarranty,
        SUM(date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()) AS outOfWarranty,
        DATE_FORMAT( MAX( from_unixtime(purchased) ), '%d.%m.%Y') AS lastPurchased,
        DATE_FORMAT( date_add( MAX( from_unixtime(purchased) ), INTERVAL 5 YEAR), '%d.%m.%Y') AS warrantyUntil
FROM products2customers p2c
JOIN products p ON p.id = p2c.pid
GROUP BY p2c.pid
ORDER BY inWarranty DESC

也许首先要Explain Plan一个Explain Plan ,看看是否可以通过创建索引来加速任何部分。

Put this into a cron job: 将其放入计划任务中:

CREATE TABLE products_warrany SELECT p2c.pid AS productNumber, ...

Later use the following query instead of original long one: 以后使用以下查询代替原来的较长查询:

SELECT * FROM products_warrany ORDER BY ...

You also gain ability to run additional filtering, ordering and so forth. 您还可以运行其他过滤,排序等功能。 Reference. 参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM