English 中文(简体)
MySQL A. 所需成绩优化
原标题:MySQL Query optimization required

I ve got a slow performing query. I know using a dependent subquery is bad, but I can t think of another way to get the data I want.

基本上,我要向过去6个月至少有50张发票但本月没有发票的客户表示。

这就是我目前的情况:

select
    Customer.name,
    Customer.id,
    Customer.latitude,
    Customer.longitude
from
    Customer
where
    EXISTS (
        SELECT 
            *
        FROM
            Invoice_Header
        WHERE
            Invoice_Header.inv_date BETWEEN  2011-03-02  AND  2011-10-02 
        AND
            Invoice_Header.account_number = Customer.account_number
        HAVING COUNT(invoice_num) > 50
    )
    AND NOT EXISTS (
        SELECT * 
        FROM 
            Invoice_Header 
        WHERE
            InvHead.inv_date >  2011-10-02 
        AND
            InvHead.account_number = Customer.account_number
    )
Group by name;

客户表有大约12k记录,Invoice_Header有大约2米记录。

我的指数为:日数,账户编号(两个表)。

希望能就如何加快这项工作提出建议。

最佳回答

我建议:

SELECT
    c.name,
    c.id,
    c.latitude,
    c.longitude
FROM
    Customer AS c
    INNER JOIN (
        SELECT account_number, count(*) AS invoice_count
        FROM Invoice_Header
        WHERE inv_date >=  2011-03-02  AND inv_date <=  2011-10-02 
        GROUP BY account_number
    ) AS lsm
        ON c.account_number = lsm.account_number
    LEFT JOIN (
        SELECT account_number, count(*) AS invoice_count
        FROM Invoice_Header
        WHERE inv_date >  2011-10-02 
        GROUP BY account_number
    ) AS lm
        ON c.account_number = lm.account_number
    WHERE
        lsm.invoice_count >= 50
        AND IFNULL(lm.invoice_count, 0) = 0
问题回答

这应消除相关分局,并大大加快:

SELECT c.name, c.id, c.latitude, c.longitude
FROM Customer c
INNER JOIN (
  SELECT account_number
  FROM Invoice_Header ih
  WHERE ih.inv_date BETWEEN  2011-03-02  AND  2011-10-02 
  GROUP BY account_number
  HAVING COUNT(*) > 50
  MINUS
  SELECT DISTINCT account_number
  FROM Invoice_Header ih
  WHERE ih.inv_date >  2011-10-02 
) tbl
ON tbl.account_number = c.account_number
select
    C.name,
    C.id,
    C.latitude,
    C.longitude,
    I.account_number,         
    count( IF(I.inv_date>= 2011-03-02  AND I.inv_date <= 2011-10-02 ,I.inv_date,NULL )) as inv_count_6,
    count( IF(I.inv_date >  2011-10-02 ,I.inv_date,NULL )) as inv_count_1

from Customer C
LEFT JOIN Invoice_Header I
ON C.account_number = I.account_number 

GROUP BY C.id, I.account_number
HAVING inv_count_6 >= 50 AND inv_count_1=0
WHERE I.inv_date  BETWEEN  2011-03-02  AND  2011-10-02 

注:

1. 导言 发票是按英文第50条计算的,因此情况是“不”的;50。

2. 结 论 页: 1





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签