长时间运行查询问题

时间:2012-05-11 09:23:00

标签: mysql mysql-slow-query-log

我正在尝试使用脚本来解决一些问题我没有在网站似乎正在快速填充mysql /tmp目录的地方。

我检查了slow query log,它有很多像这样的查询..

SELECT
    COUNT(*) AS `total`
FROM
    (
        SELECT
                *
        FROM
            `Advertising_Business_and_Retail`
        WHERE
            MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
                *
        FROM
            `Chemical` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
                *
        FROM `Clothing` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    )
AS t;

除了我相信45个工会总数(每个类别表1个),这些表格并不大,但Advertising_Business_and_Retail表格本身只有450k个列表。

我手动运行此查询,大约需要2.5分钟。

我猜这就是为什么/ tmp目录填满这么快?

我可以采取哪些措施来解决问题?

2 个答案:

答案 0 :(得分:1)

如果您确定所选行中没有重复项,请尝试将UNION替换为UNION ALL

此外,您还可以使用简单的添加替换UINON ALL

select (
  (select count(1) from `Advertising_Business_and_Retail` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company'))
+ (select count(1) from `Chemical` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company'))
+ (select count(1) from `Clothing` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company'))
) as total

答案 1 :(得分:1)

这会更快吗?

SELECT
    SUM(`count`) AS `total`
FROM
    (
        SELECT
               COUNT(*) AS `count`
        FROM
            `Advertising_Business_and_Retail`
        WHERE
            MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
               COUNT(*) AS `count`
        FROM
            `Chemical` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
                COUNT(*) AS `count`
        FROM `Clothing` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    )
AS t;