集計されたデータの多いテーブルから販売統計を収集する必要があるケースがあります。テーブルには数百万のエントリがあり、データベース自体は約22Gbです。一緒に遊ぶためのたくさんのデータ!SQLのクエリをより速く、より良くする
私はこの作業クエリを持っていますが、私はこれをより良くするべきだと感じています。また、それも非常に遅いです。
CentOS 7でsqlsrv拡張子を持つPHPを使用して、MSSQLデータベース(ERPシステム)に接続しています。このクエリで
私のPHP変数は、このように設定されています、だから、
$customer = '12345';
$year = '2016';
$compared_year = '2015';
あなたは既に推測している可能性があるため、私は2016年の各月の特定の顧客のための販売統計を収集していますし、 2015年
SELECT
ProdTr.CustNo AS customernummer,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "01' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_1,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "02' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_2,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "03' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_3,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "04' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_4,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "05' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_5,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "06' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_6,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "07' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_7,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "08' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_8,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "09' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_9,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "10' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_10,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "11' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_11,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $year . "12' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS sum_period_12,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "01' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_1,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "02' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_2,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "03' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_3,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "04' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_4,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "05' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_5,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "06' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_6,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "07' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_7,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "08' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_8,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "09' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_9,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "10' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_10,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "10' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_11,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $year . "11' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS dg_period_12,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "01' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_1,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "02' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_2,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "03' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_3,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "04' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_4,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "05' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_5,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "06' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_6,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "07' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_7,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "08' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_8,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "09' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_9,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "10' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_10,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "11' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_11,
(SELECT SUM(DAm) AS 'Sum' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "12' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_sum_period_12,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "01' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_1,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "02' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_2,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "03' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_3,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "04' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_4,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "05' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_5,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "06' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_6,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "07' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_7,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "08' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_8,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "09' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_9,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "10' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_10,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "11' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_11,
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM ProdTr WHERE AcYrPr = '" . $compared_year . "12' AND ProdTr.TrTp = 1 AND ProdTr.CustNo = '" . $customer . "') AS compared_dg_period_12
FROM ProdTr
WHERE AcYrPr LIKE 201612
AND ProdTr.TrTp = 1
AND ProdTr.CustNo = '" . $customer . "'
GROUP BY ProdTr.CustNo, ProdTr.TrTp, ProdTr.AcYrPr
クエリは私が期待する結果を与え、うまく動作することに注意してください - しかし、それは多くの注文/取引で顧客に遅すぎます。
これをスピードアップする方法の指針はありますか?
ありがとうございます!
あなたはGROUP BYを見ましたか? 1つのクエリですべてを実行できない場合でも、たとえば4つのクエリで実行し、結果を1つの結合配列に格納できます。 – Nick